aboutsummaryrefslogtreecommitdiff
path: root/libbb/fgets_str.c
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/fgets_str.c
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/fgets_str.c')
-rw-r--r--libbb/fgets_str.c26
1 files changed, 23 insertions, 3 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}