]> granicus.if.org Git - php/commitdiff
Implement feature request #50283 (allow base in gmp_strval to use full range: 2 to...
authorDavid Soria Parra <dsp@php.net>
Tue, 24 Nov 2009 13:33:35 +0000 (13:33 +0000)
committerDavid Soria Parra <dsp@php.net>
Tue, 24 Nov 2009 13:33:35 +0000 (13:33 +0000)
NEWS
ext/gmp/gmp.c
ext/gmp/tests/bug50283.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index c8f18c8970265f77b0c883c251804c1d379831f0..b60486c616a71985f17494057d05b92e9c17fc7b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,8 @@ PHP                                                                        NEWS
 
 - Fixed error_log() to be binary safe when using message_type 3. (Jani)
 
+- Implement feature request #50283 (allow base in gmp_strval to use full
+  range: 2 to 62, and -2 to -36). (David Soria Parra)
 - Fixed bug #50282 (xmlrpc_encode_request() changes object into array in 
   calling function). (Felipe)
 - Fixed bug #50219 (soap call Segmentation fault on a redirected url).
index 406862ebaac406cc0e49bd906520252f95c2b928..075f67499ff2b15affd6451736b368ac51ce2572 100644 (file)
@@ -817,12 +817,12 @@ ZEND_FUNCTION(gmp_strval)
                        break;
        }
 
-       if (base < 2 || base > 36) {
+       if ((base < 2 && base > -2) || base > 36 || base < -36) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %d", base);
                RETURN_FALSE;
        }
 
-       num_len = mpz_sizeinbase(*gmpnum, base);
+       num_len = mpz_sizeinbase(*gmpnum, abs(base));
        out_string = emalloc(num_len+2);
        if (mpz_sgn(*gmpnum) < 0) {
                num_len++;
diff --git a/ext/gmp/tests/bug50283.phpt b/ext/gmp/tests/bug50283.phpt
new file mode 100644 (file)
index 0000000..4842c00
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Feature Request #50283 (allow base in gmp_strval to use full range: 2 to 62, and -2 to -36)
+--SKIPIF--
+<?php if (!extension_loaded("gmp")) print "skip"; ?>
+--FILE--
+<?php
+$a = gmp_init("0x41682179fbf5");
+printf("Decimal: %s, 36-based: %s\n", gmp_strval($a), gmp_strval($a,-36));
+printf("Decimal: %s, 36-based: %s\n", gmp_strval($a), gmp_strval($a,36));
+printf("Decimal: %s, 36-based: %s\n", gmp_strval($a), gmp_strval($a,-1));
+printf("Decimal: %s, 36-based: %s\n", gmp_strval($a), gmp_strval($a,1));
+printf("Decimal: %s, 36-based: %s\n", gmp_strval($a), gmp_strval($a,-37));
+printf("Decimal: %s, 36-based: %s\n", gmp_strval($a), gmp_strval($a,37));
+?>
+--EXPECTF--
+Decimal: 71915494046709, 36-based: PHPISCOOL
+Decimal: 71915494046709, 36-based: phpiscool
+
+Warning: gmp_strval(): Bad base for conversion: -1 in %s on line 5
+Decimal: 71915494046709, 36-based: 
+
+Warning: gmp_strval(): Bad base for conversion: 1 in %s on line 6
+Decimal: 71915494046709, 36-based: 
+
+Warning: gmp_strval(): Bad base for conversion: -37 in %s on line 7
+Decimal: 71915494046709, 36-based: 
+
+Warning: gmp_strval(): Bad base for conversion: 37 in %s on line 8
+Decimal: 71915494046709, 36-based: