php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1335 modulus operator overflows
Submitted: 1999-04-20 14:27 UTC Modified: 1999-04-20 15:37 UTC
From: cclarke at netcom dot com Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.7 OS: Linux
Private report: No CVE-ID: None
 [1999-04-20 14:27 UTC] cclarke at netcom dot com
Sample code:
    $number = 116503231466;
  
    print $number / 1000;
    print $number % 1000;

The  first print statement will print:  1150321.466
The second print statement will print: -648
instead of the expected 466.

Looking at the code in operators.c, div_function calls
convert_string_to_number() which promotes $number to
double because strtol() returns ERANGE.  Then div_function
then performs a floating point divide.

mod_function, however calls convert_to_long, which
calls convert_to_long_base(), which ignores the ERANGE
error, returning LONG_MIN on Linux.

I think mod_function() should be using convert_string_to_number(), and case out the code depending
on the data type of the numbers.  

I don't know what else uses convert_to_long() and/or convert_to_long_base(), so I don't know if these functions need to be returning a range error, though silently returning LONG_MIN seems dangerous.

I'm happy to take a shot at rewriting mod_function() if that would be useful.
		Thanks,
			-Cam

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-04-20 15:37 UTC] andi
PHP 3 supports the BC math library functions of arbitrary precision operations, so you're best off using them as
you seem to be dealing with very large numbers.
The mod function is called bc_mod(). Check out the online man pages for more information.
In any case, there might be better ways of doing the built-in math operations. It might still be a good idea to take
a look at them, but we have to be careful not to make incompatible changes. Some places already automatically
use the BC math library in case the operations overflow and the BC library is compiled into PHP
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC