aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-08 05:14:36 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-08 05:14:36 +0000
commitdeeed59de0a9bcc068ebd14d7496a6b26e45b890 (patch)
tree7dafd469e9f6bca107adbf930fe77fff9958a0b6
parent493829207c1c2a36d55aaa13abf806533d0cb87f (diff)
downloadbusybox-w32-deeed59de0a9bcc068ebd14d7496a6b26e45b890.tar.gz
busybox-w32-deeed59de0a9bcc068ebd14d7496a6b26e45b890.tar.bz2
busybox-w32-deeed59de0a9bcc068ebd14d7496a6b26e45b890.zip
libbb: introduce and use xrealloc_vector
function old new delta xrealloc_vector_helper - 51 +51 create_list 84 99 +15 getopt_main 690 695 +5 passwd_main 1049 1053 +4 get_cached 85 89 +4 msh_main 1377 1380 +3 add_match 42 41 -1 read_lines 720 718 -2 grave 1068 1066 -2 fill_match_lines 143 141 -2 add_to_dirlist 67 65 -2 add_input_file 49 47 -2 act 252 250 -2 fsck_main 2252 2246 -6 man_main 765 757 -8 bb_internal_initgroups 228 220 -8 cut_main 1052 1041 -11 add_edge_to_node 55 43 -12 dpkg_main 3851 3835 -16 ifupdown_main 2202 2178 -24 sort_main 838 812 -26 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 5/15 up/down: 82/-124) Total: -42 bytes
-rw-r--r--archival/dpkg.c14
-rw-r--r--archival/rpm.c4
-rw-r--r--coreutils/cut.c7
-rw-r--r--coreutils/od_bloaty.c4
-rw-r--r--coreutils/sort.c3
-rw-r--r--debianutils/run_parts.c2
-rw-r--r--e2fsprogs/fsck.c6
-rw-r--r--editors/awk.c6
-rw-r--r--editors/diff.c4
-rw-r--r--editors/sed.c3
-rw-r--r--include/libbb.h12
-rw-r--r--libbb/Kbuild1
-rw-r--r--libbb/dump.c2
-rw-r--r--libbb/get_line_from_file.c2
-rw-r--r--libbb/lineedit.c7
-rw-r--r--libbb/mtab.c14
-rw-r--r--libbb/procps.c8
-rw-r--r--libbb/read.c49
-rw-r--r--libpwdgrp/pwd_grp.c6
-rw-r--r--miscutils/less.c6
-rw-r--r--miscutils/man.c6
-rw-r--r--modutils/insmod.c10
-rw-r--r--modutils/modprobe-small.c4
-rw-r--r--modutils/modprobe.c6
-rw-r--r--networking/ifupdown.c6
-rw-r--r--procps/ps.c5
-rw-r--r--procps/top.c5
-rw-r--r--util-linux/getopt.c4
-rw-r--r--util-linux/mount.c2
29 files changed, 123 insertions, 85 deletions
diff --git a/archival/dpkg.c b/archival/dpkg.c
index 637afceef..28913d2e3 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -382,9 +382,8 @@ static int search_for_provides(int needle, int start_at)
382 */ 382 */
383static void add_edge_to_node(common_node_t *node, edge_t *edge) 383static void add_edge_to_node(common_node_t *node, edge_t *edge)
384{ 384{
385 node->num_of_edges++; 385 node->edge = xrealloc_vector(node->edge, 2, node->num_of_edges);
386 node->edge = xrealloc(node->edge, sizeof(edge_t) * (node->num_of_edges + 1)); 386 node->edge[node->num_of_edges++] = edge;
387 node->edge[node->num_of_edges - 1] = edge;
388} 387}
389 388
390/* 389/*
@@ -972,7 +971,7 @@ static int check_deps(deb_file_t **deb_file, int deb_start /*, int dep_max_count
972 * installed package for conflicts*/ 971 * installed package for conflicts*/
973 while (deb_file[i] != NULL) { 972 while (deb_file[i] != NULL) {
974 const unsigned package_num = deb_file[i]->package; 973 const unsigned package_num = deb_file[i]->package;
975 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); 974 conflicts = xrealloc_vector(conflicts, 2, conflicts_num);
976 conflicts[conflicts_num] = package_num; 975 conflicts[conflicts_num] = package_num;
977 conflicts_num++; 976 conflicts_num++;
978 /* add provides to conflicts list */ 977 /* add provides to conflicts list */
@@ -989,7 +988,7 @@ static int check_deps(deb_file_t **deb_file, int deb_start /*, int dep_max_count
989 new_node->version = package_hashtable[package_num]->edge[j]->version; 988 new_node->version = package_hashtable[package_num]->edge[j]->version;
990 package_hashtable[conflicts_package_num] = new_node; 989 package_hashtable[conflicts_package_num] = new_node;
991 } 990 }
992 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); 991 conflicts = xrealloc_vector(conflicts, 2, conflicts_num);
993 conflicts[conflicts_num] = conflicts_package_num; 992 conflicts[conflicts_num] = conflicts_package_num;
994 conflicts_num++; 993 conflicts_num++;
995 } 994 }
@@ -1170,7 +1169,8 @@ static char **create_list(const char *filename)
1170 file_list = NULL; 1169 file_list = NULL;
1171 count = 0; 1170 count = 0;
1172 while ((line = xmalloc_fgetline(list_stream)) != NULL) { 1171 while ((line = xmalloc_fgetline(list_stream)) != NULL) {
1173 file_list = xrealloc(file_list, sizeof(char *) * (count + 2)); 1172//TODO: zeroing xrealloc_vector?
1173 file_list = xrealloc_vector(file_list, 2, count);
1174 file_list[count++] = line; 1174 file_list[count++] = line;
1175 file_list[count] = NULL; 1175 file_list[count] = NULL;
1176 } 1176 }
@@ -1634,7 +1634,7 @@ int dpkg_main(int argc UNUSED_PARAM, char **argv)
1634 /* Read arguments and store relevant info in structs */ 1634 /* Read arguments and store relevant info in structs */
1635 while (*argv) { 1635 while (*argv) {
1636 /* deb_count = nb_elem - 1 and we need nb_elem + 1 to allocate terminal node [NULL pointer] */ 1636 /* deb_count = nb_elem - 1 and we need nb_elem + 1 to allocate terminal node [NULL pointer] */
1637 deb_file = xrealloc(deb_file, sizeof(deb_file[0]) * (deb_count + 2)); 1637 deb_file = xrealloc_vector(deb_file, 2, deb_count);
1638 deb_file[deb_count] = xzalloc(sizeof(deb_file[0][0])); 1638 deb_file[deb_count] = xzalloc(sizeof(deb_file[0][0]));
1639 if (opt & (OPT_install | OPT_unpack)) { 1639 if (opt & (OPT_install | OPT_unpack)) {
1640 /* -i/-u: require filename */ 1640 /* -i/-u: require filename */
diff --git a/archival/rpm.c b/archival/rpm.c
index c4bcd605d..b3d7cd5f7 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -279,12 +279,12 @@ static rpm_index **rpm_gettags(int fd, int *num_tags)
279 tmpindex->type = ntohl(tmpindex->type); 279 tmpindex->type = ntohl(tmpindex->type);
280 tmpindex->count = ntohl(tmpindex->count); 280 tmpindex->count = ntohl(tmpindex->count);
281 tmpindex->offset = storepos + ntohl(tmpindex->offset); 281 tmpindex->offset = storepos + ntohl(tmpindex->offset);
282 if (pass==0) 282 if (pass == 0)
283 tmpindex->tag -= 743; 283 tmpindex->tag -= 743;
284 } 284 }
285 xlseek(fd, header.size, SEEK_CUR); /* Seek past store */ 285 xlseek(fd, header.size, SEEK_CUR); /* Seek past store */
286 /* Skip padding to 8 byte boundary after reading signature headers */ 286 /* Skip padding to 8 byte boundary after reading signature headers */
287 if (pass==0) 287 if (pass == 0)
288 xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); 288 xlseek(fd, (8 - (xlseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR);
289 } 289 }
290 tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */ 290 tags = xrealloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 179854988..3bac151b2 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -241,9 +241,10 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
241 } 241 }
242 242
243 /* add the new list */ 243 /* add the new list */
244 cut_lists = xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists)); 244 cut_lists = xrealloc_vector(cut_lists, 4, nlists);
245 cut_lists[nlists-1].startpos = s; 245 cut_lists[nlists].startpos = s;
246 cut_lists[nlists-1].endpos = e; 246 cut_lists[nlists].endpos = e;
247 nlists++;
247 } 248 }
248 249
249 /* make sure we got some cut positions out of all that */ 250 /* make sure we got some cut positions out of all that */
diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c
index a2eaf11d9..eb4579857 100644
--- a/coreutils/od_bloaty.c
+++ b/coreutils/od_bloaty.c
@@ -735,9 +735,9 @@ decode_format_string(const char *s)
735 735
736 assert(s != next); 736 assert(s != next);
737 s = next; 737 s = next;
738 spec = xrealloc_vector(spec, 4, n_specs);
739 memcpy(&spec[n_specs], &tspec, sizeof(spec[0]));
738 n_specs++; 740 n_specs++;
739 spec = xrealloc(spec, n_specs * sizeof(*spec));
740 memcpy(&spec[n_specs-1], &tspec, sizeof *spec);
741 } 741 }
742} 742}
743 743
diff --git a/coreutils/sort.c b/coreutils/sort.c
index 1fa552725..ac8fc9b2e 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -360,8 +360,7 @@ int sort_main(int argc UNUSED_PARAM, char **argv)
360 for (;;) { 360 for (;;) {
361 line = GET_LINE(fp); 361 line = GET_LINE(fp);
362 if (!line) break; 362 if (!line) break;
363 if (!(linecount & 63)) 363 lines = xrealloc_vector(lines, 6, linecount);
364 lines = xrealloc(lines, sizeof(char *) * (linecount + 64));
365 lines[linecount++] = line; 364 lines[linecount++] = line;
366 } 365 }
367 fclose_if_not_stdin(fp); 366 fclose_if_not_stdin(fp);
diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c
index 953ff6732..cb5f48fdd 100644
--- a/debianutils/run_parts.c
+++ b/debianutils/run_parts.c
@@ -90,7 +90,7 @@ static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUS
90 return SKIP; 90 return SKIP;
91 } 91 }
92 92
93 names = xrealloc(names, (cur + 2) * sizeof(names[0])); 93 names = xrealloc_vector(names, 4, cur);
94 names[cur++] = xstrdup(file); 94 names[cur++] = xstrdup(file);
95 names[cur] = NULL; 95 names[cur] = NULL;
96 96
diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c
index c17693699..86c78d881 100644
--- a/e2fsprogs/fsck.c
+++ b/e2fsprogs/fsck.c
@@ -1028,13 +1028,13 @@ static void parse_args(char **argv)
1028// FIXME: must check that arg is a blkdev, or resolve 1028// FIXME: must check that arg is a blkdev, or resolve
1029// "/path", "UUID=xxx" or "LABEL=xxx" into block device name 1029// "/path", "UUID=xxx" or "LABEL=xxx" into block device name
1030// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties) 1030// ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
1031 devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0])); 1031 devices = xrealloc_vector(devices, 2, num_devices);
1032 devices[num_devices++] = xstrdup(arg); 1032 devices[num_devices++] = xstrdup(arg);
1033 continue; 1033 continue;
1034 } 1034 }
1035 1035
1036 if (arg[0] != '-' || opts_for_fsck) { 1036 if (arg[0] != '-' || opts_for_fsck) {
1037 args = xrealloc(args, (num_args+1) * sizeof(args[0])); 1037 args = xrealloc_vector(args, 2, num_args);
1038 args[num_args++] = xstrdup(arg); 1038 args[num_args++] = xstrdup(arg);
1039 continue; 1039 continue;
1040 } 1040 }
@@ -1111,7 +1111,7 @@ static void parse_args(char **argv)
1111 if (optpos) { 1111 if (optpos) {
1112 options[0] = '-'; 1112 options[0] = '-';
1113 options[optpos + 1] = '\0'; 1113 options[optpos + 1] = '\0';
1114 args = xrealloc(args, (num_args+1) * sizeof(args[0])); 1114 args = xrealloc_vector(args, 2, num_args);
1115 args[num_args++] = options; 1115 args[num_args++] = options;
1116 } 1116 }
1117 } 1117 }
diff --git a/editors/awk.c b/editors/awk.c
index 2af39880e..7af9e1eeb 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -1473,8 +1473,10 @@ static regex_t *as_regex(node *op, regex_t *preg)
1473/* gradually increasing buffer */ 1473/* gradually increasing buffer */
1474static void qrealloc(char **b, int n, int *size) 1474static void qrealloc(char **b, int n, int *size)
1475{ 1475{
1476 if (!*b || n >= *size) 1476 if (!*b || n >= *size) {
1477 *b = xrealloc(*b, *size = n + (n>>1) + 80); 1477 *size = n + (n>>1) + 80;
1478 *b = xrealloc(*b, *size);
1479 }
1478} 1480}
1479 1481
1480/* resize field storage space */ 1482/* resize field storage space */
diff --git a/editors/diff.c b/editors/diff.c
index 570c4c490..64ad6511d 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -1059,6 +1059,7 @@ static unsigned diffreg(char *file1, char *file2, int flags)
1059 1059
1060 member = (int *) nfile[1]; 1060 member = (int *) nfile[1];
1061 equiv(sfile[0], slen[0], sfile[1], slen[1], member); 1061 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1062//TODO: xrealloc_vector?
1062 member = xrealloc(member, (slen[1] + 2) * sizeof(int)); 1063 member = xrealloc(member, (slen[1] + 2) * sizeof(int));
1063 1064
1064 class = (int *) nfile[0]; 1065 class = (int *) nfile[0];
@@ -1168,8 +1169,7 @@ static int FAST_FUNC add_to_dirlist(const char *filename,
1168 void *userdata, 1169 void *userdata,
1169 int depth UNUSED_PARAM) 1170 int depth UNUSED_PARAM)
1170{ 1171{
1171 /* +2: with space for eventual trailing NULL */ 1172 dl = xrealloc_vector(dl, 5, dl_count);
1172 dl = xrealloc(dl, (dl_count+2) * sizeof(dl[0]));
1173 dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata); 1173 dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata);
1174 dl_count++; 1174 dl_count++;
1175 return TRUE; 1175 return TRUE;
diff --git a/editors/sed.c b/editors/sed.c
index 88bae785c..67e88418a 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -732,8 +732,7 @@ static void flush_append(void)
732 732
733static void add_input_file(FILE *file) 733static void add_input_file(FILE *file)
734{ 734{
735 G.input_file_list = xrealloc(G.input_file_list, 735 G.input_file_list = xrealloc_vector(G.input_file_list, 2, G.input_file_count);
736 (G.input_file_count + 1) * sizeof(FILE *));
737 G.input_file_list[G.input_file_count++] = file; 736 G.input_file_list[G.input_file_count++] = file;
738} 737}
739 738
diff --git a/include/libbb.h b/include/libbb.h
index 378cb4401..fc65d52ff 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -564,10 +564,14 @@ void fputc_printable(int ch, FILE *file) FAST_FUNC;
564 564
565/* dmalloc will redefine these to it's own implementation. It is safe 565/* dmalloc will redefine these to it's own implementation. It is safe
566 * to have the prototypes here unconditionally. */ 566 * to have the prototypes here unconditionally. */
567extern void *malloc_or_warn(size_t size) FAST_FUNC; 567void *malloc_or_warn(size_t size) FAST_FUNC;
568extern void *xmalloc(size_t size) FAST_FUNC; 568void *xmalloc(size_t size) FAST_FUNC;
569extern void *xzalloc(size_t size) FAST_FUNC; 569void *xzalloc(size_t size) FAST_FUNC;
570extern void *xrealloc(void *old, size_t size) FAST_FUNC; 570void *xrealloc(void *old, size_t size) FAST_FUNC;
571#define xrealloc_vector(vector, shift, idx) \
572 xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx))
573void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC;
574
571 575
572extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC; 576extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC;
573extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC; 577extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC;
diff --git a/libbb/Kbuild b/libbb/Kbuild
index 5cbecd537..c49297b12 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -109,6 +109,7 @@ lib-y += xfunc_die.o
109lib-y += xgetcwd.o 109lib-y += xgetcwd.o
110lib-y += xgethostbyname.o 110lib-y += xgethostbyname.o
111lib-y += xreadlink.o 111lib-y += xreadlink.o
112lib-y += xrealloc_vector.o
112 113
113# conditionally compiled objects: 114# conditionally compiled objects:
114lib-$(CONFIG_FEATURE_MOUNT_LOOP) += loop.o 115lib-$(CONFIG_FEATURE_MOUNT_LOOP) += loop.o
diff --git a/libbb/dump.c b/libbb/dump.c
index c45595285..8a90aac5a 100644
--- a/libbb/dump.c
+++ b/libbb/dump.c
@@ -245,7 +245,7 @@ static void rewrite(FS * fs)
245 { 245 {
246 savech = *p3; 246 savech = *p3;
247 *p3 = '\0'; 247 *p3 = '\0';
248 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt)+(p3-p2)+1); 248 pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt) + (p3-p2) + 1);
249 strcat(pr->fmt, p2); 249 strcat(pr->fmt, p2);
250 *p3 = savech; 250 *p3 = savech;
251 p2 = p3; 251 p2 = p3;
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 66ea5a1a5..3a76f49a0 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -42,7 +42,7 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
42 // free(linebuf); 42 // free(linebuf);
43 // return NULL; 43 // return NULL;
44 //} 44 //}
45 linebuf = xrealloc(linebuf, idx+1); 45 linebuf = xrealloc(linebuf, idx + 1);
46 linebuf[idx] = '\0'; 46 linebuf[idx] = '\0';
47 } 47 }
48 return linebuf; 48 return linebuf;
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 3ef47ba71..a46b5d2c4 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -400,11 +400,8 @@ static void free_tab_completion_data(void)
400 400
401static void add_match(char *matched) 401static void add_match(char *matched)
402{ 402{
403 int nm = num_matches; 403 matches = xrealloc_vector(matches, 4, num_matches);
404 int nm1 = nm + 1; 404 matches[num_matches] = matched;
405
406 matches = xrealloc(matches, nm1 * sizeof(char *));
407 matches[nm] = matched;
408 num_matches++; 405 num_matches++;
409} 406}
410 407
diff --git a/libbb/mtab.c b/libbb/mtab.c
index 57654a695..2c171322a 100644
--- a/libbb/mtab.c
+++ b/libbb/mtab.c
@@ -27,14 +27,14 @@ void FAST_FUNC erase_mtab(const char *name)
27 } 27 }
28 28
29 while ((m = getmntent(mountTable)) != 0) { 29 while ((m = getmntent(mountTable)) != 0) {
30 entries = xrealloc(entries, 3, count);
31 entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
32 entries[count].mnt_dir = xstrdup(m->mnt_dir);
33 entries[count].mnt_type = xstrdup(m->mnt_type);
34 entries[count].mnt_opts = xstrdup(m->mnt_opts);
35 entries[count].mnt_freq = m->mnt_freq;
36 entries[count].mnt_passno = m->mnt_passno;
30 i = count++; 37 i = count++;
31 entries = xrealloc(entries, count * sizeof(entries[0]));
32 entries[i].mnt_fsname = xstrdup(m->mnt_fsname);
33 entries[i].mnt_dir = xstrdup(m->mnt_dir);
34 entries[i].mnt_type = xstrdup(m->mnt_type);
35 entries[i].mnt_opts = xstrdup(m->mnt_opts);
36 entries[i].mnt_freq = m->mnt_freq;
37 entries[i].mnt_passno = m->mnt_passno;
38 } 38 }
39 endmntent(mountTable); 39 endmntent(mountTable);
40 40
diff --git a/libbb/procps.c b/libbb/procps.c
index 7d49d83ce..a5168a077 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -45,8 +45,8 @@ static int get_cached(cache_t *cp, unsigned id)
45 for (i = 0; i < cp->size; i++) 45 for (i = 0; i < cp->size; i++)
46 if (cp->cache[i].id == id) 46 if (cp->cache[i].id == id)
47 return i; 47 return i;
48 i = cp->size++; 48 i = cp->size;
49 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); 49 cp->cache = xrealloc_vector(cp->cache, 2, cp->size++);
50 cp->cache[i++].id = id; 50 cp->cache[i++].id = id;
51 return -i; 51 return -i;
52} 52}
@@ -59,8 +59,8 @@ static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
59 for (i = 0; i < cp->size; i++) 59 for (i = 0; i < cp->size; i++)
60 if (cp->cache[i].id == id) 60 if (cp->cache[i].id == id)
61 return cp->cache[i].name; 61 return cp->cache[i].name;
62 i = cp->size++; 62 i = cp->size;
63 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); 63 cp->cache = xrealloc_vector(cp->cache, 2, cp->size++);
64 cp->cache[i].id = id; 64 cp->cache[i].id = id;
65 /* Never fails. Generates numeric string if name isn't found */ 65 /* Never fails. Generates numeric string if name isn't found */
66 fp(cp->cache[i].name, sizeof(cp->cache[i].name), id); 66 fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
diff --git a/libbb/read.c b/libbb/read.c
index 7b804125a..405e216dc 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -291,3 +291,52 @@ void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep)
291 bb_perror_msg_and_die("can't read '%s'", filename); 291 bb_perror_msg_and_die("can't read '%s'", filename);
292 return buf; 292 return buf;
293} 293}
294
295/* libbb candidate */
296#if 0
297static void *xmalloc_read(int fd, size_t *sizep)
298{
299 char *buf;
300 size_t size, rd_size, total;
301 off_t to_read;
302 struct stat st;
303
304 to_read = sizep ? *sizep : INT_MAX; /* max to read */
305
306 /* Estimate file size */
307 st.st_size = 0; /* in case fstat fails, assume 0 */
308 fstat(fd, &st);
309 /* /proc/N/stat files report st_size 0 */
310 /* In order to make such files readable, we add small const */
311 size = (st.st_size | 0x3ff) + 1;
312
313 total = 0;
314 buf = NULL;
315 while (1) {
316 if (to_read < size)
317 size = to_read;
318 buf = xrealloc(buf, total + size + 1);
319 rd_size = full_read(fd, buf + total, size);
320 if ((ssize_t)rd_size < 0) { /* error */
321 free(buf);
322 return NULL;
323 }
324 total += rd_size;
325 if (rd_size < size) /* EOF */
326 break;
327 to_read -= rd_size;
328 if (to_read <= 0)
329 break;
330 /* grow by 1/8, but in [1k..64k] bounds */
331 size = ((total / 8) | 0x3ff) + 1;
332 if (size > 64*1024)
333 size = 64*1024;
334 }
335 xrealloc(buf, total + 1);
336 buf[total] = '\0';
337
338 if (sizep)
339 *sizep = total;
340 return buf;
341}
342#endif
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c
index 3fe70f40c..867caf096 100644
--- a/libpwdgrp/pwd_grp.c
+++ b/libpwdgrp/pwd_grp.c
@@ -643,11 +643,7 @@ int initgroups(const char *user, gid_t gid)
643 if (group.gr_gid != gid) { 643 if (group.gr_gid != gid) {
644 for (m = group.gr_mem; *m; m++) { 644 for (m = group.gr_mem; *m; m++) {
645 if (!strcmp(*m, user)) { 645 if (!strcmp(*m, user)) {
646 if (!(num_groups & 7)) { 646 group_list = xrealloc_vector(group_list, 3, num_groups);
647 gid_t *tmp = xrealloc(group_list,
648 (num_groups+8) * sizeof(gid_t *));
649 group_list = tmp;
650 }
651 group_list[num_groups++] = group.gr_gid; 647 group_list[num_groups++] = group.gr_gid;
652 break; 648 break;
653 } 649 }
diff --git a/miscutils/less.c b/miscutils/less.c
index ecdb9ae60..1e22d333d 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -322,7 +322,7 @@ static void read_lines(void)
322 } 322 }
323 reached_eof: 323 reached_eof:
324 last_terminated = terminated; 324 last_terminated = terminated;
325 flines = xrealloc(flines, (max_fline+1) * sizeof(char *)); 325 flines = xrealloc_vector(flines, 8, max_fline);
326 if (option_mask32 & FLAG_N) { 326 if (option_mask32 & FLAG_N) {
327 /* Width of 7 preserves tab spacing in the text */ 327 /* Width of 7 preserves tab spacing in the text */
328 flines[max_fline] = xasprintf( 328 flines[max_fline] = xasprintf(
@@ -332,7 +332,7 @@ static void read_lines(void)
332 if (terminated) 332 if (terminated)
333 max_lineno++; 333 max_lineno++;
334 } else { 334 } else {
335 flines[max_fline] = xrealloc(current_line, strlen(current_line)+1); 335 flines[max_fline] = xrealloc(current_line, strlen(current_line) + 1);
336 } 336 }
337 if (max_fline >= MAXLINES) { 337 if (max_fline >= MAXLINES) {
338 eof_error = 0; /* Pretend we saw EOF */ 338 eof_error = 0; /* Pretend we saw EOF */
@@ -933,7 +933,7 @@ static void fill_match_lines(unsigned pos)
933 /* and we didn't match it last time */ 933 /* and we didn't match it last time */
934 && !(num_matches && match_lines[num_matches-1] == pos) 934 && !(num_matches && match_lines[num_matches-1] == pos)
935 ) { 935 ) {
936 match_lines = xrealloc(match_lines, (num_matches+1) * sizeof(int)); 936 match_lines = xrealloc_vector(match_lines, 4, num_matches);
937 match_lines[num_matches++] = pos; 937 match_lines[num_matches++] = pos;
938 } 938 }
939 pos++; 939 pos++;
diff --git a/miscutils/man.c b/miscutils/man.c
index 3685be7b6..b1bb1530a 100644
--- a/miscutils/man.c
+++ b/miscutils/man.c
@@ -117,11 +117,7 @@ int man_main(int argc UNUSED_PARAM, char **argv)
117 count_mp++; 117 count_mp++;
118 /* man_path_list is NULL terminated */ 118 /* man_path_list is NULL terminated */
119 man_path_list[count_mp] = NULL; 119 man_path_list[count_mp] = NULL;
120 if (!(count_mp & 0xf)) { /* 0x10, 0x20 etc */ 120 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
121 /* so that last valid man_path_list[] is [count_mp + 0x10] */
122 man_path_list = xrealloc(man_path_list,
123 (count_mp + 0x11) * sizeof(man_path_list[0]));
124 }
125 } 121 }
126 if (strcmp("MANSECT", line) == 0) { 122 if (strcmp("MANSECT", line) == 0) {
127 free(sec_list); 123 free(sec_list);
diff --git a/modutils/insmod.c b/modutils/insmod.c
index d928be27d..df6bf10a0 100644
--- a/modutils/insmod.c
+++ b/modutils/insmod.c
@@ -2201,7 +2201,7 @@ static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2201 int newidx = f->header.e_shnum++; 2201 int newidx = f->header.e_shnum++;
2202 struct obj_section *sec; 2202 struct obj_section *sec;
2203 2203
2204 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec)); 2204 f->sections = xrealloc_vector(f->sections, 2, newidx);
2205 f->sections[newidx] = sec = arch_new_section(); 2205 f->sections[newidx] = sec = arch_new_section();
2206 2206
2207 sec->header.sh_type = SHT_PROGBITS; 2207 sec->header.sh_type = SHT_PROGBITS;
@@ -2250,7 +2250,8 @@ static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2250{ 2250{
2251 unsigned long oldsize = sec->header.sh_size; 2251 unsigned long oldsize = sec->header.sh_size;
2252 if (more) { 2252 if (more) {
2253 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more); 2253 sec->header.sh_size += more;
2254 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2254 } 2255 }
2255 return sec->contents + oldsize; 2256 return sec->contents + oldsize;
2256} 2257}
@@ -2736,7 +2737,8 @@ static void new_get_kernel_symbols(void)
2736 retry_kern_sym_load: 2737 retry_kern_sym_load:
2737 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) { 2738 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2738 if (errno == ENOSPC && bufsize < ret) { 2739 if (errno == ENOSPC && bufsize < ret) {
2739 syms = xrealloc(syms, bufsize = ret); 2740 bufsize = ret;
2741 syms = xrealloc(syms, bufsize);
2740 goto retry_kern_sym_load; 2742 goto retry_kern_sym_load;
2741 } 2743 }
2742 bb_perror_msg_and_die("kernel: QM_SYMBOLS"); 2744 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
@@ -3080,7 +3082,7 @@ static void obj_allocate_commons(struct obj_file *f)
3080 if (i == f->header.e_shnum) { 3082 if (i == f->header.e_shnum) {
3081 struct obj_section *sec; 3083 struct obj_section *sec;
3082 3084
3083 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec)); 3085 f->sections = xrealloc(f->sections, 2, i);
3084 f->sections[i] = sec = arch_new_section(); 3086 f->sections[i] = sec = arch_new_section();
3085 f->header.e_shnum = i + 1; 3087 f->header.e_shnum = i + 1;
3086 3088
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index e82ee4cef..4f073536a 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -317,9 +317,7 @@ static FAST_FUNC int fileAction(const char *pathname,
317 } 317 }
318 318
319 cur = module_count++; 319 cur = module_count++;
320 if (!(cur & 0xfff)) { 320 modinfo = xrealloc_vector(modinfo, 12, cur);
321 modinfo = xrealloc(modinfo, sizeof(modinfo[0]) * (cur + 0x1001));
322 }
323 modinfo[cur].pathname = xstrdup(pathname); 321 modinfo[cur].pathname = xstrdup(pathname);
324 modinfo[cur].desc = NULL; 322 modinfo[cur].desc = NULL;
325 modinfo[cur+1].pathname = NULL; 323 modinfo[cur+1].pathname = NULL;
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index 3ac5a81a5..1a4f5d4d4 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -482,10 +482,8 @@ static struct dep_t *build_dep(void)
482 dep = xstrndup(deps, next - deps - ext + 1); 482 dep = xstrndup(deps, next - deps - ext + 1);
483 483
484 /* Add the new dependable module name */ 484 /* Add the new dependable module name */
485 current->m_depcnt++; 485 current->m_deparr = xrealloc_vector(current->m_deparr, 2, current->m_depcnt);
486 current->m_deparr = xrealloc(current->m_deparr, 486 current->m_deparr[current->m_depcnt++] = dep;
487 sizeof(char *) * current->m_depcnt);
488 current->m_deparr[current->m_depcnt - 1] = dep;
489 487
490 p = next + 2; 488 p = next + 2;
491 } while (next < end); 489 } while (next < end);
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 2b5e8a14e..cb937cac4 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -721,11 +721,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
721 currmap = xzalloc(sizeof(*currmap)); 721 currmap = xzalloc(sizeof(*currmap));
722 722
723 while ((first_word = next_word(&rest_of_line)) != NULL) { 723 while ((first_word = next_word(&rest_of_line)) != NULL) {
724 if (currmap->n_matches >= currmap->max_matches) { 724 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
725 currmap->max_matches = currmap->max_matches * 2 + 1;
726 currmap->match = xrealloc(currmap->match,
727 sizeof(*currmap->match) * currmap->max_matches);
728 }
729 currmap->match[currmap->n_matches++] = xstrdup(first_word); 725 currmap->match[currmap->n_matches++] = xstrdup(first_word);
730 } 726 }
731 /*currmap->max_mappings = 0; - done by xzalloc */ 727 /*currmap->max_mappings = 0; - done by xzalloc */
diff --git a/procps/ps.c b/procps/ps.c
index c5dff1814..d7ea9fbbf 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -303,9 +303,8 @@ static const ps_out_t out_spec[] = {
303 303
304static ps_out_t* new_out_t(void) 304static ps_out_t* new_out_t(void)
305{ 305{
306 int i = out_cnt++; 306 out = xrealloc_vector(out, 2, out_cnt);
307 out = xrealloc(out, out_cnt * sizeof(*out)); 307 return &out[out_cnt++];
308 return &out[i];
309} 308}
310 309
311static const ps_out_t* find_out_spec(const char *name) 310static const ps_out_t* find_out_spec(const char *name)
diff --git a/procps/top.c b/procps/top.c
index e13cd1521..392a3c82b 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -814,7 +814,7 @@ int top_main(int argc UNUSED_PARAM, char **argv)
814 int n; 814 int n;
815 if (scan_mask == TOP_MASK) { 815 if (scan_mask == TOP_MASK) {
816 n = ntop; 816 n = ntop;
817 top = xrealloc(top, (++ntop) * sizeof(*top)); 817 top = xrealloc_vector(top, 2, ntop++);
818 top[n].pid = p->pid; 818 top[n].pid = p->pid;
819 top[n].ppid = p->ppid; 819 top[n].ppid = p->ppid;
820 top[n].vsz = p->vsz; 820 top[n].vsz = p->vsz;
@@ -829,7 +829,8 @@ int top_main(int argc UNUSED_PARAM, char **argv)
829 if (!(p->mapped_ro | p->mapped_rw)) 829 if (!(p->mapped_ro | p->mapped_rw))
830 continue; /* kernel threads are ignored */ 830 continue; /* kernel threads are ignored */
831 n = ntop; 831 n = ntop;
832 top = xrealloc(topmem, (++ntop) * sizeof(*topmem)); 832 /* No bug here - top and topmem are the same */
833 top = xrealloc_vector(topmem, 2, ntop++);
833 strcpy(topmem[n].comm, p->comm); 834 strcpy(topmem[n].comm, p->comm);
834 topmem[n].pid = p->pid; 835 topmem[n].pid = p->pid;
835 topmem[n].vsz = p->mapped_rw + p->mapped_ro; 836 topmem[n].vsz = p->mapped_rw + p->mapped_ro;
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index bdf5f9716..25aa4069a 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -238,8 +238,8 @@ static struct option *add_long_options(struct option *long_options, char *option
238 if (tlen == 0) 238 if (tlen == 0)
239 bb_error_msg_and_die("empty long option specified"); 239 bb_error_msg_and_die("empty long option specified");
240 } 240 }
241 long_options = xrealloc(long_options, 241//TODO: zeroing version of xrealloc_vector!
242 sizeof(long_options[0]) * (long_nr+2)); 242 long_options = xrealloc_vector(long_options, 4, long_nr);
243 long_options[long_nr].has_arg = arg_opt; 243 long_options[long_nr].has_arg = arg_opt;
244 long_options[long_nr].flag = NULL; 244 long_options[long_nr].flag = NULL;
245 long_options[long_nr].val = LONG_OPT; 245 long_options[long_nr].val = LONG_OPT;
diff --git a/util-linux/mount.c b/util-linux/mount.c
index 2ceabced6..9f9249f0a 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -332,7 +332,7 @@ static long parse_mount_options(char *options, char **unrecognized)
332 if (unrecognized && i == ARRAY_SIZE(mount_options)) { 332 if (unrecognized && i == ARRAY_SIZE(mount_options)) {
333 // Add it to strflags, to pass on to kernel 333 // Add it to strflags, to pass on to kernel
334 i = *unrecognized ? strlen(*unrecognized) : 0; 334 i = *unrecognized ? strlen(*unrecognized) : 0;
335 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2); 335 *unrecognized = xrealloc(*unrecognized, i + strlen(options) + 2);
336 336
337 // Comma separated if it's not the first one 337 // Comma separated if it's not the first one
338 if (i) (*unrecognized)[i++] = ','; 338 if (i) (*unrecognized)[i++] = ',';