php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3443 sprintf pads a null-character with a negative float
Submitted: 2000-02-10 01:35 UTC Modified: 2002-09-30 17:33 UTC
From: greg at bah dot org Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.14 OS: RH Linux 6.1
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: greg at bah dot org
New email:
PHP Version: OS:

 

 [2000-02-10 01:35 UTC] greg at bah dot org
This is a really strange problem, but sprintf seems to be padding a null character to the end of a string when the string consists of a negative float. Here is a script that will reproduce the problem:

<?

$string = -122.440383;
$new_string = sprintf("%0.6f", $string);
echo "string = $string<br>\n";
echo "new_string = $new_string<br>\n";
for ($j=0;$j<strlen($new_string);$j++) {
    echo "letter #$j: " . ord($new_string[$j]) . "<br>\n";
}

?>

The output on php-3.0.14, php-3.0.13, php-4.0b3 looks like:
string = -122.440383
new_string = -122.440383
letter #0: 45
letter #1: 49
letter #2: 50
letter #3: 50
letter #4: 46
letter #5: 52
letter #6: 52
letter #7: 48
letter #8: 51
letter #9: 56
letter #10: 51
letter #11: 0

There is no null character at the end if the float is positive.

I noticed that php-3.0.12 doens't exhibit this behavior. The way I configured all these different versions of php was:

Configure command: ./configure '--with-mhash' '--with-mcrypt' '--disable-display-source' '--enable-debugger' '--enable-track-vars' '--enable-debug' '--with-xml' '--with-oracle' '--without-gd' '--with-apxs=../www/bin/apxs'

The one major difference I know between my versions of php-3.0.12 and the others is that 3.0.12 compiles against an older version of mcrypt and all these others compile against a newwr version. This is probably irrelevant because I have no idea what mcrypt libraries would have to do with sprintf, but I thought I'd mention it.

Thanks.

-Greg

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-30 17:33 UTC] hholzgra@php.net
fixed in php4
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 06 17:01:33 2025 UTC