Skip to content

Commit 86da63d

Browse files
committed
ext/gmp: Fix GMP accepting integer strings with NUL bytes
1 parent d79a32d commit 86da63d

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

ext/gmp/gmp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,15 @@ static zend_result convert_zstr_to_gmp(mpz_t gmp_number, const zend_string *val,
644644
const char *num_str = ZSTR_VAL(val);
645645
bool skip_lead = false;
646646

647+
if (UNEXPECTED(zend_str_has_nul_byte(val))) {
648+
if (arg_pos == 0) {
649+
zend_value_error("Number is not an integer string");
650+
} else {
651+
zend_argument_value_error(arg_pos, "is not an integer string");
652+
}
653+
return FAILURE;
654+
}
655+
647656
size_t num_len = ZSTR_LEN(val);
648657
while (isspace((unsigned char)*num_str)) {
649658
++num_str;

ext/gmp/tests/gmp_null_bytes.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GMP rejects integer strings containing null bytes
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
8+
$tests = [
9+
'gmp_init' => fn() => gmp_init("123\0abc"),
10+
'gmp_init prefix' => fn() => gmp_init("0x10\0ff"),
11+
'gmp_add' => fn() => gmp_add("123\0abc", 1),
12+
'constructor' => fn() => new GMP("123\0abc"),
13+
];
14+
15+
foreach ($tests as $label => $test) {
16+
try {
17+
$test();
18+
} catch (ValueError $e) {
19+
echo $label, ": ", $e->getMessage(), PHP_EOL;
20+
}
21+
}
22+
23+
?>
24+
--EXPECT--
25+
gmp_init: gmp_init(): Argument #1 ($num) is not an integer string
26+
gmp_init prefix: gmp_init(): Argument #1 ($num) is not an integer string
27+
gmp_add: gmp_add(): Argument #1 ($num1) is not an integer string
28+
constructor: GMP::__construct(): Argument #1 ($num) is not an integer string

0 commit comments

Comments
 (0)