Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions ext/gmp/tests/gmp_null_bytes.phpt
Original file line number Diff line number Diff line change
@@ -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
Loading