aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-04 10:16:52 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-04 10:16:52 +0000
commit74324c86663f57a19c1de303ee8c8e5449db9ef2 (patch)
tree11f5da9de4212875ce5811be2e1050e076378c9a
parent4e5f82c76f08614d0b69f9ec4a8baac303af15f6 (diff)
downloadbusybox-w32-74324c86663f57a19c1de303ee8c8e5449db9ef2.tar.gz
busybox-w32-74324c86663f57a19c1de303ee8c8e5449db9ef2.tar.bz2
busybox-w32-74324c86663f57a19c1de303ee8c8e5449db9ef2.zip
Audit bb_common_bufsiz usage, add script which looks for misuse.
tr: stop using globals needlessly. code: -103 bytes
-rw-r--r--archival/tar.c10
-rw-r--r--coreutils/catv.c5
-rw-r--r--coreutils/cksum.c5
-rw-r--r--coreutils/date.c7
-rw-r--r--coreutils/dos2unix.c51
-rw-r--r--coreutils/split.c2
-rw-r--r--coreutils/tr.c87
-rw-r--r--debianutils/readlink.c2
-rw-r--r--editors/ed.c8
-rw-r--r--editors/sed.c15
-rw-r--r--include/libbb.h3
-rw-r--r--libbb/messages.c2
-rw-r--r--loginutils/adduser.c11
-rw-r--r--miscutils/dc.c2
-rw-r--r--modutils/rmmod.c8
-rw-r--r--networking/hostname.c9
-rw-r--r--networking/nc.c8
-rw-r--r--networking/telnet.c15
-rwxr-xr-xscripts/find_bad_common_bufsiz13
-rwxr-xr-xscripts/find_stray_communal_vars2
-rw-r--r--sysklogd/logger.c9
21 files changed, 140 insertions, 134 deletions
diff --git a/archival/tar.c b/archival/tar.c
index 432355353..adcedf615 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -28,6 +28,8 @@
28#include "libbb.h" 28#include "libbb.h"
29#include "unarchive.h" 29#include "unarchive.h"
30 30
31#define block_buf bb_common_bufsiz1
32
31#if ENABLE_FEATURE_TAR_CREATE 33#if ENABLE_FEATURE_TAR_CREATE
32 34
33/* Tar file constants */ 35/* Tar file constants */
@@ -475,8 +477,8 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
475 /* Pad the file up to the tar block size */ 477 /* Pad the file up to the tar block size */
476 /* (a few tricks here in the name of code size) */ 478 /* (a few tricks here in the name of code size) */
477 readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1); 479 readSize = (-(int)statbuf->st_size) & (TAR_BLOCK_SIZE-1);
478 memset(bb_common_bufsiz1, 0, readSize); 480 memset(block_buf, 0, readSize);
479 xwrite(tbInfo->tarFd, bb_common_bufsiz1, readSize); 481 xwrite(tbInfo->tarFd, block_buf, readSize);
480 } 482 }
481 483
482 return TRUE; 484 return TRUE;
@@ -570,8 +572,8 @@ static int writeTarFile(const int tar_fd, const int verboseFlag,
570 include = include->link; 572 include = include->link;
571 } 573 }
572 /* Write two empty blocks to the end of the archive */ 574 /* Write two empty blocks to the end of the archive */
573 memset(bb_common_bufsiz1, 0, 2*TAR_BLOCK_SIZE); 575 memset(block_buf, 0, 2*TAR_BLOCK_SIZE);
574 xwrite(tbInfo.tarFd, bb_common_bufsiz1, 2*TAR_BLOCK_SIZE); 576 xwrite(tbInfo.tarFd, block_buf, 2*TAR_BLOCK_SIZE);
575 577
576 /* To be pedantically correct, we would check if the tarball 578 /* To be pedantically correct, we would check if the tarball
577 * is smaller than 20 tar blocks, and pad it if it was smaller, 579 * is smaller than 20 tar blocks, and pad it if it was smaller,
diff --git a/coreutils/catv.c b/coreutils/catv.c
index 2d2229f7f..cc61233e1 100644
--- a/coreutils/catv.c
+++ b/coreutils/catv.c
@@ -33,13 +33,14 @@ int catv_main(int argc, char **argv)
33 else for (;;) { 33 else for (;;) {
34 int i, res; 34 int i, res;
35 35
36 res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); 36#define read_buf bb_common_bufsiz1
37 res = read(fd, read_buf, COMMON_BUFSIZE);
37 if (res < 0) 38 if (res < 0)
38 retval = EXIT_FAILURE; 39 retval = EXIT_FAILURE;
39 if (res < 1) 40 if (res < 1)
40 break; 41 break;
41 for (i = 0; i < res; i++) { 42 for (i = 0; i < res; i++) {
42 char c = bb_common_bufsiz1[i]; 43 char c = read_buf[i];
43 44
44 if (c > 126 && (flags & CATV_OPT_v)) { 45 if (c > 126 && (flags & CATV_OPT_v)) {
45 if (c == 127) { 46 if (c == 127) {
diff --git a/coreutils/cksum.c b/coreutils/cksum.c
index 865bea0ee..987f5f32c 100644
--- a/coreutils/cksum.c
+++ b/coreutils/cksum.c
@@ -27,8 +27,9 @@ int cksum_main(int argc, char **argv)
27 crc = 0; 27 crc = 0;
28 length = 0; 28 length = 0;
29 29
30 while ((bytes_read = fread(bb_common_bufsiz1, 1, BUFSIZ, fp)) > 0) { 30#define read_buf bb_common_bufsiz1
31 cp = bb_common_bufsiz1; 31 while ((bytes_read = fread(read_buf, 1, BUFSIZ, fp)) > 0) {
32 cp = read_buf;
32 length += bytes_read; 33 length += bytes_read;
33 while (bytes_read--) 34 while (bytes_read--)
34 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL]; 35 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
diff --git a/coreutils/date.c b/coreutils/date.c
index 57c826a3f..cec8854ff 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -222,9 +222,10 @@ int date_main(int argc, char **argv)
222 date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y"; 222 date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y";
223 } 223 }
224 224
225#define date_buf bb_common_bufsiz1
225 if (*date_fmt == '\0') { 226 if (*date_fmt == '\0') {
226 /* With no format string, just print a blank line */ 227 /* With no format string, just print a blank line */
227 *bb_common_bufsiz1 = 0; 228 date_buf[0] = '\0';
228 } else { 229 } else {
229 /* Handle special conversions */ 230 /* Handle special conversions */
230 231
@@ -233,9 +234,9 @@ int date_main(int argc, char **argv)
233 } 234 }
234 235
235 /* Generate output string */ 236 /* Generate output string */
236 strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time); 237 strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time);
237 } 238 }
238 puts(bb_common_bufsiz1); 239 puts(date_buf);
239 240
240 return EXIT_SUCCESS; 241 return EXIT_SUCCESS;
241} 242}
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index b053a0cbf..86adcd91f 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -14,17 +14,20 @@
14 14
15#include "libbb.h" 15#include "libbb.h"
16 16
17enum ConvType { 17enum {
18 CT_UNIX2DOS = 1, 18 CT_UNIX2DOS = 1,
19 CT_DOS2UNIX 19 CT_DOS2UNIX
20} ConvType; 20};
21 21
22/* if fn is NULL then input is stdin and output is stdout */ 22/* if fn is NULL then input is stdin and output is stdout */
23static int convert(char *fn) 23static int convert(char *fn, int ConvType)
24{ 24{
25 FILE *in, *out; 25 FILE *in, *out;
26 int i; 26 int i;
27#define name_buf bb_common_bufsiz1
27 28
29 in = stdin;
30 out = stdout;
28 if (fn != NULL) { 31 if (fn != NULL) {
29 in = xfopen(fn, "rw"); 32 in = xfopen(fn, "rw");
30 /* 33 /*
@@ -32,24 +35,17 @@ static int convert(char *fn)
32 permissions 0666 for glibc 2.0.6 and earlier or 35 permissions 0666 for glibc 2.0.6 and earlier or
33 0600 for glibc 2.0.7 and later. 36 0600 for glibc 2.0.7 and later.
34 */ 37 */
35 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); 38 snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
36 /* 39 i = mkstemp(&name_buf[0]);
37 sizeof bb_common_bufsiz1 is 4096, so it should be big enough to 40 if (i == -1 || chmod(name_buf, 0600) == -1) {
38 hold the full path. However if the output is truncated the
39 subsequent call to mkstemp would fail.
40 */
41 i = mkstemp(&bb_common_bufsiz1[0]);
42 if (i == -1 || chmod(bb_common_bufsiz1, 0600) == -1) {
43 bb_perror_nomsg_and_die(); 41 bb_perror_nomsg_and_die();
44 } 42 }
45 out = fdopen(i, "w+"); 43 out = fdopen(i, "w+");
46 if (!out) { 44 if (!out) {
47 close(i); 45 close(i);
48 remove(bb_common_bufsiz1); 46 remove(name_buf);
47 return -2;
49 } 48 }
50 } else {
51 in = stdin;
52 out = stdout;
53 } 49 }
54 50
55 while ((i = fgetc(in)) != EOF) { 51 while ((i = fgetc(in)) != EOF) {
@@ -67,14 +63,14 @@ static int convert(char *fn)
67 if (fn != NULL) { 63 if (fn != NULL) {
68 if (fclose(in) < 0 || fclose(out) < 0) { 64 if (fclose(in) < 0 || fclose(out) < 0) {
69 bb_perror_nomsg(); 65 bb_perror_nomsg();
70 remove(bb_common_bufsiz1); 66 remove(name_buf);
71 return -2; 67 return -2;
72 } 68 }
73 /* Assume they are both on the same filesystem (which 69 /* Assume they are both on the same filesystem (which
74 * should be true since we put them into the same directory 70 * should be true since we put them into the same directory
75 * so we _should_ be ok, but you never know... */ 71 * so we _should_ be ok, but you never know... */
76 if (rename(bb_common_bufsiz1, fn) < 0) { 72 if (rename(name_buf, fn) < 0) {
77 bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn); 73 bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
78 return -1; 74 return -1;
79 } 75 }
80 } 76 }
@@ -85,13 +81,13 @@ static int convert(char *fn)
85int dos2unix_main(int argc, char **argv); 81int dos2unix_main(int argc, char **argv);
86int dos2unix_main(int argc, char **argv) 82int dos2unix_main(int argc, char **argv)
87{ 83{
88 int o; 84 int o, ConvType;
89 85
90 /* See if we are supposed to be doing dos2unix or unix2dos */ 86 /* See if we are supposed to be doing dos2unix or unix2dos */
91 if (applet_name[0] == 'd') { 87 if (applet_name[0] == 'd') {
92 ConvType = CT_DOS2UNIX; /*2 */ 88 ConvType = CT_DOS2UNIX; /* 2 */
93 } else { 89 } else {
94 ConvType = CT_UNIX2DOS; /*1 */ 90 ConvType = CT_UNIX2DOS; /* 1 */
95 } 91 }
96 /* -u and -d are mutally exclusive */ 92 /* -u and -d are mutally exclusive */
97 opt_complementary = "?:u--d:d--u"; 93 opt_complementary = "?:u--d:d--u";
@@ -105,12 +101,13 @@ int dos2unix_main(int argc, char **argv)
105 if (o) 101 if (o)
106 ConvType = o; 102 ConvType = o;
107 103
108 if (optind < argc) { 104 do {
109 while (optind < argc) 105 /* might be convert(NULL) if there is no filename given */
110 if ((o = convert(argv[optind++])) < 0) 106 o = convert(argv[optind], ConvType);
111 break; 107 if (o < 0)
112 } else 108 break;
113 o = convert(NULL); 109 optind++;
110 } while (optind < argc);
114 111
115 return o; 112 return o;
116} 113}
diff --git a/coreutils/split.c b/coreutils/split.c
index 27f9cfdd6..7b4f8c2c0 100644
--- a/coreutils/split.c
+++ b/coreutils/split.c
@@ -49,7 +49,7 @@ static char *next_file(char *old, unsigned suffix_len)
49} 49}
50 50
51#define read_buffer bb_common_bufsiz1 51#define read_buffer bb_common_bufsiz1
52enum { READ_BUFFER_SIZE = sizeof(bb_common_bufsiz1) - 1 }; 52enum { READ_BUFFER_SIZE = COMMON_BUFSIZE - 1 };
53 53
54#define SPLIT_OPT_l (1<<0) 54#define SPLIT_OPT_l (1<<0)
55#define SPLIT_OPT_b (1<<1) 55#define SPLIT_OPT_b (1<<1)
diff --git a/coreutils/tr.c b/coreutils/tr.c
index 7e89e9a80..c0d0dfacb 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -25,41 +25,9 @@
25#define TR_OPT_complement (1<<0) 25#define TR_OPT_complement (1<<0)
26#define TR_OPT_delete (1<<1) 26#define TR_OPT_delete (1<<1)
27#define TR_OPT_squeeze_reps (1<<2) 27#define TR_OPT_squeeze_reps (1<<2)
28/* some "globals" shared across this file */
29/* these last are pointers to static buffers declared in tr_main */
30static char *poutput, *pvector, *pinvec, *poutvec;
31 28
32static void ATTRIBUTE_NORETURN convert(const smalluint flags) 29static void map(char *pvector,
33{ 30 unsigned char *string1, unsigned int string1_len,
34 size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
35
36 for (;;) {
37 /* If we're out of input, flush output and read more input. */
38 if (in_index == read_chars) {
39 if (out_index) {
40 xwrite(STDOUT_FILENO, (char *)poutput, out_index);
41 out_index = 0;
42 }
43 if ((read_chars = read(STDIN_FILENO, bb_common_bufsiz1, BUFSIZ)) <= 0) {
44 if (write(STDOUT_FILENO, (char *)poutput, out_index) != out_index)
45 bb_perror_msg(bb_msg_write_error);
46 exit(EXIT_SUCCESS);
47 }
48 in_index = 0;
49 }
50 c = bb_common_bufsiz1[in_index++];
51 coded = pvector[c];
52 if ((flags & TR_OPT_delete) && pinvec[c])
53 continue;
54 if ((flags & TR_OPT_squeeze_reps) && last == coded &&
55 (pinvec[c] || poutvec[coded]))
56 continue;
57 poutput[out_index++] = last = coded;
58 }
59 /* NOTREACHED */
60}
61
62static void map(unsigned char *string1, unsigned int string1_len,
63 unsigned char *string2, unsigned int string2_len) 31 unsigned char *string2, unsigned int string2_len)
64{ 32{
65 char last = '0'; 33 char last = '0';
@@ -121,9 +89,9 @@ static unsigned int expand(const char *arg, char *buffer)
121 if (ENABLE_FEATURE_TR_CLASSES && i == ':') { 89 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
122 smalluint j; 90 smalluint j;
123 { /* not really pretty.. */ 91 { /* not really pretty.. */
124 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7 92 char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7
125 j = index_in_str_array(classes, tmp) + 1; 93 j = index_in_str_array(classes, tmp) + 1;
126 free(tmp); 94 free(tmp);
127 } 95 }
128 if (j == CLASS_alnum || j == CLASS_digit) { 96 if (j == CLASS_alnum || j == CLASS_digit) {
129 for (i = '0'; i <= '9'; i++) 97 for (i = '0'; i <= '9'; i++)
@@ -183,7 +151,7 @@ static unsigned int expand(const char *arg, char *buffer)
183 151
184static int complement(char *buffer, int buffer_len) 152static int complement(char *buffer, int buffer_len)
185{ 153{
186 short i, j, ix; 154 int i, j, ix;
187 char conv[ASCII + 2]; 155 char conv[ASCII + 2];
188 156
189 ix = 0; 157 ix = 0;
@@ -206,17 +174,12 @@ int tr_main(int argc, char **argv)
206 int idx = 1; 174 int idx = 1;
207 int i; 175 int i;
208 smalluint flags = 0; 176 smalluint flags = 0;
177 size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
209 RESERVE_CONFIG_UBUFFER(output, BUFSIZ); 178 RESERVE_CONFIG_UBUFFER(output, BUFSIZ);
210 RESERVE_CONFIG_BUFFER(vector, ASCII+1); 179 RESERVE_CONFIG_BUFFER(vector, ASCII+1);
211 RESERVE_CONFIG_BUFFER(invec, ASCII+1); 180 RESERVE_CONFIG_BUFFER(invec, ASCII+1);
212 RESERVE_CONFIG_BUFFER(outvec, ASCII+1); 181 RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
213 182
214 /* ... but make them available globally */
215 poutput = output;
216 pvector = vector;
217 pinvec = invec;
218 poutvec = outvec;
219
220 if (argc > 1 && argv[idx][0] == '-') { 183 if (argc > 1 && argv[idx][0] == '-') {
221 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) { 184 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
222 if (*ptr == 'c') 185 if (*ptr == 'c')
@@ -235,21 +198,47 @@ int tr_main(int argc, char **argv)
235 invec[i] = outvec[i] = FALSE; 198 invec[i] = outvec[i] = FALSE;
236 } 199 }
237 200
201#define tr_buf bb_common_bufsiz1
238 if (argv[idx] != NULL) { 202 if (argv[idx] != NULL) {
239 input_length = expand(argv[idx++], bb_common_bufsiz1); 203 input_length = expand(argv[idx++], tr_buf);
240 if (flags & TR_OPT_complement) 204 if (flags & TR_OPT_complement)
241 input_length = complement(bb_common_bufsiz1, input_length); 205 input_length = complement(tr_buf, input_length);
242 if (argv[idx] != NULL) { 206 if (argv[idx] != NULL) {
243 if (*argv[idx] == '\0') 207 if (*argv[idx] == '\0')
244 bb_error_msg_and_die("STRING2 cannot be empty"); 208 bb_error_msg_and_die("STRING2 cannot be empty");
245 output_length = expand(argv[idx], output); 209 output_length = expand(argv[idx], output);
246 map(bb_common_bufsiz1, input_length, output, output_length); 210 map(vector, tr_buf, input_length, output, output_length);
247 } 211 }
248 for (i = 0; i < input_length; i++) 212 for (i = 0; i < input_length; i++)
249 invec[(unsigned char)bb_common_bufsiz1[i]] = TRUE; 213 invec[(unsigned char)tr_buf[i]] = TRUE;
250 for (i = 0; i < output_length; i++) 214 for (i = 0; i < output_length; i++)
251 outvec[output[i]] = TRUE; 215 outvec[output[i]] = TRUE;
252 } 216 }
253 convert(flags); 217
218 for (;;) {
219 /* If we're out of input, flush output and read more input. */
220 if (in_index == read_chars) {
221 if (out_index) {
222 xwrite(STDOUT_FILENO, (char *)output, out_index);
223 out_index = 0;
224 }
225 read_chars = read(STDIN_FILENO, tr_buf, BUFSIZ);
226 if (read_chars <= 0) {
227 if (write(STDOUT_FILENO, (char *)output, out_index) != out_index)
228 bb_perror_msg(bb_msg_write_error);
229 exit(EXIT_SUCCESS);
230 }
231 in_index = 0;
232 }
233 c = tr_buf[in_index++];
234 coded = vector[c];
235 if ((flags & TR_OPT_delete) && invec[c])
236 continue;
237 if ((flags & TR_OPT_squeeze_reps) && last == coded &&
238 (invec[c] || outvec[coded]))
239 continue;
240 output[out_index++] = last = coded;
241 }
242 /* NOTREACHED */
254 return EXIT_SUCCESS; 243 return EXIT_SUCCESS;
255} 244}
diff --git a/debianutils/readlink.c b/debianutils/readlink.c
index b6b49651c..d454cbf19 100644
--- a/debianutils/readlink.c
+++ b/debianutils/readlink.c
@@ -43,7 +43,7 @@ int readlink_main(int argc, char **argv)
43 return EXIT_FAILURE; 43 return EXIT_FAILURE;
44 puts(buf); 44 puts(buf);
45 45
46 if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1) 46 if (ENABLE_FEATURE_CLEAN_UP && !opt)
47 free(buf); 47 free(buf);
48 48
49 fflush_stdout_and_exit(EXIT_SUCCESS); 49 fflush_stdout_and_exit(EXIT_SUCCESS);
diff --git a/editors/ed.c b/editors/ed.c
index 731aef1cb..e6576b406 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -9,9 +9,11 @@
9 9
10#include "libbb.h" 10#include "libbb.h"
11 11
12#define searchString bb_common_bufsiz1
13
12enum { 14enum {
13 USERSIZE = sizeof(bb_common_bufsiz1) > 1024 ? 1024 15 USERSIZE = sizeof(searchString) > 1024 ? 1024
14 : sizeof(bb_common_bufsiz1) - 1, /* max line length typed in by user */ 16 : sizeof(searchString) - 1, /* max line length typed in by user */
15 INITBUF_SIZE = 1024, /* initial buffer size */ 17 INITBUF_SIZE = 1024, /* initial buffer size */
16}; 18};
17 19
@@ -22,8 +24,6 @@ typedef struct LINE {
22 char data[1]; 24 char data[1];
23} LINE; 25} LINE;
24 26
25#define searchString bb_common_bufsiz1
26
27static LINE lines, *curLine; 27static LINE lines, *curLine;
28static int curNum, lastNum, marks[26], dirty; 28static int curNum, lastNum, marks[26], dirty;
29static char *bufBase, *bufPtr, *fileName; 29static char *bufBase, *bufPtr, *fileName;
diff --git a/editors/sed.c b/editors/sed.c
index d49627ff4..d0c2ca742 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -118,8 +118,14 @@ struct globals {
118 int len; /* Space allocated */ 118 int len; /* Space allocated */
119 } pipeline; 119 } pipeline;
120}; 120};
121
122#define G (*(struct globals*)&bb_common_bufsiz1) 121#define G (*(struct globals*)&bb_common_bufsiz1)
122void BUG_sed_globals_too_big(void);
123#define INIT_G() do { \
124 if (sizeof(struct globals) > COMMON_BUFSIZE) \
125 BUG_sed_globals_too_big(); \
126 G.sed_cmd_tail = &G.sed_cmd_head; \
127} while (0)
128
123 129
124#if ENABLE_FEATURE_CLEAN_UP 130#if ENABLE_FEATURE_CLEAN_UP
125static void sed_free_and_close_stuff(void) 131static void sed_free_and_close_stuff(void)
@@ -1210,8 +1216,6 @@ static void add_cmd_block(char *cmdstr)
1210 free(sv); 1216 free(sv);
1211} 1217}
1212 1218
1213void BUG_sed_globals_too_big(void);
1214
1215int sed_main(int argc, char **argv); 1219int sed_main(int argc, char **argv);
1216int sed_main(int argc, char **argv) 1220int sed_main(int argc, char **argv)
1217{ 1221{
@@ -1222,10 +1226,7 @@ int sed_main(int argc, char **argv)
1222 llist_t *opt_e, *opt_f; 1226 llist_t *opt_e, *opt_f;
1223 int status = EXIT_SUCCESS; 1227 int status = EXIT_SUCCESS;
1224 1228
1225 if (sizeof(struct globals) > sizeof(bb_common_bufsiz1)) 1229 INIT_G();
1226 BUG_sed_globals_too_big();
1227
1228 G.sed_cmd_tail = &G.sed_cmd_head;
1229 1230
1230 /* destroy command strings on exit */ 1231 /* destroy command strings on exit */
1231 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff); 1232 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
diff --git a/include/libbb.h b/include/libbb.h
index 385b30f16..013633716 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -954,7 +954,8 @@ extern const int const_int_1;
954#define BUFSIZ 4096 954#define BUFSIZ 4096
955#endif 955#endif
956/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */ 956/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
957extern char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1]; 957enum { COMMON_BUFSIZE = (BUFSIZ >= 256*sizeof(void*) ? BUFSIZ+1 : 256*sizeof(void*)) };
958extern char bb_common_bufsiz1[COMMON_BUFSIZE];
958/* This struct is deliberately not defined. */ 959/* This struct is deliberately not defined. */
959/* See docs/keep_data_small.txt */ 960/* See docs/keep_data_small.txt */
960struct globals; 961struct globals;
diff --git a/libbb/messages.c b/libbb/messages.c
index 3febe7645..fbd882c43 100644
--- a/libbb/messages.c
+++ b/libbb/messages.c
@@ -54,7 +54,7 @@ WTMP_FILE;
54# error unknown path to wtmp file 54# error unknown path to wtmp file
55#endif 55#endif
56 56
57char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1]; 57char bb_common_bufsiz1[COMMON_BUFSIZE];
58 58
59struct globals; 59struct globals;
60/* Make it reside in R/W memory: */ 60/* Make it reside in R/W memory: */
diff --git a/loginutils/adduser.c b/loginutils/adduser.c
index 82a4381ad..916a25b89 100644
--- a/loginutils/adduser.c
+++ b/loginutils/adduser.c
@@ -182,15 +182,14 @@ int adduser_main(int argc, char **argv)
182 /* check for min, max and missing args and exit on error */ 182 /* check for min, max and missing args and exit on error */
183 opt_complementary = "-1:?1:?"; 183 opt_complementary = "-1:?1:?";
184 getopt32(argc, argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup); 184 getopt32(argc, argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup);
185 argv += optind;
185 186
186 /* create string for $HOME if not specified already */ 187 /* create a passwd struct */
188 pw.pw_name = argv[0];
187 if (!pw.pw_dir) { 189 if (!pw.pw_dir) {
188 snprintf(bb_common_bufsiz1, BUFSIZ, "/home/%s", argv[optind]); 190 /* create string for $HOME if not specified already */
189 pw.pw_dir = bb_common_bufsiz1; 191 pw.pw_dir = xasprintf("/home/%s", argv[0]);
190 } 192 }
191
192 /* create a passwd struct */
193 pw.pw_name = argv[optind];
194 pw.pw_passwd = (char *)"x"; 193 pw.pw_passwd = (char *)"x";
195 pw.pw_uid = 0; 194 pw.pw_uid = 0;
196 pw.pw_gid = usegroup ? xgroup2gid(usegroup) : 0; /* exits on failure */ 195 pw.pw_gid = usegroup ? xgroup2gid(usegroup) : 0; /* exits on failure */
diff --git a/miscutils/dc.c b/miscutils/dc.c
index 872b814cd..ced5149df 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -8,7 +8,7 @@
8 8
9/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ 9/* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
10 10
11enum { STACK_SIZE = sizeof(bb_common_bufsiz1) / sizeof(double) }; 11enum { STACK_SIZE = COMMON_BUFSIZE / sizeof(double) };
12 12
13#define stack ((double*)&bb_common_bufsiz1) 13#define stack ((double*)&bb_common_bufsiz1)
14static unsigned int pointer; 14static unsigned int pointer;
diff --git a/modutils/rmmod.c b/modutils/rmmod.c
index 4c792d3ae..f78f96ccc 100644
--- a/modutils/rmmod.c
+++ b/modutils/rmmod.c
@@ -44,6 +44,8 @@ int rmmod_main(int argc, char **argv)
44 int n, ret = EXIT_SUCCESS; 44 int n, ret = EXIT_SUCCESS;
45 unsigned int flags = O_NONBLOCK|O_EXCL; 45 unsigned int flags = O_NONBLOCK|O_EXCL;
46 46
47#define misc_buf bb_common_bufsiz1
48
47 /* Parse command line. */ 49 /* Parse command line. */
48 n = getopt32(argc, argv, "wfa"); 50 n = getopt32(argc, argv, "wfa");
49 if (n & 1) // --wait 51 if (n & 1) // --wait
@@ -65,7 +67,7 @@ int rmmod_main(int argc, char **argv)
65 pnmod = nmod; 67 pnmod = nmod;
66 // the 1 here is QM_MODULES. 68 // the 1 here is QM_MODULES.
67 if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL, 69 if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
68 1, bb_common_bufsiz1, sizeof(bb_common_bufsiz1), 70 1, misc_buf, sizeof(misc_buf),
69 &nmod)) 71 &nmod))
70 { 72 {
71 bb_perror_msg_and_die("QM_MODULES"); 73 bb_perror_msg_and_die("QM_MODULES");
@@ -84,10 +86,10 @@ int rmmod_main(int argc, char **argv)
84 afterslash = strrchr(argv[n], '/'); 86 afterslash = strrchr(argv[n], '/');
85 if (!afterslash) afterslash = argv[n]; 87 if (!afterslash) afterslash = argv[n];
86 else afterslash++; 88 else afterslash++;
87 filename2modname(bb_common_bufsiz1, afterslash); 89 filename2modname(misc_buf, afterslash);
88 } 90 }
89 91
90 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? bb_common_bufsiz1 : argv[n], flags)) { 92 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) {
91 bb_perror_msg("%s", argv[n]); 93 bb_perror_msg("%s", argv[n]);
92 ret = EXIT_FAILURE; 94 ret = EXIT_FAILURE;
93 } 95 }
diff --git a/networking/hostname.c b/networking/hostname.c
index 50ef7b5d1..862bbdfa2 100644
--- a/networking/hostname.c
+++ b/networking/hostname.c
@@ -29,12 +29,13 @@ static void do_sethostname(char *s, int isfile)
29 } 29 }
30 } else { 30 } else {
31 f = xfopen(s, "r"); 31 f = xfopen(s, "r");
32 while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) { 32#define strbuf bb_common_bufsiz1
33 if (bb_common_bufsiz1[0] == '#') { 33 while (fgets(strbuf, sizeof(strbuf), f) != NULL) {
34 if (strbuf[0] == '#') {
34 continue; 35 continue;
35 } 36 }
36 chomp(bb_common_bufsiz1); 37 chomp(strbuf);
37 do_sethostname(bb_common_bufsiz1, 0); 38 do_sethostname(strbuf, 0);
38 } 39 }
39 if (ENABLE_FEATURE_CLEAN_UP) 40 if (ENABLE_FEATURE_CLEAN_UP)
40 fclose(f); 41 fclose(f);
diff --git a/networking/nc.c b/networking/nc.c
index 1fb38f83c..e7bd519e0 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -174,11 +174,10 @@ int nc_main(int argc, char **argv)
174 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0) 174 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
175 bb_perror_msg_and_die("select"); 175 bb_perror_msg_and_die("select");
176 176
177#define iobuf bb_common_bufsiz1
177 for (fd = 0; fd < FD_SETSIZE; fd++) { 178 for (fd = 0; fd < FD_SETSIZE; fd++) {
178 if (FD_ISSET(fd, &testfds)) { 179 if (FD_ISSET(fd, &testfds)) {
179 nread = safe_read(fd, bb_common_bufsiz1, 180 nread = safe_read(fd, iobuf, sizeof(iobuf));
180 sizeof(bb_common_bufsiz1));
181
182 if (fd == cfd) { 181 if (fd == cfd) {
183 if (nread < 1) 182 if (nread < 1)
184 exit(0); 183 exit(0);
@@ -192,8 +191,7 @@ int nc_main(int argc, char **argv)
192 } 191 }
193 ofd = cfd; 192 ofd = cfd;
194 } 193 }
195 194 xwrite(ofd, iobuf, nread);
196 xwrite(ofd, bb_common_bufsiz1, nread);
197 if (delay > 0) sleep(delay); 195 if (delay > 0) sleep(delay);
198 } 196 }
199 } 197 }
diff --git a/networking/telnet.c b/networking/telnet.c
index caca89d2d..a634d7a2b 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -52,7 +52,6 @@ enum {
52 52
53typedef unsigned char byte; 53typedef unsigned char byte;
54 54
55
56struct globals { 55struct globals {
57 int netfd; /* console fd:s are 0 and 1 (and 2) */ 56 int netfd; /* console fd:s are 0 and 1 (and 2) */
58 short iaclen; /* could even use byte */ 57 short iaclen; /* could even use byte */
@@ -78,9 +77,13 @@ struct globals {
78 struct termios termios_def; 77 struct termios termios_def;
79 struct termios termios_raw; 78 struct termios termios_raw;
80}; 79};
81
82#define G (*(struct globals*)&bb_common_bufsiz1) 80#define G (*(struct globals*)&bb_common_bufsiz1)
83 81void BUG_telnet_globals_too_big(void);
82#define INIT_G() do { \
83 if (sizeof(G) > COMMON_BUFSIZE) \
84 BUG_telnet_globals_too_big(); \
85 /* memset(&G, 0, sizeof G); - already is */ \
86} while (0)
84 87
85/* Function prototypes */ 88/* Function prototypes */
86static void rawmode(void); 89static void rawmode(void);
@@ -547,8 +550,6 @@ static void cookmode(void)
547 tcsetattr(0, TCSADRAIN, &G.termios_def); 550 tcsetattr(0, TCSADRAIN, &G.termios_def);
548} 551}
549 552
550void BUG_telnet_globals_too_big(void);
551
552int telnet_main(int argc, char** argv); 553int telnet_main(int argc, char** argv);
553int telnet_main(int argc, char** argv) 554int telnet_main(int argc, char** argv)
554{ 555{
@@ -562,9 +563,7 @@ int telnet_main(int argc, char** argv)
562 int maxfd; 563 int maxfd;
563#endif 564#endif
564 565
565 if (sizeof(G) > sizeof(bb_common_bufsiz1)) 566 INIT_G();
566 BUG_telnet_globals_too_big();
567 /* memset(&G, 0, sizeof G); - already is */
568 567
569#if ENABLE_FEATURE_AUTOWIDTH 568#if ENABLE_FEATURE_AUTOWIDTH
570 get_terminal_width_height(0, &G.win_width, &G.win_height); 569 get_terminal_width_height(0, &G.win_width, &G.win_height);
diff --git a/scripts/find_bad_common_bufsiz b/scripts/find_bad_common_bufsiz
new file mode 100755
index 000000000..e80cf62ef
--- /dev/null
+++ b/scripts/find_bad_common_bufsiz
@@ -0,0 +1,13 @@
1#!/bin/sh
2
3# This script finds applets with multiple uses of bb_common_bufsiz1
4# (== possible bugs).
5# Currently (2007-06-04) reports 3 false positives:
6# ./coreutils/diff.c:7
7# ./loginutils/getty.c:2
8# ./util-linux/mount.c:5
9
10find -name '*.c' \
11| while read name; do
12 grep -Hc bb_common_bufsiz1 "$name"
13done | grep -v ':[01]$'
diff --git a/scripts/find_stray_communal_vars b/scripts/find_stray_communal_vars
index 9f999d705..95563d1a3 100755
--- a/scripts/find_stray_communal_vars
+++ b/scripts/find_stray_communal_vars
@@ -1,6 +1,6 @@
1#!/bin/sh 1#!/bin/sh
2 2
3# Communal variables are elusize, then don't show in size output! 3# Communal variables are elusive, they don't show up in size output!
4# This script will show all communals in *.o, sorted by size 4# This script will show all communals in *.o, sorted by size
5 5
6find -name '*.o' \ 6find -name '*.o' \
diff --git a/sysklogd/logger.c b/sysklogd/logger.c
index 971a6beae..d9c3e5048 100644
--- a/sysklogd/logger.c
+++ b/sysklogd/logger.c
@@ -106,12 +106,13 @@ int logger_main(int argc, char **argv)
106 argc -= optind; 106 argc -= optind;
107 argv += optind; 107 argv += optind;
108 if (!argc) { 108 if (!argc) {
109 while (fgets(bb_common_bufsiz1, BUFSIZ, stdin)) { 109#define strbuf bb_common_bufsiz1
110 if (bb_common_bufsiz1[0] 110 while (fgets(strbuf, BUFSIZ, stdin)) {
111 && NOT_LONE_CHAR(bb_common_bufsiz1, '\n') 111 if (strbuf[0]
112 && NOT_LONE_CHAR(strbuf, '\n')
112 ) { 113 ) {
113 /* Neither "" nor "\n" */ 114 /* Neither "" nor "\n" */
114 syslog(i, "%s", bb_common_bufsiz1); 115 syslog(i, "%s", strbuf);
115 } 116 }
116 } 117 }
117 } else { 118 } else {