aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-03-18 17:32:44 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-03-18 17:32:44 +0000
commit9f57cf6604638f14390effa01b51c8ad979f14cd (patch)
tree713ecefd47867bb5d2b16c4ae3e5acd28c1c90e3 /libbb
parentfce4a9454c5399c2ce8ca8e87048331c6e3d98fa (diff)
downloadbusybox-w32-9f57cf6604638f14390effa01b51c8ad979f14cd.tar.gz
busybox-w32-9f57cf6604638f14390effa01b51c8ad979f14cd.tar.bz2
busybox-w32-9f57cf6604638f14390effa01b51c8ad979f14cd.zip
ftpd: fix command fetching to not do it in 1-byte reads;
fix command de-escaping. Tested to download files with embeeded \xff and LF. libbb: tweaks for the above function old new delta ftpd_main 2231 2321 +90 xmalloc_fgets_internal 190 222 +32 xmalloc_fgets_str_len - 27 +27 xmalloc_fgets_str 7 23 +16 xmalloc_fgetline_str 10 26 +16 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 4/0 up/down: 181/0) Total: 181 bytes
Diffstat (limited to 'libbb')
-rw-r--r--libbb/fgets_str.c26
-rw-r--r--libbb/read.c6
2 files changed, 26 insertions, 6 deletions
diff --git a/libbb/fgets_str.c b/libbb/fgets_str.c
index 8026a15da..3fe61cdc3 100644
--- a/libbb/fgets_str.c
+++ b/libbb/fgets_str.c
@@ -10,7 +10,7 @@
10 10
11#include "libbb.h" 11#include "libbb.h"
12 12
13static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off) 13static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off, size_t *maxsz_p)
14{ 14{
15 char *linebuf = NULL; 15 char *linebuf = NULL;
16 const int term_length = strlen(terminating_string); 16 const int term_length = strlen(terminating_string);
@@ -18,6 +18,7 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string,
18 int linebufsz = 0; 18 int linebufsz = 0;
19 int idx = 0; 19 int idx = 0;
20 int ch; 20 int ch;
21 size_t maxsz = *maxsz_p;
21 22
22 while (1) { 23 while (1) {
23 ch = fgetc(file); 24 ch = fgetc(file);
@@ -30,6 +31,11 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string,
30 if (idx >= linebufsz) { 31 if (idx >= linebufsz) {
31 linebufsz += 200; 32 linebufsz += 200;
32 linebuf = xrealloc(linebuf, linebufsz); 33 linebuf = xrealloc(linebuf, linebufsz);
34 if (idx >= maxsz) {
35 linebuf[idx] = ch;
36 idx++;
37 break;
38 }
33 } 39 }
34 40
35 linebuf[idx] = ch; 41 linebuf[idx] = ch;
@@ -48,6 +54,7 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string,
48 /* Grow/shrink *first*, then store NUL */ 54 /* Grow/shrink *first*, then store NUL */
49 linebuf = xrealloc(linebuf, idx + 1); 55 linebuf = xrealloc(linebuf, idx + 1);
50 linebuf[idx] = '\0'; 56 linebuf[idx] = '\0';
57 *maxsz_p = idx;
51 return linebuf; 58 return linebuf;
52} 59}
53 60
@@ -57,10 +64,23 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string,
57 * Return NULL if EOF is reached immediately. */ 64 * Return NULL if EOF is reached immediately. */
58char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string) 65char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string)
59{ 66{
60 return xmalloc_fgets_internal(file, terminating_string, 0); 67 size_t maxsz = INT_MAX - 4095;
68 return xmalloc_fgets_internal(file, terminating_string, 0, &maxsz);
69}
70
71char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p)
72{
73 size_t maxsz;
74
75 if (!maxsz_p) {
76 maxsz = INT_MAX - 4095;
77 maxsz_p = &maxsz;
78 }
79 return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p);
61} 80}
62 81
63char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string) 82char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string)
64{ 83{
65 return xmalloc_fgets_internal(file, terminating_string, 1); 84 size_t maxsz = INT_MAX - 4095;
85 return xmalloc_fgets_internal(file, terminating_string, 1, &maxsz);
66} 86}
diff --git a/libbb/read.c b/libbb/read.c
index 815007c1e..37503e84d 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -141,7 +141,7 @@ char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
141{ 141{
142 char *p; 142 char *p;
143 size_t sz = buf ? strlen(buf) : 0; 143 size_t sz = buf ? strlen(buf) : 0;
144 size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t); 144 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
145 145
146 goto jump_in; 146 goto jump_in;
147 while (sz < maxsz) { 147 while (sz < maxsz) {
@@ -198,7 +198,7 @@ void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
198 size_t to_read; 198 size_t to_read;
199 struct stat st; 199 struct stat st;
200 200
201 to_read = maxsz_p ? *maxsz_p : MAXINT(ssize_t); /* max to read */ 201 to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
202 202
203 /* Estimate file size */ 203 /* Estimate file size */
204 st.st_size = 0; /* in case fstat fails, assume 0 */ 204 st.st_size = 0; /* in case fstat fails, assume 0 */
@@ -262,7 +262,7 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
262 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ 262 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
263 if (len != (off_t)-1) { 263 if (len != (off_t)-1) {
264 xlseek(fd, 0, SEEK_SET); 264 xlseek(fd, 0, SEEK_SET);
265 size = maxsz_p ? *maxsz_p : INT_MAX; 265 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
266 if (len < size) 266 if (len < size)
267 size = len; 267 size = len;
268 } 268 }