diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-26 15:45:17 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-26 15:45:17 +0000 |
commit | dd16e0325070225462e98088574fd17defad0ae0 (patch) | |
tree | af6c1681e91a3e5c13bf3dce8aa798c07462468a | |
parent | 48a92503e79c2345601821c4599ee25d1db35d66 (diff) | |
download | busybox-w32-dd16e0325070225462e98088574fd17defad0ae0.tar.gz busybox-w32-dd16e0325070225462e98088574fd17defad0ae0.tar.bz2 busybox-w32-dd16e0325070225462e98088574fd17defad0ae0.zip |
small fixes:
fix xstrdup to not grossly overallocate memory
use xopen instean of xopen3 in several places
etc.
git-svn-id: svn://busybox.net/trunk/busybox@16673 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | archival/tar.c | 2 | ||||
-rw-r--r-- | coreutils/dd.c | 2 | ||||
-rw-r--r-- | libbb/xfuncs.c | 30 | ||||
-rw-r--r-- | miscutils/crontab.c | 7 | ||||
-rw-r--r-- | miscutils/rx.c | 2 | ||||
-rw-r--r-- | networking/ftpgetput.c | 2 | ||||
-rw-r--r-- | networking/wget.c | 4 | ||||
-rw-r--r-- | runit/svlogd.c | 6 | ||||
-rw-r--r-- | util-linux/mdev.c | 4 |
9 files changed, 35 insertions, 24 deletions
diff --git a/archival/tar.c b/archival/tar.c index 48a1c34cd..be3df687e 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -851,7 +851,7 @@ int tar_main(int argc, char **argv) | |||
851 | tar_handle->src_fd = fileno(tar_stream); | 851 | tar_handle->src_fd = fileno(tar_stream); |
852 | tar_handle->seek = seek_by_read; | 852 | tar_handle->seek = seek_by_read; |
853 | } else { | 853 | } else { |
854 | tar_handle->src_fd = xopen3(tar_filename, flags, 0666); | 854 | tar_handle->src_fd = xopen(tar_filename, flags); |
855 | } | 855 | } |
856 | } | 856 | } |
857 | 857 | ||
diff --git a/coreutils/dd.c b/coreutils/dd.c index 01702a580..01f37abeb 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c | |||
@@ -138,7 +138,7 @@ int dd_main(int argc, char **argv) | |||
138 | if (!seek && (flags & trunc_flag)) | 138 | if (!seek && (flags & trunc_flag)) |
139 | oflag |= O_TRUNC; | 139 | oflag |= O_TRUNC; |
140 | 140 | ||
141 | ofd = xopen3(outfile, oflag, 0666); | 141 | ofd = xopen(outfile, oflag); |
142 | 142 | ||
143 | if (seek && (flags & trunc_flag)) { | 143 | if (seek && (flags & trunc_flag)) { |
144 | if (ftruncate(ofd, seek * obs) < 0) { | 144 | if (ftruncate(ofd, seek * obs) < 0) { |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 773e718b8..ade639516 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -57,7 +57,7 @@ char * xstrdup(const char *s) | |||
57 | if (s == NULL) | 57 | if (s == NULL) |
58 | return NULL; | 58 | return NULL; |
59 | 59 | ||
60 | t = strdup (s); | 60 | t = strdup(s); |
61 | 61 | ||
62 | if (t == NULL) | 62 | if (t == NULL) |
63 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 63 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
@@ -69,23 +69,33 @@ char * xstrdup(const char *s) | |||
69 | // the (possibly truncated to length n) string into it. | 69 | // the (possibly truncated to length n) string into it. |
70 | char * xstrndup(const char *s, int n) | 70 | char * xstrndup(const char *s, int n) |
71 | { | 71 | { |
72 | int m; | ||
72 | char *t; | 73 | char *t; |
73 | 74 | ||
74 | if (ENABLE_DEBUG && s == NULL) | 75 | if (ENABLE_DEBUG && s == NULL) |
75 | bb_error_msg_and_die("xstrndup bug"); | 76 | bb_error_msg_and_die("xstrndup bug"); |
76 | 77 | ||
77 | /* TODO: think about xstrndup("abc", 10000)!!! */ | 78 | /* We can just xmalloc(n+1) and strncpy into it, */ |
78 | t = xmalloc(++n); | 79 | /* but think about xstrndup("abc", 10000) wastage! */ |
80 | m = n; | ||
81 | t = (char*) s; | ||
82 | while (m) { | ||
83 | if (!*t) break; | ||
84 | m--; t++; | ||
85 | } | ||
86 | n = n - m; | ||
87 | t = xmalloc(n + 1); | ||
88 | t[n] = '\0'; | ||
79 | 89 | ||
80 | return safe_strncpy(t,s,n); | 90 | return memcpy(t,s,n); |
81 | } | 91 | } |
82 | 92 | ||
83 | // Die if we can't open a file and return a FILE * to it. | 93 | // Die if we can't open a file and return a FILE * to it. |
84 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. | 94 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. |
85 | FILE *xfopen(const char *path, const char *mode) | 95 | FILE *xfopen(const char *path, const char *mode) |
86 | { | 96 | { |
87 | FILE *fp; | 97 | FILE *fp = fopen(path, mode); |
88 | if ((fp = fopen(path, mode)) == NULL) | 98 | if (fp == NULL) |
89 | bb_perror_msg_and_die("%s", path); | 99 | bb_perror_msg_and_die("%s", path); |
90 | return fp; | 100 | return fp; |
91 | } | 101 | } |
@@ -93,8 +103,8 @@ FILE *xfopen(const char *path, const char *mode) | |||
93 | // Die if we can't open an existing file and return an fd. | 103 | // Die if we can't open an existing file and return an fd. |
94 | int xopen(const char *pathname, int flags) | 104 | int xopen(const char *pathname, int flags) |
95 | { | 105 | { |
96 | if (ENABLE_DEBUG && (flags & O_CREAT)) | 106 | //if (ENABLE_DEBUG && (flags & O_CREAT)) |
97 | bb_error_msg_and_die("xopen() with O_CREAT"); | 107 | // bb_error_msg_and_die("xopen() with O_CREAT"); |
98 | 108 | ||
99 | return xopen3(pathname, flags, 0666); | 109 | return xopen3(pathname, flags, 0666); |
100 | } | 110 | } |
@@ -142,7 +152,7 @@ off_t xlseek(int fd, off_t offset, int whence) | |||
142 | return off; | 152 | return off; |
143 | } | 153 | } |
144 | 154 | ||
145 | // Die with supplied error message if this FILE * has ferror set. | 155 | // Die with supplied filename if this FILE * has ferror set. |
146 | void die_if_ferror(FILE *fp, const char *fn) | 156 | void die_if_ferror(FILE *fp, const char *fn) |
147 | { | 157 | { |
148 | if (ferror(fp)) { | 158 | if (ferror(fp)) { |
@@ -214,7 +224,6 @@ void xsetenv(const char *key, const char *value) | |||
214 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 224 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
215 | } | 225 | } |
216 | 226 | ||
217 | |||
218 | // Converts unsigned long long value into compact 4-char | 227 | // Converts unsigned long long value into compact 4-char |
219 | // representation. Examples: "1234", "1.2k", " 27M", "123T" | 228 | // representation. Examples: "1234", "1.2k", " 27M", "123T" |
220 | // Fifth char is always '\0' | 229 | // Fifth char is always '\0' |
@@ -257,7 +266,6 @@ void smart_ulltoa5(unsigned long long ul, char buf[5]) | |||
257 | buf[4] = '\0'; | 266 | buf[4] = '\0'; |
258 | } | 267 | } |
259 | 268 | ||
260 | |||
261 | // Convert unsigned integer to ascii, writing into supplied buffer. A | 269 | // Convert unsigned integer to ascii, writing into supplied buffer. A |
262 | // truncated result is always null terminated (unless buflen is 0), and | 270 | // truncated result is always null terminated (unless buflen is 0), and |
263 | // contains the first few digits of the result ala strncpy. | 271 | // contains the first few digits of the result ala strncpy. |
diff --git a/miscutils/crontab.c b/miscutils/crontab.c index 743ac74ae..39d3aae41 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c | |||
@@ -156,6 +156,7 @@ int crontab_main(int ac, char **av) | |||
156 | break; | 156 | break; |
157 | case EDIT: | 157 | case EDIT: |
158 | { | 158 | { |
159 | /* FIXME: messy code here! we have file copying helpers for this! */ | ||
159 | FILE *fi; | 160 | FILE *fi; |
160 | int fd; | 161 | int fd; |
161 | int n; | 162 | int n; |
@@ -163,11 +164,12 @@ int crontab_main(int ac, char **av) | |||
163 | 164 | ||
164 | snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); | 165 | snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid()); |
165 | fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600); | 166 | fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600); |
167 | /* race, use fchown */ | ||
166 | chown(tmp, getuid(), getgid()); | 168 | chown(tmp, getuid(), getgid()); |
167 | fi = fopen(pas->pw_name, "r"); | 169 | fi = fopen(pas->pw_name, "r"); |
168 | if (fi) { | 170 | if (fi) { |
169 | while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) | 171 | while ((n = fread(buf, 1, sizeof(buf), fi)) > 0) |
170 | write(fd, buf, n); | 172 | full_write(fd, buf, n); |
171 | } | 173 | } |
172 | EditFile(caller, tmp); | 174 | EditFile(caller, tmp); |
173 | remove(tmp); | 175 | remove(tmp); |
@@ -178,6 +180,7 @@ int crontab_main(int ac, char **av) | |||
178 | /* fall through */ | 180 | /* fall through */ |
179 | case REPLACE: | 181 | case REPLACE: |
180 | { | 182 | { |
183 | /* same here */ | ||
181 | char path[1024]; | 184 | char path[1024]; |
182 | int fd; | 185 | int fd; |
183 | int n; | 186 | int n; |
@@ -186,7 +189,7 @@ int crontab_main(int ac, char **av) | |||
186 | fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600); | 189 | fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600); |
187 | if (fd >= 0) { | 190 | if (fd >= 0) { |
188 | while ((n = read(repFd, buf, sizeof(buf))) > 0) { | 191 | while ((n = read(repFd, buf, sizeof(buf))) > 0) { |
189 | write(fd, buf, n); | 192 | full_write(fd, buf, n); |
190 | } | 193 | } |
191 | close(fd); | 194 | close(fd); |
192 | rename(path, pas->pw_name); | 195 | rename(path, pas->pw_name); |
diff --git a/miscutils/rx.c b/miscutils/rx.c index f723c1676..9b9f6afd4 100644 --- a/miscutils/rx.c +++ b/miscutils/rx.c | |||
@@ -263,7 +263,7 @@ int rx_main(int argc, char **argv) | |||
263 | 263 | ||
264 | fn = argv[1]; | 264 | fn = argv[1]; |
265 | ttyfd = xopen(CURRENT_TTY, O_RDWR); | 265 | ttyfd = xopen(CURRENT_TTY, O_RDWR); |
266 | filefd = xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666); | 266 | filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC); |
267 | 267 | ||
268 | if (tcgetattr(ttyfd, &tty) < 0) | 268 | if (tcgetattr(ttyfd, &tty) < 0) |
269 | bb_perror_msg_and_die("tcgetattr"); | 269 | bb_perror_msg_and_die("tcgetattr"); |
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index a842401c0..fa1854903 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c | |||
@@ -166,7 +166,7 @@ int ftp_receive(ftp_host_info_t *server, FILE *control_stream, | |||
166 | if (do_continue) { | 166 | if (do_continue) { |
167 | fd_local = xopen(local_path, O_APPEND | O_WRONLY); | 167 | fd_local = xopen(local_path, O_APPEND | O_WRONLY); |
168 | } else { | 168 | } else { |
169 | fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666); | 169 | fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY); |
170 | } | 170 | } |
171 | } | 171 | } |
172 | 172 | ||
diff --git a/networking/wget.c b/networking/wget.c index 5a547ce1f..1e51ce96b 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -452,8 +452,8 @@ int wget_main(int argc, char **argv) | |||
452 | 452 | ||
453 | /* Do it before progressmeter (want to have nice error message) */ | 453 | /* Do it before progressmeter (want to have nice error message) */ |
454 | if (output_fd < 0) | 454 | if (output_fd < 0) |
455 | output_fd = xopen3(fname_out, | 455 | output_fd = xopen(fname_out, |
456 | O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666); | 456 | O_WRONLY|O_CREAT|O_EXCL|O_TRUNC); |
457 | 457 | ||
458 | if (!(opt & WGET_OPT_QUIET)) | 458 | if (!(opt & WGET_OPT_QUIET)) |
459 | progressmeter(-1); | 459 | progressmeter(-1); |
diff --git a/runit/svlogd.c b/runit/svlogd.c index b2fbe5167..7024c3db4 100644 --- a/runit/svlogd.c +++ b/runit/svlogd.c | |||
@@ -148,19 +148,19 @@ static unsigned processorstart(struct logdir *ld) | |||
148 | if (fd_move(0, fd) == -1) | 148 | if (fd_move(0, fd) == -1) |
149 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); | 149 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); |
150 | ld->fnsave[26] = 't'; | 150 | ld->fnsave[26] = 't'; |
151 | fd = xopen3(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644); | 151 | fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT); |
152 | if (fd_move(1, fd) == -1) | 152 | if (fd_move(1, fd) == -1) |
153 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); | 153 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); |
154 | fd = open_read("state"); | 154 | fd = open_read("state"); |
155 | if (fd == -1) { | 155 | if (fd == -1) { |
156 | if (errno != ENOENT) | 156 | if (errno != ENOENT) |
157 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name); | 157 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name); |
158 | close(xopen3("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644)); | 158 | close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT)); |
159 | fd = xopen("state", O_RDONLY|O_NDELAY); | 159 | fd = xopen("state", O_RDONLY|O_NDELAY); |
160 | } | 160 | } |
161 | if (fd_move(4, fd) == -1) | 161 | if (fd_move(4, fd) == -1) |
162 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); | 162 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); |
163 | fd = xopen3("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644); | 163 | fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT); |
164 | if (fd_move(5, fd) == -1) | 164 | if (fd_move(5, fd) == -1) |
165 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); | 165 | bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name); |
166 | 166 | ||
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index c03dd6130..957316d52 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
@@ -73,7 +73,7 @@ static void make_device(char *path, int delete) | |||
73 | 73 | ||
74 | line++; | 74 | line++; |
75 | /* find end of this line */ | 75 | /* find end of this line */ |
76 | for(end=pos; end-conf<len && *end!='\n'; end++) | 76 | for (end=pos; end-conf<len && *end!='\n'; end++) |
77 | ; | 77 | ; |
78 | 78 | ||
79 | /* Three fields: regex, uid:gid, mode */ | 79 | /* Three fields: regex, uid:gid, mode */ |
@@ -111,7 +111,7 @@ static void make_device(char *path, int delete) | |||
111 | char *s, *s2; | 111 | char *s, *s2; |
112 | 112 | ||
113 | /* Find : */ | 113 | /* Find : */ |
114 | for(s=pos; s<end2 && *s!=':'; s++) | 114 | for (s=pos; s<end2 && *s!=':'; s++) |
115 | ; | 115 | ; |
116 | if (s == end2) break; | 116 | if (s == end2) break; |
117 | 117 | ||