From c1005355718055983912ebdd79357b11894e0958 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 29 Nov 2018 11:44:10 +0100 Subject: cat,nl: fix handling of open errors $ cat -n does_not_exist; echo $? cat: does_not_exist: No such file or directory 1 function old new delta print_numbered_lines 118 129 +11 nl_main 196 201 +5 cat_main 421 425 +4 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 20/0) Total: 20 bytes Signed-off-by: Denys Vlasenko --- libbb/print_numbered_lines.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/print_numbered_lines.c b/libbb/print_numbered_lines.c index 9a8a51440..d6459d7c3 100644 --- a/libbb/print_numbered_lines.c +++ b/libbb/print_numbered_lines.c @@ -8,12 +8,16 @@ #include "libbb.h" -void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename) +int FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename) { FILE *fp = fopen_or_warn_stdin(filename); - unsigned N = ns->start; + unsigned N; char *line; + if (!fp) + return EXIT_FAILURE; + + N = ns->start; while ((line = xmalloc_fgetline(fp)) != NULL) { if (ns->all || (ns->nonempty && line[0]) @@ -27,4 +31,6 @@ void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filenam ns->start = N; fclose(fp); + + return EXIT_SUCCESS; } -- cgit v1.2.3-55-g6feb From 480c7e5dfbddafa763f241aecfe3831d2b3dfca5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 29 Nov 2018 12:34:50 +0100 Subject: libbb: @ in "\x3@" is not a valid hex digit function old new delta bb_process_escape_sequence 134 141 +7 Signed-off-by: Denys Vlasenko --- libbb/process_escape_sequence.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c index 59d0d3ea8..11adbfcea 100644 --- a/libbb/process_escape_sequence.c +++ b/libbb/process_escape_sequence.c @@ -41,8 +41,16 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr) unsigned d = (unsigned char)(*q) - '0'; #else unsigned d = (unsigned char)_tolower(*q) - '0'; - if (d >= 10) - d += ('0' - 'a' + 10); + if (d >= 10) { + //d += ('0' - 'a' + 10); + /* The above would maps 'A'-'F' and 'a'-'f' to 10-15, + * however, some chars like '@' would map to 9 < base. + * Do not allow that, map invalid chars to N > base: + */ + d += ('0' - 'a'); + if ((int)d >= 0) + d += 10; + } #endif if (d >= base) { if (WANT_HEX_ESCAPES && base == 16) { -- cgit v1.2.3-55-g6feb From 9a2b6dcc2d3298499b88aea3b04c967f2f9ae965 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 29 Nov 2018 13:15:57 +0100 Subject: libbb: do not misinterpret 0x10-0x19 chars in "\xNNN" too function old new delta bb_process_escape_sequence 141 151 +10 Signed-off-by: Denys Vlasenko --- libbb/process_escape_sequence.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'libbb') diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c index 11adbfcea..13022b83e 100644 --- a/libbb/process_escape_sequence.c +++ b/libbb/process_escape_sequence.c @@ -37,17 +37,15 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr) * We treat \2 as a valid octal escape sequence. */ do { unsigned r; -#if !WANT_HEX_ESCAPES unsigned d = (unsigned char)(*q) - '0'; -#else - unsigned d = (unsigned char)_tolower(*q) - '0'; +#if WANT_HEX_ESCAPES if (d >= 10) { - //d += ('0' - 'a' + 10); - /* The above would maps 'A'-'F' and 'a'-'f' to 10-15, + d = (unsigned char)_tolower(*q) - 'a'; + //d += 10; + /* The above would map 'A'-'F' and 'a'-'f' to 10-15, * however, some chars like '@' would map to 9 < base. * Do not allow that, map invalid chars to N > base: */ - d += ('0' - 'a'); if ((int)d >= 0) d += 10; } -- cgit v1.2.3-55-g6feb