aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-04-02 09:24:14 +0100
committerRon Yorston <rmy@pobox.com>2018-04-02 09:24:14 +0100
commit34a68d327b42c3c700e84cd475496985782290b1 (patch)
tree99bfe59cca420d26f01e81a7f41763f71b44d22c /libbb
parentaff3c5bd7b6bdcfb97f63153ab839c5f55f16a12 (diff)
parente84212f8346741a2d4a04b40639c44fe519cf5a7 (diff)
downloadbusybox-w32-34a68d327b42c3c700e84cd475496985782290b1.tar.gz
busybox-w32-34a68d327b42c3c700e84cd475496985782290b1.tar.bz2
busybox-w32-34a68d327b42c3c700e84cd475496985782290b1.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'libbb')
-rw-r--r--libbb/get_line_from_file.c15
-rw-r--r--libbb/wfopen.c2
-rw-r--r--libbb/xfuncs.c7
-rw-r--r--libbb/xfuncs_printf.c15
4 files changed, 22 insertions, 17 deletions
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 4e09ddc80..09ccfba67 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -10,16 +10,19 @@
10 */ 10 */
11#include "libbb.h" 11#include "libbb.h"
12 12
13char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) 13char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end)
14{ 14{
15 int ch; 15 int ch;
16 unsigned idx = 0; 16 size_t idx = 0;
17 char *linebuf = NULL; 17 char *linebuf = NULL;
18 18
19 while ((ch = getc(file)) != EOF) { 19 while ((ch = getc(file)) != EOF) {
20 /* grow the line buffer as necessary */ 20 /* grow the line buffer as necessary */
21 if (!(idx & 0xff)) 21 if (!(idx & 0xff)) {
22 if (idx == ((size_t)-1) - 0xff)
23 bb_die_memory_exhausted();
22 linebuf = xrealloc(linebuf, idx + 0x100); 24 linebuf = xrealloc(linebuf, idx + 0x100);
25 }
23 linebuf[idx++] = (char) ch; 26 linebuf[idx++] = (char) ch;
24 if (ch == '\0') 27 if (ch == '\0')
25 break; 28 break;
@@ -44,14 +47,12 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
44/* Get line, including trailing \n if any */ 47/* Get line, including trailing \n if any */
45char* FAST_FUNC xmalloc_fgets(FILE *file) 48char* FAST_FUNC xmalloc_fgets(FILE *file)
46{ 49{
47 int i; 50 return bb_get_chunk_from_file(file, NULL);
48
49 return bb_get_chunk_from_file(file, &i);
50} 51}
51/* Get line. Remove trailing \n */ 52/* Get line. Remove trailing \n */
52char* FAST_FUNC xmalloc_fgetline(FILE *file) 53char* FAST_FUNC xmalloc_fgetline(FILE *file)
53{ 54{
54 int i; 55 size_t i;
55 char *c = bb_get_chunk_from_file(file, &i); 56 char *c = bb_get_chunk_from_file(file, &i);
56 57
57 if (i && c[--i] == '\n') 58 if (i && c[--i] == '\n')
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)
42{ 42{
43 FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r"); 43 FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
44 if (!fp) 44 if (!fp)
45 bb_error_msg_and_die(bb_msg_memory_exhausted); 45 bb_die_memory_exhausted();
46 return fp; 46 return fp;
47} 47}
48FILE* FAST_FUNC xfdopen_for_read(int fd) 48FILE* FAST_FUNC xfdopen_for_read(int fd)
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)
59 * A truncated result contains the first few digits of the result ala strncpy. 59 * A truncated result contains the first few digits of the result ala strncpy.
60 * Returns a pointer past last generated digit, does _not_ store NUL. 60 * Returns a pointer past last generated digit, does _not_ store NUL.
61 */ 61 */
62void BUG_sizeof(void);
63char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen) 62char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
64{ 63{
65 unsigned i, out, res; 64 unsigned i, out, res;
66 65
67 if (buflen) { 66 if (buflen) {
68 out = 0; 67 out = 0;
68
69 BUILD_BUG_ON(sizeof(n) != 4 && sizeof(n) != 8);
69 if (sizeof(n) == 4) 70 if (sizeof(n) == 4)
70 // 2^32-1 = 4294967295 71 // 2^32-1 = 4294967295
71 i = 1000000000; 72 i = 1000000000;
72#if UINT_MAX > 4294967295 /* prevents warning about "const too large" */ 73#if UINT_MAX > 0xffffffff /* prevents warning about "const too large" */
73 else 74 else
74 if (sizeof(n) == 8) 75 if (sizeof(n) == 8)
75 // 2^64-1 = 18446744073709551615 76 // 2^64-1 = 18446744073709551615
76 i = 10000000000000000000; 77 i = 10000000000000000000;
77#endif 78#endif
78 else
79 BUG_sizeof();
80 for (; i; i /= 10) { 79 for (; i; i /= 10) {
81 res = n / i; 80 res = n / i;
82 n = n % i; 81 n = n % i;
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 7032e5f8f..21263ccfe 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -25,6 +25,11 @@
25 * fail, so callers never need to check for errors. If it returned, it 25 * fail, so callers never need to check for errors. If it returned, it
26 * succeeded. */ 26 * succeeded. */
27 27
28void FAST_FUNC bb_die_memory_exhausted(void)
29{
30 bb_error_msg_and_die(bb_msg_memory_exhausted);
31}
32
28#ifndef DMALLOC 33#ifndef DMALLOC
29/* dmalloc provides variants of these that do abort() on failure. 34/* dmalloc provides variants of these that do abort() on failure.
30 * Since dmalloc's prototypes overwrite the impls here as they are 35 * Since dmalloc's prototypes overwrite the impls here as they are
@@ -44,7 +49,7 @@ void* FAST_FUNC xmalloc(size_t size)
44{ 49{
45 void *ptr = malloc(size); 50 void *ptr = malloc(size);
46 if (ptr == NULL && size != 0) 51 if (ptr == NULL && size != 0)
47 bb_error_msg_and_die(bb_msg_memory_exhausted); 52 bb_die_memory_exhausted();
48 return ptr; 53 return ptr;
49} 54}
50 55
@@ -55,7 +60,7 @@ void* FAST_FUNC xrealloc(void *ptr, size_t size)
55{ 60{
56 ptr = realloc(ptr, size); 61 ptr = realloc(ptr, size);
57 if (ptr == NULL && size != 0) 62 if (ptr == NULL && size != 0)
58 bb_error_msg_and_die(bb_msg_memory_exhausted); 63 bb_die_memory_exhausted();
59 return ptr; 64 return ptr;
60} 65}
61#endif /* DMALLOC */ 66#endif /* DMALLOC */
@@ -79,7 +84,7 @@ char* FAST_FUNC xstrdup(const char *s)
79 t = strdup(s); 84 t = strdup(s);
80 85
81 if (t == NULL) 86 if (t == NULL)
82 bb_error_msg_and_die(bb_msg_memory_exhausted); 87 bb_die_memory_exhausted();
83 88
84 return t; 89 return t;
85} 90}
@@ -327,14 +332,14 @@ char* FAST_FUNC xasprintf(const char *format, ...)
327 va_end(p); 332 va_end(p);
328 333
329 if (r < 0) 334 if (r < 0)
330 bb_error_msg_and_die(bb_msg_memory_exhausted); 335 bb_die_memory_exhausted();
331 return string_ptr; 336 return string_ptr;
332} 337}
333 338
334void FAST_FUNC xsetenv(const char *key, const char *value) 339void FAST_FUNC xsetenv(const char *key, const char *value)
335{ 340{
336 if (setenv(key, value, 1)) 341 if (setenv(key, value, 1))
337 bb_error_msg_and_die(bb_msg_memory_exhausted); 342 bb_die_memory_exhausted();
338} 343}
339 344
340/* Handles "VAR=VAL" strings, even those which are part of environ 345/* Handles "VAR=VAL" strings, even those which are part of environ