php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #61596 Documentation doesn't mention negative return values
Submitted: 2012-04-02 03:00 UTC Modified: 2012-04-02 08:00 UTC
From: rosuav at rosuav dot com Assigned: aharvey (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.4Git-2012-04-02 (Git) OS: Linux [Debian 6.0.4]
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: rosuav at rosuav dot com
New email:
PHP Version: OS:

 

 [2012-04-02 03:00 UTC] rosuav at rosuav dot com
Description:
------------
Originally tested with PHP 5.3.3.7+squeeze8 with Suhosin-Patch (cli) (built: Feb 
10 2012 13:05:56) from Debian repository, confirmed with latest from Debian's 
git repository (announced as 5.4.0).

The sscanf() function, when given variables into which to put its results, is 
documented as returning the number of assigned values: 
http://php.net/manual/en/function.sscanf.php

Usage, however, shows that it can return -1 if the input string does not match 
the pattern - see test script. I'm not sure if this is a documentation error or 
an actual function bug (it seems odd that a prefix match should affect the 
return value), but either way, the disconnect is confusing.

The issue seems to arise from ext/standard/scanf.c lines 687-695 (in latest 
cloned from git), in which different treatment is given to mismatch and 
underflow, and then line 1200 triggers the -1 return in the case of underflow.

My original code (very similar to the test script attached) was using "if 
(sscanf(....)) do_stuff" - had I known about negative returns, a more correct 
line of code would say "if (sscanf(...)>0) do_stuff" instead.


Test script:
---------------
<?php
$_POST = array('foobar1'=>5, 'foobar2'=>7, 'foo'=>'Do some Fooage', 'other'=>42);
foreach ($_POST as $k=>$v)
{
	$ret=sscanf($k,'foobar%d',$id);
	echo "key $k -> id $id, value $v - sscanf returned $ret\n";
}


Expected result:
----------------
According to the documentation, the foobar1 and foobar2 lines should have a 
return value of 1, because $id was assigned to; the other two should have a 
return value of 0.

Actual result:
--------------
The foo line has a return value of -1, unexpected according to the documentation.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-02 06:52 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=324696
Log: Fix doc bug #61596 (Documentation doesn't mention negative return values).
 [2012-04-02 06:52 UTC] aharvey@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: aharvey
 [2012-04-02 06:52 UTC] aharvey@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2012-04-02 08:00 UTC] rosuav at rosuav dot com
Ah! Thanks for that. That's sorted then!
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 21:00:02 2026 UTC