php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14134 Literal -2147483648 should be an int, not a double.
Submitted: 2001-11-20 02:34 UTC Modified: 2003-04-25 11:25 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (66.7%)
From: at217 at chebucto dot ns dot ca Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4.0.6 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2001-11-20 02:34 UTC] at217 at chebucto dot ns dot ca
When assigning the value -2147483648 to a variable, the variable becomes a double, even though the value is within the range of integers. In fact, assigning -2147483647 - 1 works, as does using intval("-2147483648").

The following script demonstrates, and can be accessed at <http://www.randomsample.org/tests/test-int-min.php>.

<?php
$int_max = 2147483647;
$int_min = -2147483648;
$int_min_alt = -2147483647 - 1;
?>
<html><head><title>Test INT_MIN</title></head>
<body>
<p>Type of "2147483647": <?php echo gettype( $int_max ); ?>
   (value = <?php echo $int_max; ?>)</p>
<p>Type of "-2147483648": <?php echo gettype( $int_min ); ?>
   (value = <?php echo $int_min; ?>)</p>
<p>Type of "-2147483647 - 1": <?php echo gettype( $int_min_alt ); ?>
   (value = <?php echo $int_min_alt; ?>)</p>
</body>
</html>

This is an ISP-provided build, but the configure line from phpinfo() is:
'./configure' '--with-config-file-path=/etc' '--enable-safe-mode=yes' '--enable-force-cgi-redirect=yes' '--with-gdbm' '--with-mysql' '--with-exec-dir=/www/cgi-bin' '--bindir=/www/cgi-bin' '--enable-ftp' '--enable-trans-sid' '--with-gd=/usr' '--with-zlib=/usr' '--with-jpeg-dir=/usr' '--enable-gd-native-ttf' '--with-png-dir=/usr' '--with-ttf=/usr/lib'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-26 02:37 UTC] derick@php.net
Ok, as I have no time to look into a fix (if it's even do-able), I'll explain here why it happens:

The parser embedded in PHP, does not take "-2147483648" as a string to convert (with strtol) to a unsigned integer, but it instead does this:

1. it tries to convert "2147483648" to a unsigned integer (which fails)
2. it tries to substract this from 0, ie. 0 - 2147483648.

Because the conversion to a unsigned long fails in the first step, it gets converted to a floating point number, which is substracted from 0.

A way to solve this can be to add constants for these outher limits, ie. INT_MIN (for -2147483648) and INT_MAX (for 2147483647).

Derick
 [2001-11-26 14:35 UTC] at217 at chebucto dot ns dot ca
Derick,

Can you tell me where this code is so I can have a look? I'm a little confused about your comment about conversion to unsigned integers. First, I believe strtol converts to a signed integer (and, as used in intval, it does work correctly). But 2147483648 actually is a valid unsigned long. If the conversion is failing, it must be trying to convert to a signed integer.

As for the workaround, creating constants INT_MIN and INT_MAX is exactly what I did, although you still have to be careful when creating INT_MIN. It must be something like:

define( "INT_MIN", -2147483647 - 1 );
define( "INT_MAX", 2147483647 );

 [2001-11-26 14:42 UTC] derick@php.net
s/unsigned/signed in my text... I was sleeping I think :)

Derick
 [2001-12-12 15:47 UTC] derick@php.net
Changing this into a suspended scripting engine problem, as the bug lies in Zend. It is possible to change te parsing, but this will need a lot of adjustments in it. That's why I'm suspending it too.

Derick


 [2003-04-25 11:25 UTC] sniper@php.net
Just one oddity we have to live with:

http://www.php.net/manual/en/language.types.integer.php

 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC