From e4defe826be49b8ef19912ba4cb291bfe9166e0f Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 28 Mar 2018 17:12:56 +0200 Subject: libbb: use BUILD_BUG_ON in utoa_to_buf() Signed-off-by: Denys Vlasenko --- libbb/xfuncs.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 43ae98065..e8c027f17 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -59,24 +59,23 @@ char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src) * A truncated result contains the first few digits of the result ala strncpy. * Returns a pointer past last generated digit, does _not_ store NUL. */ -void BUG_sizeof(void); char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) { unsigned i, out, res; if (buflen) { out = 0; + + BUILD_BUG_ON(sizeof(n) != 4 && sizeof(n) != 8); if (sizeof(n) == 4) // 2^32-1 = 4294967295 i = 1000000000; -#if UINT_MAX > 4294967295 /* prevents warning about "const too large" */ +#if UINT_MAX > 0xffffffff /* prevents warning about "const too large" */ else if (sizeof(n) == 8) // 2^64-1 = 18446744073709551615 i = 10000000000000000000; #endif - else - BUG_sizeof(); for (; i; i /= 10) { res = n / i; n = n % i; -- cgit v1.2.3-55-g6feb From 2da9724b56169f00bd7fb6b9a11c9409a7620981 Mon Sep 17 00:00:00 2001 From: Quentin Rameau Date: Sun, 1 Apr 2018 17:05:35 +0200 Subject: libbb: remove unnecessary variable in xmalloc_fgets Signed-off-by: Quentin Rameau Signed-off-by: Denys Vlasenko --- libbb/get_line_from_file.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'libbb') diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index cc61a9265..941ea12b5 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -44,9 +44,7 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) /* Get line, including trailing \n if any */ char* FAST_FUNC xmalloc_fgets(FILE *file) { - int i; - - return bb_get_chunk_from_file(file, &i); + return bb_get_chunk_from_file(file, NULL); } /* Get line. Remove trailing \n */ char* FAST_FUNC xmalloc_fgetline(FILE *file) -- cgit v1.2.3-55-g6feb From e2afae6303e871a31a061d03359cfcd5dd86c088 Mon Sep 17 00:00:00 2001 From: Quentin Rameau Date: Sun, 1 Apr 2018 19:49:58 +0200 Subject: sed: prevent overflow of length from bb_get_chunk_from_file This fragment did not work right: temp = bb_get_chunk_from_file(fp, &len); if (temp) { /* len > 0 here, it's ok to do temp[len-1] */ char c = temp[len-1]; With "int len" _sign-extending_, temp[len-1] can refer to a wrong location if len > 0x7fffffff. Signed-off-by: Quentin Rameau Signed-off-by: Denys Vlasenko --- editors/sed.c | 2 +- include/libbb.h | 2 +- libbb/get_line_from_file.c | 11 +++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) (limited to 'libbb') diff --git a/editors/sed.c b/editors/sed.c index 9d800c2c3..470220859 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -988,7 +988,7 @@ static void flush_append(char *last_puts_char) static char *get_next_line(char *gets_char, char *last_puts_char) { char *temp = NULL; - int len; + size_t len; char gc; flush_append(last_puts_char); diff --git a/include/libbb.h b/include/libbb.h index fa878433e..309c58734 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -911,7 +911,7 @@ extern void xprint_and_close_file(FILE *file) FAST_FUNC; * end of line. If end isn't NULL, length of the chunk is stored in it. * Returns NULL if EOF/error. */ -extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC; +extern char *bb_get_chunk_from_file(FILE *file, size_t *end) FAST_FUNC; /* Reads up to (and including) TERMINATING_STRING: */ extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC RETURNS_MALLOC; /* Same, with limited max size, and returns the length (excluding NUL): */ diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 941ea12b5..d10066937 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -10,16 +10,19 @@ */ #include "libbb.h" -char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) +char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) { int ch; - unsigned idx = 0; + size_t idx = 0; char *linebuf = NULL; while ((ch = getc(file)) != EOF) { /* grow the line buffer as necessary */ - if (!(idx & 0xff)) + if (!(idx & 0xff)) { + if (idx == ((size_t)-1) - 0xff) + bb_error_msg_and_die(bb_msg_memory_exhausted); linebuf = xrealloc(linebuf, idx + 0x100); + } linebuf[idx++] = (char) ch; if (ch == '\0') break; @@ -49,7 +52,7 @@ char* FAST_FUNC xmalloc_fgets(FILE *file) /* Get line. Remove trailing \n */ char* FAST_FUNC xmalloc_fgetline(FILE *file) { - int i; + size_t i; char *c = bb_get_chunk_from_file(file, &i); if (i && c[--i] == '\n') -- cgit v1.2.3-55-g6feb From 899ae5337acc2d24edcd64e570adfc5f3c1a8a8a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 1 Apr 2018 19:59:37 +0200 Subject: libbb: new function bb_die_memory_exhausted function old new delta bb_die_memory_exhausted - 10 +10 xstrdup 28 23 -5 xsetenv 27 22 -5 xrealloc 32 27 -5 xputenv 22 17 -5 xmalloc 30 25 -5 xfdopen_helper 40 35 -5 xasprintf 44 39 -5 wget_main 2387 2382 -5 open_socket 54 49 -5 glob_brace 419 414 -5 bb_get_chunk_from_file 146 141 -5 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/11 up/down: 10/-55) Total: -45 bytes Signed-off-by: Denys Vlasenko --- archival/libarchive/data_extract_to_command.c | 2 +- include/libbb.h | 29 ++++++++++++++------------- libbb/get_line_from_file.c | 2 +- libbb/wfopen.c | 2 +- libbb/xfuncs_printf.c | 15 +++++++++----- networking/wget.c | 4 ++-- shell/hush.c | 4 ++-- 7 files changed, 32 insertions(+), 26 deletions(-) (limited to 'libbb') diff --git a/archival/libarchive/data_extract_to_command.c b/archival/libarchive/data_extract_to_command.c index 1114a95cb..0fcabb4a9 100644 --- a/archival/libarchive/data_extract_to_command.c +++ b/archival/libarchive/data_extract_to_command.c @@ -37,7 +37,7 @@ static const char *const tar_var[] = { static void xputenv(char *str) { if (putenv(str)) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); } static void str2env(char *env[], int idx, const char *str) diff --git a/include/libbb.h b/include/libbb.h index 309c58734..ad1c7346f 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1277,20 +1277,21 @@ extern smallint syslog_level; extern smallint logmode; extern uint8_t xfunc_error_retval; extern void (*die_func)(void); -extern void xfunc_die(void) NORETURN FAST_FUNC; -extern void bb_show_usage(void) NORETURN FAST_FUNC; -extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; -extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; -extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; -extern void bb_simple_perror_msg(const char *s) FAST_FUNC; -extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; -extern void bb_simple_perror_msg_and_die(const char *s) NORETURN FAST_FUNC; -extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; -extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; -extern void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC; -extern void bb_perror_nomsg(void) FAST_FUNC; -extern void bb_verror_msg(const char *s, va_list p, const char *strerr) FAST_FUNC; -extern void bb_logenv_override(void) FAST_FUNC; +void xfunc_die(void) NORETURN FAST_FUNC; +void bb_show_usage(void) NORETURN FAST_FUNC; +void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; +void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; +void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; +void bb_simple_perror_msg(const char *s) FAST_FUNC; +void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; +void bb_simple_perror_msg_and_die(const char *s) NORETURN FAST_FUNC; +void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; +void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; +void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC; +void bb_perror_nomsg(void) FAST_FUNC; +void bb_verror_msg(const char *s, va_list p, const char *strerr) FAST_FUNC; +void bb_die_memory_exhausted(void) NORETURN FAST_FUNC; +void bb_logenv_override(void) FAST_FUNC; /* We need to export XXX_main from libbusybox * only if we build "individual" binaries diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index d10066937..f3d6c6203 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -20,7 +20,7 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) /* grow the line buffer as necessary */ if (!(idx & 0xff)) { if (idx == ((size_t)-1) - 0xff) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); linebuf = xrealloc(linebuf, idx + 0x100); } linebuf[idx++] = (char) ch; diff --git a/libbb/wfopen.c b/libbb/wfopen.c index 20fe18b23..1c7f7f3d7 100644 --- a/libbb/wfopen.c +++ b/libbb/wfopen.c @@ -42,7 +42,7 @@ static FILE* xfdopen_helper(unsigned fd_and_rw_bit) { FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r"); if (!fp) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); return fp; } FILE* FAST_FUNC xfdopen_for_read(int fd) diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 2bc01ad10..7247c915b 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -25,6 +25,11 @@ * fail, so callers never need to check for errors. If it returned, it * succeeded. */ +void FAST_FUNC bb_die_memory_exhausted(void) +{ + bb_error_msg_and_die(bb_msg_memory_exhausted); +} + #ifndef DMALLOC /* dmalloc provides variants of these that do abort() on failure. * Since dmalloc's prototypes overwrite the impls here as they are @@ -44,7 +49,7 @@ void* FAST_FUNC xmalloc(size_t size) { void *ptr = malloc(size); if (ptr == NULL && size != 0) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); return ptr; } @@ -55,7 +60,7 @@ void* FAST_FUNC xrealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); if (ptr == NULL && size != 0) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); return ptr; } #endif /* DMALLOC */ @@ -79,7 +84,7 @@ char* FAST_FUNC xstrdup(const char *s) t = strdup(s); if (t == NULL) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); return t; } @@ -327,14 +332,14 @@ char* FAST_FUNC xasprintf(const char *format, ...) va_end(p); if (r < 0) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); return string_ptr; } void FAST_FUNC xsetenv(const char *key, const char *value) { if (setenv(key, value, 1)) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); } /* Handles "VAR=VAL" strings, even those which are part of environ diff --git a/networking/wget.c b/networking/wget.c index 8969310a4..12ee29a6f 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -409,7 +409,7 @@ static FILE *open_socket(len_and_sockaddr *lsa) /* hopefully it understands what ESPIPE means... */ fp = fdopen(fd, "r+"); if (!fp) - bb_perror_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); return fp; } @@ -1114,7 +1114,7 @@ static void download_one_url(const char *url) # endif sfp = fdopen(fd, "r+"); if (!sfp) - bb_perror_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); goto socket_opened; } sfp = open_socket(lsa); diff --git a/shell/hush.c b/shell/hush.c index 1921932d1..8246b5fd8 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -3158,7 +3158,7 @@ static int glob_brace(char *pattern, o_string *o, int n) return o_save_ptr_helper(o, n); } if (gr == GLOB_NOSPACE) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); /* GLOB_ABORTED? Only happens with GLOB_ERR flag, * but we didn't specify it. Paranoia again. */ bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); @@ -3260,7 +3260,7 @@ static int perform_glob(o_string *o, int n) goto literal; } if (gr == GLOB_NOSPACE) - bb_error_msg_and_die(bb_msg_memory_exhausted); + bb_die_memory_exhausted(); /* GLOB_ABORTED? Only happens with GLOB_ERR flag, * but we didn't specify it. Paranoia again. */ bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); -- cgit v1.2.3-55-g6feb