php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47422 modulus operator returns incorrect results on 64 bit linux
Submitted: 2009-02-17 12:36 UTC Modified: 2009-02-17 14:22 UTC
From: d_kelsey at uk dot ibm dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2CVS-2009-02-17 (CVS) OS: Linux 64Bit
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: d_kelsey at uk dot ibm dot com
New email:
PHP Version: OS:

 

 [2009-02-17 12:36 UTC] d_kelsey at uk dot ibm dot com
Description:
------------
on a 64bit system the following 
var_dump(1%4294967295);
should return 1, but returns 0. 
The problem is in the 
ZEND_API int mod_function(...) in zend_operators.c

A call is made to abs which expects a 32bit int and returns a 32bit int, but 4294967295 is > 32 bits in size so the abs call returns an incorrect value.

A proposed fix is to change the line
"if (abs(op2->value.lval) == 1) {"
to
"if (op2->value.lval == 1 || op2->value.lval == -1) {"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-17 14:22 UTC] mattwil@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Thanks. Actually, just -1 needs to be checked there anyway, since only when doing LONG_MIN % -1 (in PHP, "(-PHP_INT_MAX - 1) % -1;") would there be a crash/error (which that's there to catch of course).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Feb 03 18:01:32 2025 UTC