From:             erik at ekriirke dot com
Operating system: LAMP, WAMP, WIMP
PHP version:      5.2.8
PHP Bug Type:     MySQLi related
Bug description:  Bitwise operation number overflow

Description:
------------
This happens the same on Linux (RHE5) and Windows (Server2K,XPPro)
Differing versions of MySQL as well (5.0.51a, 5.1.11)
Using either MySQLi or MySQL extensions

I have a table with an UNSIGNED INTEGER column.  The value in the column
has a value where MSB (31) is set (0x80000000 and higher).

Selecting this value into an array via either mysql_fetch_assoc,
mysql_fetch_row (and mysqli variant) stores the value correctly as
displayed by either a print_r or echo'ing directly.

Should I perform a math operation (+-*/) the result is correct.

Should I perform a bitwise operation  or function the answers are wrong. 
It appears doing so internally changes the selected value to 0x7FFFFFFF no
matter its original value as long as bit 31 is set.

However, if I perform math on the value THEN a bitwise operation, the
result is correct.

Now, I know the MSB is a touchy subject in 31bit math, but there is
inconsistency here.

Reproduce code:
---------------
$q=mysql_query("SELECT val FROM test"); // val is an UNSIGED INT
containing a value with MSB set.  Ex:  2147488308 (0x80001234)
$a=mysql_fetch_assoc($q);
echo $a['val'],"<br>\n";    //2147488308 - correct
echo dechex($a['val']),"<br>\n";    //7fffffff - wrong

echo $a['val']+0,"<br>\n";    //2147488308 - correct
echo $a['val']+1,"<br>\n";    //2147488309 - correct

echo $a['val']|0,"<br>\n";    //2147483647 - wrong
echo dechex($a['val']+0),"<br>\n";    //80001234 - correct

echo $a['val']&1,"<br>\n";    //1 - wrong
echo ($a['val']+0)&1,"<br>\n";    //0 - correct

/*** Workaround ***/
$a['val']+=0;  //as long as some kind of math was performed it functions
correctly afterward

echo $a['val']|0,"<br>\n";    //2147488308 or -2147478988 - correct
echo $a['val']&1,"<br>\n";    //0 - correct
echo dechex($a['val']),"<br>\n";    //80001234 - correct

Expected result:
----------------
2147488308
80001234
2147488308
2147488309
-2147478988
80001234
0
0
-2147478988
0
80001234

Actual result:
--------------
2147488308
7fffffff
2147488308
2147488309
2147483647
80001234
1
0
-2147478988
0
80001234

-- 
Edit bug report at http://bugs.php.net/?id=47092&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=47092&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=47092&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=47092&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=47092&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=47092&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=47092&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=47092&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=47092&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=47092&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=47092&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=47092&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=47092&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=47092&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=47092&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=47092&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=47092&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=47092&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=47092&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=47092&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=47092&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=47092&r=mysqlcfg

Reply via email to