[php-src] master: Merge branch 'PHP-8.2' into PHP-8.3

From: Date: Sun, 17 Mar 2024 17:52:48 +0000
Subject: [php-src] master: Merge branch 'PHP-8.2' into PHP-8.3
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-127761@lists.php.net to get a copy of this message
Author: Niels Dossche (nielsdos) Date: 2024-03-17T18:38:36+01:00 Commit: https://github.com/php/php-src/commit/dab5f8c15c1179d32ddacaad1564a9fde0e49566 Raw diff: https://github.com/php/php-src/commit/dab5f8c15c1179d32ddacaad1564a9fde0e49566.diff Merge branch 'PHP-8.2' into PHP-8.3 * PHP-8.2: Fix GH-13685: Unexpected null pointer in zend_string.h Changed paths: A ext/spl/tests/gh13685.phpt M NEWS M ext/spl/spl_directory.c Diff: diff --git a/NEWS b/NEWS index 77ba90394580d..bf3f4f1984bd4 100644 --- a/NEWS +++ b/NEWS @@ -37,6 +37,9 @@ PHP NEWS . Fixed bug GH-13680 (Segfault with session_decode and compilation error). (nielsdos) +- SPL: + . Fixed bug GH-13685 (Unexpected null pointer in zend_string.h). (nielsdos) + - Standard: . Fixed bug GH-11808 (Live filesystem modified by tests). (nielsdos) . Fixed GH-13402 (Added validation of \n in $additional_headers of mail()). diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index 528dc5ecc4720..417b38fa99ecf 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -1859,6 +1859,11 @@ static zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry * } /* }}} */ +static ZEND_COLD void spl_filesystem_file_cannot_read(spl_filesystem_object *intern) +{ + zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", ZSTR_VAL(intern->file_name)); +} + static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv) { char *buf; @@ -1868,7 +1873,7 @@ static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bo if (php_stream_eof(intern->u.file.stream)) { if (!silent) { - zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", ZSTR_VAL(intern->file_name)); + spl_filesystem_file_cannot_read(intern); } return FAILURE; } @@ -1923,10 +1928,10 @@ static bool is_line_empty(spl_filesystem_object *intern) || (current_line_len == 2 && current_line[0] == '\r' && current_line[1] == '\n')))); } -static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value) /* {{{ */ +static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value, bool silent) /* {{{ */ { do { - zend_result ret = spl_filesystem_file_read(intern, /* silent */ true, /* csv */ true); + zend_result ret = spl_filesystem_file_read(intern, silent, /* csv */ true); if (ret != SUCCESS) { return ret; } @@ -1952,19 +1957,21 @@ static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, c } /* }}} */ -/* Call to this function reads a line in a "silent" fashion and does not throw an exception */ -static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */ +static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, bool silent) /* {{{ */ { zval retval; /* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */ if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) { - return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL); + return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL, silent); } if (intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) { spl_filesystem_file_free_line(intern); if (php_stream_eof(intern->u.file.stream)) { + if (!silent) { + spl_filesystem_file_cannot_read(intern); + } return FAILURE; } zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(this_ptr), &intern->u.file.func_getCurr, "getCurrentLine", &retval); @@ -1988,18 +1995,17 @@ static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesys zval_ptr_dtor(&retval); return SUCCESS; } else { - return spl_filesystem_file_read(intern, /* silent */ true, /* csv */ false); + return spl_filesystem_file_read(intern, silent, /* csv */ false); } } /* }}} */ -/* Call to this function reads a line in a "silent" fashion and does not throw an exception */ -static zend_result spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */ +static zend_result spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, bool silent) /* {{{ */ { - zend_result ret = spl_filesystem_file_read_line_ex(this_ptr, intern); + zend_result ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent); while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && is_line_empty(intern)) { spl_filesystem_file_free_line(intern); - ret = spl_filesystem_file_read_line_ex(this_ptr, intern); + ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent); } return ret; @@ -2021,7 +2027,7 @@ static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *i intern->u.file.current_line_num = 0; if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) { - spl_filesystem_file_read_line(this_ptr, intern); + spl_filesystem_file_read_line(this_ptr, intern, true); } } /* }}} */ @@ -2175,7 +2181,7 @@ PHP_METHOD(SplFileObject, current) CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern); if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) { - spl_filesystem_file_read_line(ZEND_THIS, intern); + spl_filesystem_file_read_line(ZEND_THIS, intern, true); } if (intern->u.file.current_line && (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || Z_ISUNDEF(intern->u.file.current_zval))) { RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len); @@ -2214,7 +2220,7 @@ PHP_METHOD(SplFileObject, next) spl_filesystem_file_free_line(intern); if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) { - spl_filesystem_file_read_line(ZEND_THIS, intern); + spl_filesystem_file_read_line(ZEND_THIS, intern, true); } intern->u.file.current_line_num++; } /* }}} */ @@ -2332,7 +2338,7 @@ PHP_METHOD(SplFileObject, fgetcsv) } } - if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value) == FAILURE) { + if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value, true) == FAILURE) { RETURN_FALSE; } } @@ -2713,7 +2719,7 @@ PHP_METHOD(SplFileObject, seek) spl_filesystem_file_rewind(ZEND_THIS, intern); for (i = 0; i < line_pos; i++) { - if (spl_filesystem_file_read_line(ZEND_THIS, intern) == FAILURE) { + if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == FAILURE) { return; } } @@ -2733,8 +2739,12 @@ PHP_METHOD(SplFileObject, __toString) CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern); - if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) { - spl_filesystem_file_read_line(ZEND_THIS, intern); + if (!intern->u.file.current_line) { + ZEND_ASSERT(Z_ISUNDEF(intern->u.file.current_zval)); + zend_result result = spl_filesystem_file_read_line(ZEND_THIS, intern, false); + if (UNEXPECTED(result != SUCCESS)) { + RETURN_THROWS(); + } } RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len); diff --git a/ext/spl/tests/gh13685.phpt b/ext/spl/tests/gh13685.phpt new file mode 100644 index 0000000000000..0f679d0e93fc9 --- /dev/null +++ b/ext/spl/tests/gh13685.phpt @@ -0,0 +1,52 @@ +--TEST-- +GH-13685 (Unexpected null pointer in zend_string.h) +--FILE-- +<?php + +$contents = <<<EOS +"A", "B", "C" +"D", "E", "F" +EOS; + +echo "--- Directly call fgetcsv ---\n"; + +$file = new SplTempFileObject; +$file->fwrite($contents); +$file->rewind(); +while (($data = $file->fgetcsv(',', '"', ''))) { + var_dump((string) $file); +} +try { + var_dump((string) $file); +} catch (Exception $e) { + echo $e->getMessage(), "\n"; +} + +echo "--- Use csv control ---\n"; + +$file = new SplTempFileObject; +$file->fwrite($contents); +$file->rewind(); +$file->setFlags(SplFileObject::READ_CSV); +$file->setCsvControl(',', '"', ''); +foreach ($file as $row) { + var_dump((string) $file); +} +try { + var_dump((string) $file); +} catch (Exception $e) { + echo $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +--- Directly call fgetcsv --- +string(14) ""A", "B", "C" +" +string(13) ""D", "E", "F"" +Cannot read from file php://temp +--- Use csv control --- +string(14) ""A", "B", "C" +" +string(13) ""D", "E", "F"" +Cannot read from file php://temp

« previous php.cvs (#127761) next »