[php-src] master: ext/gmp: Fix leading whitespace before explicit octal prefix

From: Date: Tue, 23 Apr 2024 22:39:57 +0000
Subject: [php-src] master: ext/gmp: Fix leading whitespace before explicit octal prefix
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-128227@lists.php.net to get a copy of this message
Author: Gina Peter Banyard (Girgias) Date: 2024-04-23T23:39:48+01:00 Commit: https://github.com/php/php-src/commit/13a5a8126edf6db8dc0298097c65a3ace4214767 Raw diff: https://github.com/php/php-src/commit/13a5a8126edf6db8dc0298097c65a3ace4214767.diff ext/gmp: Fix leading whitespace before explicit octal prefix Changed paths: A ext/gmp/tests/gmp_init_leading_spaces.phpt M ext/gmp/gmp.c Diff: diff --git a/ext/gmp/gmp.c b/ext/gmp/gmp.c index dddd875dac37..91b65840c3c5 100644 --- a/ext/gmp/gmp.c +++ b/ext/gmp/gmp.c @@ -583,7 +583,13 @@ 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 (ZSTR_LEN(val) >= 2 && num_str[0] == '0') { + size_t num_len = ZSTR_LEN(val); + while (isspace(*num_str)) { + ++num_str; + --num_len; + } + + if (num_len >= 2 && num_str[0] == '0') { if ((base == 0 || base == 16) && (num_str[1] == 'x' || num_str[1] == 'X')) { base = 16; skip_lead = true; diff --git a/ext/gmp/tests/gmp_init_leading_spaces.phpt b/ext/gmp/tests/gmp_init_leading_spaces.phpt new file mode 100644 index 000000000000..4199c277917b --- /dev/null +++ b/ext/gmp/tests/gmp_init_leading_spaces.phpt @@ -0,0 +1,61 @@ +--TEST-- +gmp_init() with various integer notations and leading spaces +--EXTENSIONS-- +gmp +--FILE-- +<?php + +var_dump(gmp_init(" 0x16")); +var_dump(gmp_init(" 0X16")); +var_dump(gmp_init(" 0o16")); +var_dump(gmp_init(" 0o16")); +var_dump(gmp_init(" 016")); +var_dump(gmp_init(" 016")); +var_dump(gmp_init(" 0b11")); +var_dump(gmp_init(" 0b11")); +var_dump(gmp_init(" 0")); +try { + var_dump(gmp_init(" ")); +} catch (\Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; +} + +?> +--EXPECT-- +object(GMP)#1 (1) { + ["num"]=> + string(2) "22" +} +object(GMP)#1 (1) { + ["num"]=> + string(2) "22" +} +object(GMP)#1 (1) { + ["num"]=> + string(2) "14" +} +object(GMP)#1 (1) { + ["num"]=> + string(2) "14" +} +object(GMP)#1 (1) { + ["num"]=> + string(2) "14" +} +object(GMP)#1 (1) { + ["num"]=> + string(2) "14" +} +object(GMP)#1 (1) { + ["num"]=> + string(1) "3" +} +object(GMP)#1 (1) { + ["num"]=> + string(1) "3" +} +object(GMP)#1 (1) { + ["num"]=> + string(1) "0" +} +ValueError: gmp_init(): Argument #1 ($num) is not an integer string

« previous php.cvs (#128227) next »