diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-20 01:27:59 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-20 01:27:59 +0000 |
commit | 25cfe4996ec967e19283f6b66c45cf220a615528 (patch) | |
tree | e8ff708534af65f0cdf6739d04496969d4a0ddb5 /libbb | |
parent | 5b3adae7e844f87febdcae15e0f3a652839fe750 (diff) | |
download | busybox-w32-25cfe4996ec967e19283f6b66c45cf220a615528.tar.gz busybox-w32-25cfe4996ec967e19283f6b66c45cf220a615528.tar.bz2 busybox-w32-25cfe4996ec967e19283f6b66c45cf220a615528.zip |
libbb: prevent xmalloc_open_read_close from dying on seek failure
start_stop_daemon: use open_read_close instead of xmalloc_open_read_close
start_stop_daemon: use local structure instead of global one
function old new delta
check 1620 1661 +41
xmalloc_open_read_close 171 190 +19
start_stop_daemon_main 976 954 -22
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 60/-22) Total: 38 bytes
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/read.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/libbb/read.c b/libbb/read.c index ba366cb97..e5f140f3b 100644 --- a/libbb/read.c +++ b/libbb/read.c | |||
@@ -208,25 +208,34 @@ ssize_t open_read_close(const char *filename, void *buf, size_t size) | |||
208 | void *xmalloc_open_read_close(const char *filename, size_t *sizep) | 208 | void *xmalloc_open_read_close(const char *filename, size_t *sizep) |
209 | { | 209 | { |
210 | char *buf; | 210 | char *buf; |
211 | size_t size = sizep ? *sizep : INT_MAX; | 211 | size_t size; |
212 | int fd; | 212 | int fd; |
213 | off_t len; | 213 | off_t len; |
214 | 214 | ||
215 | fd = open(filename, O_RDONLY); | 215 | fd = open(filename, O_RDONLY); |
216 | if (fd < 0) | 216 | if (fd < 0) |
217 | return NULL; | 217 | return NULL; |
218 | |||
218 | /* /proc/N/stat files report len 0 here */ | 219 | /* /proc/N/stat files report len 0 here */ |
219 | /* In order to make such files readable, we add small const */ | 220 | /* In order to make such files readable, we add small const */ |
220 | len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ | 221 | size = 0x3ff; /* read only 1k on unseekable files */ |
221 | xlseek(fd, 0, SEEK_SET); | 222 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
222 | if (len < size) | 223 | if (len != (off_t)-1) { |
223 | size = len; | 224 | xlseek(fd, 0, SEEK_SET); |
225 | size = sizep ? *sizep : INT_MAX; | ||
226 | if (len < size) | ||
227 | size = len; | ||
228 | } | ||
229 | |||
224 | buf = xmalloc(size + 1); | 230 | buf = xmalloc(size + 1); |
225 | size = read_close(fd, buf, size); | 231 | size = read_close(fd, buf, size); |
226 | if ((ssize_t)size < 0) | 232 | if ((ssize_t)size < 0) { |
227 | bb_perror_msg_and_die("'%s'", filename); | 233 | free(buf); |
234 | return NULL; | ||
235 | } | ||
228 | xrealloc(buf, size + 1); | 236 | xrealloc(buf, size + 1); |
229 | buf[size] = '\0'; | 237 | buf[size] = '\0'; |
238 | |||
230 | if (sizep) | 239 | if (sizep) |
231 | *sizep = size; | 240 | *sizep = size; |
232 | return buf; | 241 | return buf; |