Bug 14975 - sprintf() with a '%10$' specification fails
sprintf() with a '%10$' specification fails
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Low-level
R 2.15.0 patched
All All
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-07-03 23:50 UTC by David Hinds
Modified: 2012-07-04 03:32 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Hinds 2012-07-03 23:50:38 UTC
sprintf() format strings can include positional references to arguments, i.e.:

> sprintf('%2$d %1$d', 1, 2)
[1] "2 1"

However, references to arguments 10,20,30,... fail:

> sprintf('%10$d', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Error in sprintf("%10$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) : 
  invalid format '%10$d'; use format %f, %e, %g or %a for numeric objects

In src/main/sprintf.c, line 182:

    } else if(fmt[2] >= '1' && fmt[2] <= '9' && fmt[3] == '$') {

should be:

    } else if(fmt[2] >= '0' && fmt[2] <= '9' && fmt[3] == '$') {
Comment 1 Duncan Murdoch 2012-07-04 04:37:34 UTC
On 12-07-03 6:50 PM, r-bugs@r-project.org wrote:
> https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14975
>
>             Summary: sprintf() with a '%10$' specification fails
>             Product: R
>             Version: R 2.15.0 patched
>            Platform: All
>          OS/Version: All
>              Status: NEW
>            Severity: minor
>            Priority: P5
>           Component: Low-level
>          AssignedTo: R-core@R-project.org
>          ReportedBy: dhinds@sonic.net
>     Estimated Hours: 0.0
>
>
> sprintf() format strings can include positional references to arguments, i.e.:
>
>> sprintf('%2$d %1$d', 1, 2)
> [1] "2 1"
>
> However, references to arguments 10,20,30,... fail:
>
>> sprintf('%10$d', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
> Error in sprintf("%10$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) :
>    invalid format '%10$d'; use format %f, %e, %g or %a for numeric objects
>
> In src/main/sprintf.c, line 182:
>
>      } else if(fmt[2]>= '1'&&  fmt[2]<= '9'&&  fmt[3] == '$') {
>
> should be:
>
>      } else if(fmt[2]>= '0'&&  fmt[2]<= '9'&&  fmt[3] == '$') {
>


Thanks for spotting this.  There are 3 other occurrences of the same 
thinko elsewhere; I'll fix them.  (They were written by 3 different 
authors!)

Duncan Murdoch