php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28800 Strings beginning with "inf" improperly converted
Submitted: 2004-06-15 22:26 UTC Modified: 2004-07-16 04:24 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (100.0%)
From: pmichaud at pobox dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.3.6, 5.0.0RC2 OS: Linux
Private report: No CVE-ID:
 [2004-06-15 22:26 UTC] pmichaud at pobox dot com
Description:
------------
PHP seems to incorrectly convert strings beginning with "inf" to INF (infinity).  This includes converting strings such as "info" and "inflammable".  Surprisingly, the strings "inf" and "infinity" do *not* convert to infinity but correctly become zeros.  A script that illustrates the problem is given below:

This is very annoying because one cannot simply do a test for a positive number with code such as

  if ($x>0) { echo "$x is positive"; }

because if $x is any string beginning with "inf" (but not "inf" or "infinity") it is treated as a positive number.  

Pm

Reproduce code:
---------------
Here's a script that illustrates the problem:

<?php
  header("Content-type: text/plain");
  $a = "into";
  $b = "info";
  $c = "inf";
  $d = "infinity";
  $e = "infin";
  $f = "inflammable";

  echo "a: $a == ",$a+0,"\n";   # outputs 0 (correct)
  echo "b: $b == ",$b+0,"\n";   # outputs INF, incorrect!
  echo "c: $c == ",$c+0,"\n";   # outputs 0 (?!)
  echo "d: $d == ",$d+0,"\n";   # outputs 0 (?!)
  echo "e: $e == ",$e+0,"\n";   # outputs INF, incorrect!
  echo "f: $f == ",$f+0,"\n";   # outputs INF, incorrect!
?>


Expected result:
----------------
a: into == 0
b: info == 0
c: inf == 0
d: infinity == 0
e: infin == 0
f: inflammable == 0

Actual result:
--------------
a: into == 0
b: info == INF
c: inf == 0
d: infinity == 0
e: infin == INF
f: inflammable == INF

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-20 04:49 UTC] pmichaud at pobox dot com
Changed bug category.
 [2004-06-20 05:05 UTC] pmichaud at pobox dot com
Changed PHP version number to note reports of bug in many versions of PHP.
 [2004-06-20 05:08 UTC] pmichaud at pobox dot com
Changed PHP version number to note bug exists in 5.0.0RC2.
 [2004-06-28 12:35 UTC] rasmus@php.net
Verified that this is indeed the case.  Since we are using strtol() internally here, I would actually expect INF for both your "inf" and "infinity" cases.  The strtol() spec says that this should be the result.  And a quick local check verifies this, but the others definitely shouldn't be returning INF.  It looks like we are not checking the end pointer returned from strtol() at all in any of our conversion functions.  We need something like this:

        l = strtol(s, &endp, 0);
        if (s != endp && *endp == `\0') { 
             ... valid conversion ...
        }



 [2004-07-16 04:24 UTC] iliaa@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.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 00:01:21 2014 UTC