]> granicus.if.org Git - php/commitdiff
Fixed bug #50175
authorNikita Popov <nikic@php.net>
Tue, 2 Sep 2014 17:04:55 +0000 (19:04 +0200)
committerNikita Popov <nikic@php.net>
Tue, 2 Sep 2014 17:04:55 +0000 (19:04 +0200)
0x and 0b prefix is now only handled if either no base is given
or if the base is 16 (0x) or 2 (0b). Always handling it is incorrect
because 0x and 0b are perfectly valid numbers in other bases.

NEWS
ext/gmp/gmp.c
ext/gmp/tests/bug50175.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 1034a5d93f5b82e7acff3cf2b6d67cf33e1cf731..f78969c233bc2baf6bb2d0fd9cea81effb8d8b4d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -22,6 +22,8 @@ PHP                                                                        NEWS
 - GMP:
   . Fixed bug #67917 (Using GMP objects with overloaded operators can cause
     memory exhaustion). (Nikita)
+  . Fixed bug #50175 (gmp_init() results 0 on given base and number starting
+    with 0x or 0b). (Nikita)
   . Implemented gmp_import() and gmp_export(). (Leigh, Nikita)
 
 - MySQLi:
index bf0f4f3fc7fde7340e78d58710d94ae22d53ef4c..619f1c39c35b8738c28f7f93cedec13e6e2da293 100644 (file)
@@ -773,15 +773,13 @@ static int convert_to_gmp(mpz_t gmpnumber, zval *val, int base TSRMLS_DC)
                int skip_lead = 0;
                int ret;
 
-               if (Z_STRLEN_P(val) > 2) {
-                       if (numstr[0] == '0') {
-                               if (numstr[1] == 'x' || numstr[1] == 'X') {
-                                       base = 16;
-                                       skip_lead = 1;
-                               } else if (base != 16 && (numstr[1] == 'b' || numstr[1] == 'B')) {
-                                       base = 2;
-                                       skip_lead = 1;
-                               }
+               if (Z_STRLEN_P(val) > 2 && numstr[0] == '0') {
+                       if ((base == 0 || base == 16) && (numstr[1] == 'x' || numstr[1] == 'X')) {
+                               base = 16;
+                               skip_lead = 1;
+                       } else if ((base == 0 || base == 2) && (numstr[1] == 'b' || numstr[1] == 'B')) {
+                               base = 2;
+                               skip_lead = 1;
                        }
                }
 
diff --git a/ext/gmp/tests/bug50175.phpt b/ext/gmp/tests/bug50175.phpt
new file mode 100644 (file)
index 0000000..0998e02
--- /dev/null
@@ -0,0 +1,18 @@
+--TEST--
+Bug #50175: gmp_init() results 0 on given base and number starting with 0x or 0b
+--FILE--
+<?php
+
+var_dump(gmp_init('0bcd', 16));
+var_dump(gmp_init('0xyz', 36));
+
+?>
+--EXPECTF--
+object(GMP)#%d (1) {
+  ["num"]=>
+  string(4) "3021"
+}
+object(GMP)#%d (1) {
+  ["num"]=>
+  string(5) "44027"
+}