Commit df2090a3933 for php.net

commit df2090a39330a9f482fdc1c9f33cde4af3aadf32
Author: Louis-Arnaud <la.catoire@gmail.com>
Date:   Wed Sep 2 08:28:59 2026 +0200

    ext/hash: report argument #2 ($filename) for null bytes in hash_file() (#23534)

    php_hash_do_hash() hardcodes argument index 1 in the isfilename branch,
    so hash_file() blames $algo for a null byte carried by $filename.
    hash_file() is the only caller passing isfilename = 1, and its filename
    is argument #2; php_hash_do_hash_hmac() already uses 2 for the same
    check.

    Co-authored-by: NickSdot <32384907+NickSdot@users.noreply.github.com>

diff --git a/ext/hash/hash.c b/ext/hash/hash.c
index d57ae149691..4a0b0c49437 100644
--- a/ext/hash/hash.c
+++ b/ext/hash/hash.c
@@ -361,7 +361,7 @@ static void php_hash_do_hash(
 	}
 	if (isfilename) {
 		if (zend_char_has_nul_byte(data, data_len)) {
-			zend_argument_value_error(1, "must not contain any null bytes");
+			zend_argument_value_error(2, "must not contain any null bytes");
 			RETURN_THROWS();
 		}
 		stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
diff --git a/ext/hash/tests/hash_file_error.phpt b/ext/hash/tests/hash_file_error.phpt
index a381d3bdd63..4ef84a62ddf 100644
--- a/ext/hash/tests/hash_file_error.phpt
+++ b/ext/hash/tests/hash_file_error.phpt
@@ -19,6 +19,13 @@
     echo $exception::class, ': ', $exception->getMessage(), "\n";
 }

+echo "\n-- Testing hash_file() function with a null byte in the filename --\n";
+try {
+    hash_file('md5', $filename . chr(0) . $filename);
+} catch (Throwable $e) {
+    echo $e::class, ': ', $e->getMessage(), "\n";
+}
+
 echo "\n-- Testing hash_file() function with a non-existent file --\n";
 var_dump(hash_file('md5', 'nonexistent.txt'));

@@ -36,6 +43,9 @@
 -- Testing hash_file() function with an unknown algorithm --
 ValueError: hash_file(): Argument #1 ($algo) must be a valid hashing algorithm

+-- Testing hash_file() function with a null byte in the filename --
+ValueError: hash_file(): Argument #2 ($filename) must not contain any null bytes
+
 -- Testing hash_file() function with a non-existent file --

 Warning: hash_file(): Failed to open stream: No such file or directory in %s on line %d