[PATCH] printf handles repeated placeholders wrong
| Bug #29733 | [PATCH] printf handles repeated placeholders wrong | ||||
|---|---|---|---|---|---|
| Submitted: | 2004-08-18 14:46 UTC | Modified: | 2005-01-26 01:03 UTC | ||
| From: | bugs dot php dot net at bluetwanger dot de | Assigned: | |||
| Status: | Closed | Package: | Strings related | ||
| PHP Version: | 4CVS, 5CVS (2004-12-12) | OS: | * | ||
| Private report: | No | CVE-ID: | None | ||
[2004-08-18 14:46 UTC] bugs dot php dot net at bluetwanger dot de
Description:
------------
printf('%s - %s %s %3$s %2$s', 1, 2, 3);
complains:
Warning: printf(): Too few arguments in /home/bertheau/printf.php on line 2
printf('%s - %s %s %3$s %2$s', 1, 2, 3, 4);
does not complain and prints:
1 - 2 3 3 2
I expect the first version to not complain and print what the second version prints.
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2004-08-27 14:30 UTC] bugs dot php dot net at bluetwanger dot de
Here's a patch: --- ext/standard/formatted_print.c.orig 2004-07-18 19:28:04.000000000 +0200 +++ ext/standard/formatted_print.c 2004-08-27 14:23:07.580732341 +0200 @@ -537,12 +537,6 @@ php_sprintf_appendchar(&result, &outpos, &size, '%' TSRMLS_CC); inpos += 2; } else { - if (currarg >= argc && format[inpos + 1] != '%') { - efree(result); - efree(args); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too few arguments"); - return NULL; - } /* starting a new format specifier, reset variables */ alignment = ALIGN_RIGHT; adjusting = 0; @@ -574,13 +568,6 @@ argnum += format_offset; - if (argnum >= argc) { - efree(result); - efree(args); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too few arguments"); - return NULL; - } - /* after argnum comes modifiers */ PRINTF_DEBUG(("sprintf: looking for modifiers\n" "sprintf: now looking at '%c', inpos=%d\n", @@ -635,6 +622,13 @@ argnum = currarg++ + format_offset; } + if (argnum >= argc) { + efree(result); + efree(args); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too few arguments"); + return NULL; + } + if (format[inpos] == 'l') { inpos++; } Let's see if the line breaks survive. It basically removes the bogus (format[inpos + 1] != '%' will always be true there) arg number check and moves the right one outside the special case for "complicated" format specifiers.[2004-08-27 14:36 UTC] bugs dot php dot net at bluetwanger dot de
[2004-09-30 16:42 UTC] danielc at analysisandsolutions dot com
Note, the error only happens when mixing numbered and non-numberd directives. Tweaking the example from the original bug report to use only numbered directives eliminates the error: printf('%1$s - %2$s %3$s %3$s %2$s', 1, 2, 3);[2004-10-05 17:50 UTC] bugs dot php dot net at afdelingp dot dk
[2005-01-26 01:03 UTC] iliaa@php.net