Skip to content

Commit 9e77a00

Browse files
committed
Fix php-wasm popen stream leaks
1 parent 6468205 commit 9e77a00

7 files changed

Lines changed: 24573 additions & 20575 deletions

File tree

packages/php-wasm/compile/php/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,10 @@ RUN /root/replace.sh 's/PHPAPI int php_exec(.+)$/PHPAPI extern int php_exec\1; i
459459
# the process inside PHP_FUNCTION(popen).
460460
RUN /root/replace.sh 's/#define VCWD_POPEN.+/#define VCWD_POPEN(command, type) wasm_popen(command,type)/g' /root/php-src/Zend/zend_virtual_cwd.h
461461
RUN echo 'extern FILE *wasm_popen(const char *cmd, const char *mode);' >> /root/php-src/Zend/zend_virtual_cwd.h
462+
# wasm_popen() buffers read-mode output to a temporary regular file, so the PHP
463+
# stream must close it with fclose() semantics instead of pclose() semantics.
464+
RUN perl -0pi -e "s/stream = php_stream_fopen_from_pipe\\(fp, mode\\);/stream = (*posix_mode == 'r') ? php_stream_fopen_from_file(fp, mode) : php_stream_fopen_from_pipe(fp, mode);/g" /root/php-src/ext/standard/file.c
465+
RUN perl -0pi -e 's/stream = php_stream_fopen_from_pipe\(in, "rb"\);/stream = php_stream_fopen_from_file(in, "rb");/g' /root/php-src/ext/standard/exec.c
462466

463467
# Provide a custom implementation of the shutdown() function.
464468
RUN perl -pi.bak -e $'s/(\s+)shutdown\(/$1 wasm_shutdown(/g' /root/php-src/sapi/cli/php_cli_server.c

packages/php-wasm/compile/php/php_wasm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ EMSCRIPTEN_KEEPALIVE int wasm_php_exec(int type, const char *cmd, zval *array, z
675675
goto err;
676676
}
677677

678-
stream = php_stream_fopen_from_pipe(fp, "rb");
678+
stream = php_stream_fopen_from_file(fp, "rb");
679679

680680
buf = (char *)emalloc(EXEC_INPUT_BUF);
681681
buflen = EXEC_INPUT_BUF;
1.35 KB
Binary file not shown.

0 commit comments

Comments
 (0)