Skip to content

Commit 13a5a81

Browse files
committed
ext/gmp: Fix leading whitespace before explicit octal prefix
1 parent 4719ef2 commit 13a5a81

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

ext/gmp/gmp.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,13 @@ static zend_result convert_zstr_to_gmp(mpz_t gmp_number, const zend_string *val,
583583
const char *num_str = ZSTR_VAL(val);
584584
bool skip_lead = false;
585585

586-
if (ZSTR_LEN(val) >= 2 && num_str[0] == '0') {
586+
size_t num_len = ZSTR_LEN(val);
587+
while (isspace(*num_str)) {
588+
++num_str;
589+
--num_len;
590+
}
591+
592+
if (num_len >= 2 && num_str[0] == '0') {
587593
if ((base == 0 || base == 16) && (num_str[1] == 'x' || num_str[1] == 'X')) {
588594
base = 16;
589595
skip_lead = true;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
gmp_init() with various integer notations and leading spaces
3+
--EXTENSIONS--
4+
gmp
5+
--FILE--
6+
<?php
7+
8+
var_dump(gmp_init(" 0x16"));
9+
var_dump(gmp_init(" 0X16"));
10+
var_dump(gmp_init(" 0o16"));
11+
var_dump(gmp_init(" 0o16"));
12+
var_dump(gmp_init(" 016"));
13+
var_dump(gmp_init(" 016"));
14+
var_dump(gmp_init(" 0b11"));
15+
var_dump(gmp_init(" 0b11"));
16+
var_dump(gmp_init(" 0"));
17+
try {
18+
var_dump(gmp_init(" "));
19+
} catch (\Throwable $e) {
20+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
21+
}
22+
23+
?>
24+
--EXPECT--
25+
object(GMP)#1 (1) {
26+
["num"]=>
27+
string(2) "22"
28+
}
29+
object(GMP)#1 (1) {
30+
["num"]=>
31+
string(2) "22"
32+
}
33+
object(GMP)#1 (1) {
34+
["num"]=>
35+
string(2) "14"
36+
}
37+
object(GMP)#1 (1) {
38+
["num"]=>
39+
string(2) "14"
40+
}
41+
object(GMP)#1 (1) {
42+
["num"]=>
43+
string(2) "14"
44+
}
45+
object(GMP)#1 (1) {
46+
["num"]=>
47+
string(2) "14"
48+
}
49+
object(GMP)#1 (1) {
50+
["num"]=>
51+
string(1) "3"
52+
}
53+
object(GMP)#1 (1) {
54+
["num"]=>
55+
string(1) "3"
56+
}
57+
object(GMP)#1 (1) {
58+
["num"]=>
59+
string(1) "0"
60+
}
61+
ValueError: gmp_init(): Argument #1 ($num) is not an integer string

0 commit comments

Comments
 (0)