sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 27996f2b86ada4de79190fb1fff49b48091ef72a
parent 6ba882ec3900daaf7992707d4f1e8a25cbc2ec3f
Author: FRIGN <dev@frign.de>
Date:   Sat, 24 Jan 2015 21:25:40 +0100

Fix segmentation fault in cut(1)

Be stricter while resolving escapes in the delimiter-string and
error out when it has length 0 or contains an invalid escape.

Thanks to Hiltjo Posthuma's sharp eagle eyes this bug was spotted.

Diffstat:
Mcut.c | 7+++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/cut.c b/cut.c @@ -144,7 +144,7 @@ resolveescapes(char *s, size_t len) { size_t i, off, m; - for (i = 0; i < len - 1; i++) { + for (i = 0; i < len; i++) { if (s[i] != '\\') continue; off = 0; @@ -158,7 +158,8 @@ resolveescapes(char *s, size_t len) case 'r': s[i] = '\r'; off++; break; case 't': s[i] = '\t'; off++; break; case 'v': s[i] = '\v'; off++; break; - default: continue; + case '\0': eprintf("cut: null escape sequence in delimiter\n"); + default: eprintf("cut: invalid escape sequence '\\%c' in delimiter\n", s[i + 1]); } for (m = i + 1; m <= len - off; m++) @@ -191,6 +192,8 @@ main(int argc, char *argv[]) break; case 'd': delim = EARGF(usage()); + if (!*delim) + eprintf("cut: empty delimiter\n"); delimlen = resolveescapes(delim, strlen(delim)); break; case 'n':