41
41
static int _Exec (int type , char * cmd , pval * array , pval * return_value )
42
42
{
43
43
FILE * fp ;
44
- char buf [EXEC_INPUT_BUF ], * tmp = NULL ;
44
+ char * buf , * tmp = NULL ;
45
+ int buflen = 0 ;
45
46
int t , l , ret , output = 1 ;
46
47
int overflow_limit , lcmd , ldir ;
47
48
char * b , * c , * d = NULL ;
48
49
PLS_FETCH ();
49
50
51
+ buf = (char * ) emalloc (EXEC_INPUT_BUF );
52
+ if (!buf ) {
53
+ php3_error (E_WARNING , "Unable to emalloc %d bytes for exec buffer" , EXEC_INPUT_BUF );
54
+ return -1 ;
55
+ }
56
+ buflen = EXEC_INPUT_BUF ;
57
+
50
58
if (PG (safe_mode )) {
51
59
lcmd = strlen (cmd );
52
60
ldir = strlen (PG (safe_mode_exec_dir ));
@@ -56,6 +64,7 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
56
64
if (c ) * c = '\0' ;
57
65
if (strstr (cmd , ".." )) {
58
66
php3_error (E_WARNING , "No '..' components allowed in path" );
67
+ efree (buf );
59
68
return -1 ;
60
69
}
61
70
d = emalloc (l );
@@ -85,6 +94,7 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
85
94
if (!fp ) {
86
95
php3_error (E_WARNING , "Unable to fork [%s]" , d );
87
96
efree (d );
97
+ efree (buf );
88
98
return -1 ;
89
99
}
90
100
} else { /* not safe_mode */
@@ -95,6 +105,7 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
95
105
#endif
96
106
if (!fp ) {
97
107
php3_error (E_WARNING , "Unable to fork [%s]" , cmd );
108
+ efree (buf );
98
109
return -1 ;
99
110
}
100
111
}
@@ -106,7 +117,33 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
106
117
}
107
118
}
108
119
if (type != 3 ) {
109
- while (fgets (buf , EXEC_INPUT_BUF - 1 , fp )) {
120
+ l = 0 ;
121
+ while ( !feof (fp ) || l != 0 ) {
122
+ l = 0 ;
123
+ /* Read a line or fill the buffer, whichever comes first */
124
+ do {
125
+ if ( buflen <= (l + 1 ) ) {
126
+ buf = erealloc (buf , buflen + EXEC_INPUT_BUF );
127
+ if ( buf == NULL ) {
128
+ php3_error (E_WARNING , "Unable to erealloc %d bytes for exec buffer" ,
129
+ buflen + EXEC_INPUT_BUF );
130
+ return -1 ;
131
+ }
132
+ buflen += EXEC_INPUT_BUF ;
133
+ }
134
+
135
+ if ( fgets (& (buf [l ]), buflen - l , fp ) == NULL ) {
136
+ /* eof */
137
+ break ;
138
+ }
139
+ l += strlen (& (buf [l ]));
140
+ } while ( (l > 0 ) && (buf [l - 1 ] != '\n' ) );
141
+
142
+ if ( feof (fp ) && (l == 0 ) ) {
143
+ break ;
144
+ }
145
+
146
+
110
147
if (type == 1 ) {
111
148
SLS_FETCH ();
112
149
@@ -132,7 +169,7 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
132
169
/* strip trailing whitespaces */
133
170
l = strlen (buf );
134
171
t = l ;
135
- while (l && isspace ((int )buf [-- l ]));
172
+ while (l -- && isspace ((int )buf [l ]));
136
173
if (l < t ) {
137
174
buf [l + 1 ] = '\0' ;
138
175
}
@@ -173,6 +210,7 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
173
210
#endif
174
211
175
212
if (d ) efree (d );
213
+ efree (buf );
176
214
return ret ;
177
215
}
178
216
0 commit comments