exec.c patch for dynamic length strings

From: Date: Tue, 13 Jul 1999 16:59:23 +0000
Subject: exec.c patch for dynamic length strings
Groups: php.dev 
Request: Send a blank email to php-dev+get-8446@lists.php.net to get a copy of this message
The following patch fixes two problems: 1) If any line of output from exec(cmd, array, returnval) is longer than (EXEC_INPUT_BUF - 2) characters, the line will be split among 2 or more array elements. 2) exec() normally removes all trailing whitespace from each array element. However, lines of output that only contain a newline do not have the newline removed. Could someone add them to 3.0.12-dev? I prepared the patch from the 3.0.12-dev source tree in the following manner: cd php3 cvs diff functions/exec.c If I should prepare patches in a different manner, please let me know. Thanks much, Mike -- Mike Gerdts UNIX Systems Administrator Computer-Aided Engineering Center University of Wisconsin - Madison Index: functions/exec.c =================================================================== RCS file: /repository/php3/functions/exec.c,v retrieving revision 1.83 diff -c -r1.83 exec.c *** functions/exec.c 1999/06/16 11:34:18 1.83 --- functions/exec.c 1999/07/13 16:29:26 *************** *** 54,65 **** static int _Exec(int type, char *cmd, pval *array, pval *return_value) { FILE *fp; ! char buf[EXEC_INPUT_BUF], *tmp=NULL; int t, l, ret, output=1; int overflow_limit, lcmd, ldir; char *b, *c, *d=NULL; TLS_VARS; if (php3_ini.safe_mode) { lcmd = strlen(cmd); ldir = strlen(php3_ini.safe_mode_exec_dir); --- 54,73 ---- static int _Exec(int type, char *cmd, pval *array, pval *return_value) { FILE *fp; ! char *buf, *tmp=NULL; ! int buflen=0; int t, l, ret, output=1; int overflow_limit, lcmd, ldir; char *b, *c, *d=NULL; TLS_VARS; + buf = (char*) emalloc(EXEC_INPUT_BUF); + if (!buf) { + php3_error(E_WARNING, "Unable to emalloc %d bytes", EXEC_INPUT_BUF); + return -1; + } + buflen = EXEC_INPUT_BUF; + if (php3_ini.safe_mode) { lcmd = strlen(cmd); ldir = strlen(php3_ini.safe_mode_exec_dir); *************** *** 69,74 **** --- 77,83 ---- if (c) *c = '\0'; if (strstr(cmd, "..")) { php3_error(E_WARNING, "No '..' components allowed in path"); + efree(buf); return -1; } d = emalloc(l); *************** *** 98,103 **** --- 107,113 ---- if (!fp) { php3_error(E_WARNING, "Unable to fork [%s]", d); efree(d); + efree(buf); return -1; } } else { /* not safe_mode */ *************** *** 108,113 **** --- 118,124 ---- #endif if (!fp) { php3_error(E_WARNING, "Unable to fork [%s]", cmd); + efree(buf); return -1; } } *************** *** 122,128 **** } } if (type != 3) { ! while (fgets(buf, EXEC_INPUT_BUF - 1, fp)) { if (type == 1) { if (output) PUTS(buf); #if APACHE --- 133,164 ---- } } if (type != 3) { ! l = 0; ! while ( !feof(fp) || l != 0 ) { ! l = 0; ! /* Read a line or fill the buffer, whichever comes first */ ! do { ! if ( buflen <= (l+1) ) { ! buf = erealloc(buf, buflen + EXEC_INPUT_BUF); ! if ( buf == NULL ) { ! php3_error(E_WARNING, "Unable to erealloc %d bytes", ! buflen + EXEC_INPUT_BUF); ! return -1; ! } ! buflen += EXEC_INPUT_BUF; ! } ! ! if ( fgets(&(buf[l]), buflen - l, fp) == NULL ) { ! /* eof */ ! break; ! } ! l += strlen(&(buf[l])); ! } while ( (l > 0) && (buf[l-1] != '\n') ); ! ! if ( feof(fp) && (l == 0) ) { ! break; ! } ! if (type == 1) { if (output) PUTS(buf); #if APACHE *************** *** 148,154 **** /* strip trailing whitespaces */ l = strlen(buf); t = l; ! while (l && isspace((int)buf[--l])); if (l < t) buf[l + 1] = '\0'; tmp.value.str.len = strlen(buf); tmp.value.str.val = estrndup(buf,tmp.value.str.len); --- 184,190 ---- /* strip trailing whitespaces */ l = strlen(buf); t = l; ! while (l-- && isspace((int)buf[l])); if (l < t) buf[l + 1] = '\0'; tmp.value.str.len = strlen(buf); tmp.value.str.val = estrndup(buf,tmp.value.str.len); *************** *** 190,195 **** --- 226,232 ---- #endif if (d) efree(d); + efree(buf); return ret; }

« previous php.dev (#8446) next »