xref: /PHP-8.3/ext/gmp/tests/bug80560.phpt (revision e9f783fc)
1--TEST--
2Bug #80560: Strings containing only a base prefix return 0 object
3--EXTENSIONS--
4gmp
5--FILE--
6<?php
7
8$functions1 = [
9    'gmp_init',
10    'gmp_export',
11    'gmp_intval',
12    'gmp_strval',
13    'gmp_neg',
14    'gmp_abs',
15    'gmp_fact',
16    'gmp_sqrt',
17    'gmp_sqrtrem',
18    'gmp_perfect_square',
19    'gmp_perfect_power',
20    'gmp_prob_prime',
21    'gmp_sign',
22    'gmp_random_seed',
23    'gmp_popcount',
24    'gmp_com',
25    'gmp_nextprime',
26];
27$functions1_need_int_2 = [
28    'gmp_testbit',
29    'gmp_scan0',
30    'gmp_scan1',
31    'gmp_binomial',
32    'gmp_root',
33    'gmp_rootrem',
34    'gmp_pow',
35];
36$functions2 = [
37    'gmp_add',
38    'gmp_sub',
39    'gmp_mul',
40    'gmp_div',
41    'gmp_div_q',
42    'gmp_div_r',
43    'gmp_div_qr',
44    'gmp_divexact',
45    'gmp_mod',
46    'gmp_gcd',
47    'gmp_gcdext',
48    'gmp_lcm',
49    'gmp_invert',
50    'gmp_jacobi',
51    'gmp_legendre',
52    'gmp_kronecker',
53    'gmp_cmp',
54    'gmp_random_range',
55    'gmp_and',
56    'gmp_or',
57    'gmp_xor',
58    'gmp_hamdist',
59];
60$functions3 = [
61    'gmp_powm',
62];
63
64echo 'Explicit base with gmp_init:', \PHP_EOL;
65echo 'Hexadecimal', \PHP_EOL;
66try {
67    var_dump(gmp_init('0X', 16));
68} catch (\ValueError $e) {
69    echo $e->getMessage(), \PHP_EOL;
70}
71try {
72    var_dump(gmp_init('0x', 16));
73} catch (\ValueError $e) {
74    echo $e->getMessage(), \PHP_EOL;
75}
76
77echo 'Binary', \PHP_EOL;
78try {
79    var_dump(gmp_init('0B', 2));
80} catch (\ValueError $e) {
81    echo $e->getMessage(), \PHP_EOL;
82}
83try {
84    var_dump(gmp_init('0b', 2));
85} catch (\ValueError $e) {
86    echo $e->getMessage(), \PHP_EOL;
87}
88
89echo 'Fuzzing gmp functions:', \PHP_EOL;
90foreach ($functions1 as $function) {
91    try {
92        $function('0B');
93        echo $function, ' failed with 0B', \PHP_EOL;
94    } catch (\ValueError) { }
95    try {
96        $function('0b');
97        echo $function, ' failed with 0b', \PHP_EOL;
98    } catch (\ValueError) { }
99    try {
100        $function('0X');
101        echo $function, ' failed with 0X', \PHP_EOL;
102    } catch (\ValueError) { }
103    try {
104        $function('0x');
105        echo $function, ' failed with 0x', \PHP_EOL;
106    } catch (\ValueError) { }
107}
108foreach ($functions1_need_int_2 as $function) {
109    try {
110        $function('0B', 1);
111        echo $function, ' failed with 0B', \PHP_EOL;
112    } catch (\ValueError) { }
113    try {
114        $function('0b', 1);
115        echo $function, ' failed with 0b', \PHP_EOL;
116    } catch (\ValueError) { }
117    try {
118        $function('0X', 1);
119        echo $function, ' failed with 0X', \PHP_EOL;
120    } catch (\ValueError) { }
121    try {
122        $function('0x', 1);
123        echo $function, ' failed with 0x', \PHP_EOL;
124    } catch (\ValueError) { }
125}
126foreach ($functions2 as $function) {
127    try {
128        $function('0B', 1);
129        echo $function, ' arg 1 failed with 0B', \PHP_EOL;
130    } catch (\ValueError) { }
131    try {
132        $function('0b', 1);
133        echo $function, ' arg 1 failed with 0b', \PHP_EOL;
134    } catch (\ValueError) { }
135    try {
136        $function('0X', 1);
137        echo $function, ' arg 1 failed with 0X', \PHP_EOL;
138    } catch (\ValueError) { }
139    try {
140        $function('0x', 1);
141        echo $function, ' arg 1 failed with 0x', \PHP_EOL;
142    } catch (\ValueError) { }
143    try {
144        $function(1, '0B');
145        echo $function, ' arg 2 failed with 0B', \PHP_EOL;
146    } catch (\ValueError) { }
147    try {
148        $function(1, '0b');
149        echo $function, ' arg 2 failed with 0b', \PHP_EOL;
150    } catch (\ValueError) { }
151    try {
152        $function(1, '0X');
153        echo $function, ' arg 2 failed with 0X', \PHP_EOL;
154    } catch (\ValueError) { }
155    try {
156        $function(1, '0x');
157        echo $function, ' arg 2 failed with 0x', \PHP_EOL;
158    } catch (\ValueError) { }
159}
160foreach ($functions3 as $function) {
161    try {
162        $function('0B', 1, 1);
163        echo $function, ' arg 1 failed with 0B', \PHP_EOL;
164    } catch (\ValueError) { }
165    try {
166        $function('0b', 1, 1);
167        echo $function, ' arg 1 failed with 0b', \PHP_EOL;
168    } catch (\ValueError) { }
169    try {
170        $function('0X', 1, 1);
171        echo $function, ' arg 1 failed with 0X', \PHP_EOL;
172    } catch (\ValueError) { }
173    try {
174        $function('0x', 1, 1);
175        echo $function, ' arg 1 failed with 0x', \PHP_EOL;
176    } catch (\ValueError) { }
177    try {
178        $function(1, '0B', 1);
179        echo $function, ' arg 2 failed with 0B', \PHP_EOL;
180    } catch (\ValueError) { }
181    try {
182        $function(1, '0b', 1);
183        echo $function, ' arg 2 failed with 0b', \PHP_EOL;
184    } catch (\ValueError) { }
185    try {
186        $function(1, '0X', 1);
187        echo $function, ' arg 2 failed with 0X', \PHP_EOL;
188    } catch (\ValueError) { }
189    try {
190        $function(1, '0x', 1);
191        echo $function, ' arg 2 failed with 0x', \PHP_EOL;
192    } catch (\ValueError) { }
193    try {
194        $function(1, 1, '0B');
195        echo $function, ' arg 3 failed with 0B', \PHP_EOL;
196    } catch (\ValueError) { }
197    try {
198        $function(1, 1, '0b');
199        echo $function, ' arg 3 failed with 0b', \PHP_EOL;
200    } catch (\ValueError) { }
201    try {
202        $function(1, 1, '0X');
203        echo $function, ' arg 3 failed with 0X', \PHP_EOL;
204    } catch (\ValueError) { }
205    try {
206        $function(1, 1, '0x');
207        echo $function, ' arg 3 failed with 0x', \PHP_EOL;
208    } catch (\ValueError) { }
209}
210
211echo "Done\n";
212?>
213--EXPECT--
214Explicit base with gmp_init:
215Hexadecimal
216gmp_init(): Argument #1 ($num) is not an integer string
217gmp_init(): Argument #1 ($num) is not an integer string
218Binary
219gmp_init(): Argument #1 ($num) is not an integer string
220gmp_init(): Argument #1 ($num) is not an integer string
221Fuzzing gmp functions:
222Done
223