diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-09-07 02:38:26 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-09-07 02:38:26 +0200 |
commit | 0c6914e50c913b54632b7279dbe3544ca7e633d0 (patch) | |
tree | 347e5580023c368fc16dd21ff6539a609c963b3b /modutils/modprobe-small.c | |
parent | f9c814b0eedf757f13d4796c254329d84a4ae85c (diff) | |
download | busybox-w32-0c6914e50c913b54632b7279dbe3544ca7e633d0.tar.gz busybox-w32-0c6914e50c913b54632b7279dbe3544ca7e633d0.tar.bz2 busybox-w32-0c6914e50c913b54632b7279dbe3544ca7e633d0.zip |
modprobe-small: make string buffer code robust; fix help text
together with previous one-liner:
function old new delta
append 70 99 +29
parse_module 295 311 +16
copy_stringbuf 35 36 +1
packed_usage 26545 26540 -5
appendc 27 - -27
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 3/1 up/down: 46/-32) Total: 14 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'modutils/modprobe-small.c')
-rw-r--r-- | modutils/modprobe-small.c | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c index bc80723fa..e2359d042 100644 --- a/modutils/modprobe-small.c +++ b/modutils/modprobe-small.c | |||
@@ -44,11 +44,13 @@ struct globals { | |||
44 | char *module_load_options; | 44 | char *module_load_options; |
45 | smallint dep_bb_seen; | 45 | smallint dep_bb_seen; |
46 | smallint wrote_dep_bb_ok; | 46 | smallint wrote_dep_bb_ok; |
47 | int module_count; | 47 | unsigned module_count; |
48 | int module_found_idx; | 48 | int module_found_idx; |
49 | int stringbuf_idx; | 49 | unsigned stringbuf_idx; |
50 | char stringbuf[32 * 1024]; /* some modules have lots of stuff */ | 50 | unsigned stringbuf_size; |
51 | char *stringbuf; /* some modules have lots of stuff */ | ||
51 | /* for example, drivers/media/video/saa7134/saa7134.ko */ | 52 | /* for example, drivers/media/video/saa7134/saa7134.ko */ |
53 | /* therefore having a fixed biggish buffer is not wise */ | ||
52 | }; | 54 | }; |
53 | #define G (*ptr_to_globals) | 55 | #define G (*ptr_to_globals) |
54 | #define modinfo (G.modinfo ) | 56 | #define modinfo (G.modinfo ) |
@@ -58,16 +60,29 @@ struct globals { | |||
58 | #define module_found_idx (G.module_found_idx ) | 60 | #define module_found_idx (G.module_found_idx ) |
59 | #define module_load_options (G.module_load_options) | 61 | #define module_load_options (G.module_load_options) |
60 | #define stringbuf_idx (G.stringbuf_idx ) | 62 | #define stringbuf_idx (G.stringbuf_idx ) |
63 | #define stringbuf_size (G.stringbuf_size ) | ||
61 | #define stringbuf (G.stringbuf ) | 64 | #define stringbuf (G.stringbuf ) |
62 | #define INIT_G() do { \ | 65 | #define INIT_G() do { \ |
63 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ | 66 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
64 | } while (0) | 67 | } while (0) |
65 | 68 | ||
69 | static void append(const char *s) | ||
70 | { | ||
71 | unsigned len = strlen(s); | ||
72 | if (stringbuf_idx + len + 15 > stringbuf_size) { | ||
73 | stringbuf_size = stringbuf_idx + len + 127; | ||
74 | dbg2_error_msg("grow stringbuf to %u", stringbuf_size); | ||
75 | stringbuf = xrealloc(stringbuf, stringbuf_size); | ||
76 | } | ||
77 | memcpy(stringbuf + stringbuf_idx, s, len); | ||
78 | stringbuf_idx += len; | ||
79 | } | ||
66 | 80 | ||
67 | static void appendc(char c) | 81 | static void appendc(char c) |
68 | { | 82 | { |
69 | if (stringbuf_idx < sizeof(stringbuf)) | 83 | /* We appendc() only after append(), + 15 trick in append() |
70 | stringbuf[stringbuf_idx++] = c; | 84 | * makes it unnecessary to check for overflow here */ |
85 | stringbuf[stringbuf_idx++] = c; | ||
71 | } | 86 | } |
72 | 87 | ||
73 | static void bksp(void) | 88 | static void bksp(void) |
@@ -76,15 +91,6 @@ static void bksp(void) | |||
76 | stringbuf_idx--; | 91 | stringbuf_idx--; |
77 | } | 92 | } |
78 | 93 | ||
79 | static void append(const char *s) | ||
80 | { | ||
81 | size_t len = strlen(s); | ||
82 | if (stringbuf_idx + len < sizeof(stringbuf)) { | ||
83 | memcpy(stringbuf + stringbuf_idx, s, len); | ||
84 | stringbuf_idx += len; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | static void reset_stringbuf(void) | 94 | static void reset_stringbuf(void) |
89 | { | 95 | { |
90 | stringbuf_idx = 0; | 96 | stringbuf_idx = 0; |
@@ -92,7 +98,7 @@ static void reset_stringbuf(void) | |||
92 | 98 | ||
93 | static char* copy_stringbuf(void) | 99 | static char* copy_stringbuf(void) |
94 | { | 100 | { |
95 | char *copy = xmalloc(stringbuf_idx); | 101 | char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */ |
96 | return memcpy(copy, stringbuf, stringbuf_idx); | 102 | return memcpy(copy, stringbuf, stringbuf_idx); |
97 | } | 103 | } |
98 | 104 | ||
@@ -216,7 +222,6 @@ static void parse_module(module_info *info, const char *pathname) | |||
216 | pos = (ptr - module_image); | 222 | pos = (ptr - module_image); |
217 | } | 223 | } |
218 | bksp(); /* remove last ' ' */ | 224 | bksp(); /* remove last ' ' */ |
219 | appendc('\0'); | ||
220 | info->aliases = copy_stringbuf(); | 225 | info->aliases = copy_stringbuf(); |
221 | replace(info->aliases, '-', '_'); | 226 | replace(info->aliases, '-', '_'); |
222 | 227 | ||
@@ -229,7 +234,6 @@ static void parse_module(module_info *info, const char *pathname) | |||
229 | dbg2_error_msg("dep:'%s'", ptr); | 234 | dbg2_error_msg("dep:'%s'", ptr); |
230 | append(ptr); | 235 | append(ptr); |
231 | } | 236 | } |
232 | appendc('\0'); | ||
233 | info->deps = copy_stringbuf(); | 237 | info->deps = copy_stringbuf(); |
234 | 238 | ||
235 | free(module_image); | 239 | free(module_image); |