pajoye Tue Sep 2 10:15:55 2008 UTC Modified files: (Branch: PHP_5_3) /php-src/ext/fileinfo fileinfo.c /php-src/ext/fileinfo/libmagic magic.c /php-src/ext/fileinfo/tests finfo_file_001.phpt Log: - don't close caller streams in libmagic (prevent leak and zombie stream) - string returned by magic_* are freed on magic_close, duplicate before calling magic_close (set return value) - if stat failed, don't try to call magic_* (when FILEINFO_MODE_FILE) http://cvs.php.net/viewvc.cgi/php-src/ext/fileinfo/fileinfo.c?r1=1.20.2.11&r2=1.20.2.12&diff_format=u Index: php-src/ext/fileinfo/fileinfo.c diff -u php-src/ext/fileinfo/fileinfo.c:1.20.2.11 php-src/ext/fileinfo/fileinfo.c:1.20.2.12 --- php-src/ext/fileinfo/fileinfo.c:1.20.2.11 Mon Sep 1 23:44:00 2008 +++ php-src/ext/fileinfo/fileinfo.c Tue Sep 2 10:15:54 2008 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fileinfo.c,v 1.20.2.11 2008/09/01 23:44:00 felipe Exp $ */ +/* $Id: fileinfo.c,v 1.20.2.12 2008/09/02 10:15:54 pajoye Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -452,10 +452,8 @@ magic = magic_open(MAGIC_MIME); if (magic_load(magic, NULL) == -1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to load magic database."); - magic_close(magic); - RETURN_FALSE; + goto common; } - } else if (object) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &buffer, &buffer_len, &options, &zcontext) == FAILURE) { RETURN_FALSE; @@ -508,12 +506,19 @@ if (buffer == NULL || !*buffer) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty filename or path"); - RETURN_FALSE; + RETVAL_FALSE; + goto clean; } - if (php_sys_stat(buffer, &sb) == 0 && (sb.st_mode & _S_IFDIR)) { - ret_val = mime_directory; - goto common; + if (php_sys_stat(buffer, &sb) == 0) { + if (sb.st_mode & _S_IFDIR) { + ret_val = mime_directory; + goto common; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File or path not found '%s'", buffer); + RETVAL_FALSE; + goto clean; } wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0 TSRMLS_CC); @@ -524,10 +529,8 @@ php_stream *stream = php_stream_open_wrapper_ex(buffer, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); if (!stream) { - if (mimetype_emu) { - magic_close(magic); - } - RETURN_FALSE; + RETVAL_FALSE; + goto clean; } ret_val = magic_stream(magic, stream); @@ -538,27 +541,26 @@ default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only process string or stream arguments"); - RETURN_FALSE; } common: + if (ret_val) { + RETVAL_STRING(ret_val, 1); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); + RETVAL_FALSE; + } + +clean: if (mimetype_emu) { - if (magic) { - magic_close(magic); - } + magic_close(magic); } /* Restore options */ if (options) { FINFO_SET_OPTION(magic, finfo->options) } - - if (!ret_val) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); - RETURN_FALSE; - } else { - RETURN_STRING(ret_val, 1); - } + return; } /* }}} */ http://cvs.php.net/viewvc.cgi/php-src/ext/fileinfo/libmagic/magic.c?r1=1.1.2.3&r2=1.1.2.4&diff_format=u Index: php-src/ext/fileinfo/libmagic/magic.c diff -u php-src/ext/fileinfo/libmagic/magic.c:1.1.2.3 php-src/ext/fileinfo/libmagic/magic.c:1.1.2.4 --- php-src/ext/fileinfo/libmagic/magic.c:1.1.2.3 Mon Sep 1 18:56:06 2008 +++ php-src/ext/fileinfo/libmagic/magic.c Tue Sep 2 10:15:54 2008 @@ -275,7 +275,7 @@ unsigned char *buf; struct stat sb; ssize_t nbytes = 0; /* number of bytes read from a datafile */ - int ispipe = 0; + int no_in_stream = 0; TSRMLS_FETCH(); if (!inname && !stream) { @@ -303,31 +303,29 @@ goto done; } - errno = 0; + errno = 0; - if (!stream && inname) { + if (!stream && inname) { + no_in_stream = 1; #if (PHP_MAJOR_VERSION < 6) stream = php_stream_open_wrapper(inname, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); #else stream = php_stream_open_wrapper(inname, "rb", REPORT_ERRORS, NULL); #endif - } + } - if (!stream) { - fprintf(stderr, "couldn't open file\n"); - if (info_from_stat(ms, sb.st_mode) == -1) - goto done; - rv = 0; - goto done; - } + if (!stream) { + fprintf(stderr, "couldn't open file\n"); + if (info_from_stat(ms, sb.st_mode) == -1) + goto done; + rv = 0; + goto done; + } #ifdef O_NONBLOCK -/* we should be already be in non blocking mode for network socket - * leaving the comment/#ifdef as documentation - */ + /* we should be already be in non blocking mode for network socket */ #endif - /* * try looking at the first HOWMANY bytes */ @@ -343,7 +341,7 @@ done: efree(buf); - if (stream) { + if (no_in_stream && stream) { php_stream_close(stream); } @@ -351,7 +349,6 @@ return rv == 0 ? file_getbuffer(ms) : NULL; } - public const char * magic_buffer(struct magic_set *ms, const void *buf, size_t nb) { http://cvs.php.net/viewvc.cgi/php-src/ext/fileinfo/tests/finfo_file_001.phpt?r1=1.1.2.5&r2=1.1.2.6&diff_format=u Index: php-src/ext/fileinfo/tests/finfo_file_001.phpt diff -u php-src/ext/fileinfo/tests/finfo_file_001.phpt:1.1.2.5 php-src/ext/fileinfo/tests/finfo_file_001.phpt:1.1.2.6 --- php-src/ext/fileinfo/tests/finfo_file_001.phpt:1.1.2.5 Mon Sep 1 23:44:00 2008 +++ php-src/ext/fileinfo/tests/finfo_file_001.phpt Tue Sep 2 10:15:54 2008 @@ -22,5 +22,5 @@ bool(false) string(9) "directory" -Warning: finfo_file(&): failed to open stream: No such file or directory in %s on line %d +Warning: finfo_file(): File or path not found '&' in %s on line %d bool(false)
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php