Bug #55393 [Com]: base_convert makes negative numbers positive
| From: | ffeast at gmail dot com | Date: | Sun, 14 Dec 2014 18:32:05 +0000 |
| Subject: | Bug #55393 [Com]: base_convert makes negative numbers positive | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189060@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55393&edit=1
ID: 55393
Comment by: ffeast at gmail dot com
Reported by: phpbugs at ch dot pkts dot ca
Summary: base_convert makes negative numbers positive
Status: Open
Type: Bug
Package: Unknown/Other Function
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Hi
There's a simpler example:
php -r 'print(base_convert(-5, 10, 2));'
101
So '-' is omitted.
$ php -v
PHP 5.5.9-1ubuntu4.4 (cli) (built: Sep 4 2014 06:57:30)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
uname -a
Linux Oleg 3.13.0-34-generic #60-Ubuntu SMP Wed Aug 13 15:49:09 UTC 2014 i686 i686 i686 GNU/Linux
This is why it happens:
https://github.com/php/php-src/blob/php-5.5.9/ext/standard/math.c#L869
any char except '0'..'9', 'a'..'z',
'A'..'Z' is simp
ly left out without warnings :-(
Previous Comments:
------------------------------------------------------------------------
[2011-08-10 18:20:58] phpbugs at ch dot pkts dot ca
Description:
------------
I'd created a fake email code fragment:
e-mail: foo.<?php echo
base_convert(time(),10,36).".".base_convert(ip2long($_SERVER['REMOTE_ADDR']),10,36)
?>@mydomain.com
However, long2ip(base_convert("cxcapb",36,10)) gave the wrong answer (should be
209.105.205.209, got 46.150.50.47)
It turns out that base_convert(...,10,36) should have returned -cxcapb instead of cxcapb, since
ip2long("209.105.205.209") = -781595183 and ip2long("46.150.50.47") = 781595183.
At this point, I'm not sure if there's legions of programs that work around this bug and
would be broken by a fix.
Test script:
---------------
$a=base_convert(-781595183,10,36);
if (base_convert($a,36,10) != -781595183) { echo "fail\n"; } else { echo
"success\!"; }
Expected result:
----------------
success!
Actual result:
--------------
fail
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55393&edit=1