[php-src] master: ext/gmp: Fix GMP accepting integer strings with NUL bytes (#22798)

From: Date: Sat, 18 Jul 2026 21:24:45 +0000
Subject: [php-src] master: ext/gmp: Fix GMP accepting integer strings with NUL bytes (#22798)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-140095@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Committer: GitHub (web-flow) Pusher: LamentXU123 Date: 2026-07-19T05:24:42+08:00 Commit: https://github.com/php/php-src/commit/988eb99c2c21d4ca682b86a3bcf25e883b065475 Raw diff: https://github.com/php/php-src/commit/988eb99c2c21d4ca682b86a3bcf25e883b065475.diff ext/gmp: Fix GMP accepting integer strings with NUL bytes (#22798) convert_zstr_to_gmp() passes PHP strings to mpz_set_str(), which treats input as a NUL-terminated C string. This allowed strings like "123\0abc" to be accepted as 123. Reject strings containing NUL bytes with zend_str_has_nul_byte() before calling into GMP, preserving the existing "not an integer string" error path. Changed paths: A ext/gmp/tests/gmp_null_bytes.phpt M ext/gmp/gmp.c Diff: diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index b05706e48297..a5cbdba3484e 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -644,6 +644,15 @@ static zend_result convert_zstr_to_gmp(mpz_t gmp_number, const zend_string *val, const char *num_str = ZSTR_VAL(val); bool skip_lead = false; + if (UNEXPECTED(zend_str_has_nul_byte(val))) { + if (arg_pos == 0) { + zend_value_error("Number is not an integer string"); + } else { + zend_argument_value_error(arg_pos, "is not an integer string"); + } + return FAILURE; + } + size_t num_len = ZSTR_LEN(val); while (isspace((unsigned char)*num_str)) { ++num_str; diff --git a/ext/gmp/tests/gmp_null_bytes.phpt b/ext/gmp/tests/gmp_null_bytes.phpt new file mode 100644 index 000000000000..1518d342dda2 --- /dev/null +++ b/ext/gmp/tests/gmp_null_bytes.phpt @@ -0,0 +1,28 @@ +--TEST-- +GMP rejects integer strings containing null bytes +--EXTENSIONS-- +gmp +--FILE-- +<?php + +$tests = [ + 'gmp_init' => fn() => gmp_init("123\0abc"), + 'gmp_init prefix' => fn() => gmp_init("0x10\0ff"), + 'gmp_add' => fn() => gmp_add("123\0abc", 1), + 'constructor' => fn() => new GMP("123\0abc"), +]; + +foreach ($tests as $label => $test) { + try { + $test(); + } catch (ValueError $e) { + echo $label, ": ", $e->getMessage(), PHP_EOL; + } +} + +?> +--EXPECT-- +gmp_init: gmp_init(): Argument #1 ($num) is not an integer string +gmp_init prefix: gmp_init(): Argument #1 ($num) is not an integer string +gmp_add: gmp_add(): Argument #1 ($num1) is not an integer string +constructor: GMP::__construct(): Argument #1 ($num) is not an integer string

« previous php.cvs (#140095) next »