Wrong results using skipping positional arguments

Originator:drodriguez
Number:rdar://11423106 Date Originated:10-May-2012 12:13 PM
Status:Open Resolved:
Product:Mac OS X Product Version:10.7.3
Classification:Serious Bug Reproducible:Always
 
Summary:
NSLog, +stringWithFormat, -initWithFormat:arguments and CFStringCreateWithFormatAndArguments (at least) behave wrong with positional arguments that skip over the first argument.

Steps to Reproduce:

Compile the provided code.

Expected Results:

No output should be generated.

Actual Results:

012-05-10 11:33:38.029 bjc[18467:707] Problem: "%2$@ %2$@ %2$@ %2$@" != "%1$@ %1$@ %1$@ %1$@"
2012-05-10 11:33:38.031 bjc[18467:707] Problem: "%3$@ %3$@ %3$@ %3$@" != "%1$@ %1$@ %1$@ %1$@"
2012-05-10 11:33:38.032 bjc[18467:707] Problem: "%4$@ %4$@ %4$@ %4$@" != "%1$@ %1$@ %1$@ %1$@"
2012-05-10 11:33:38.033 bjc[18467:707] Problem: "     %2$@ %3$@ %4$@" != "     %1$@ %2$@ %3$@"
2012-05-10 11:33:38.034 bjc[18467:707] Problem: "%1$@      %3$@ %4$@" != "%1$@      %2$@ %3$@"
2012-05-10 11:33:38.034 bjc[18467:707] Problem: "%1$@ %2$@      %4$@" != "%1$@ %2$@      %3$@"
2012-05-10 11:33:38.035 bjc[18467:707] Problem: "%4$@ %3$@ %2$@     " != "%3$@ %2$@ %1$@     "
2012-05-10 11:33:38.036 bjc[18467:707] Problem: "%4$@ %3$@      %1$@" != "%3$@ %2$@      %1$@"
2012-05-10 11:33:38.036 bjc[18467:707] Problem: "%4$@      %2$@ %1$@" != "%3$@      %2$@ %1$@"

Regression:
Seems that this bug has existed since long time ago.

Notes:
Probably a duplicated of rdar://8045892 but at https://devforums.apple.com/message/659268 the user big3 dismissed it as “not a bug”, which I think this other radar proves wrong.

I have been able to reproduce the bug in 10.7.3, but also in Mac OS X 10.6 and iOS 5.0/5.1.

--- interpolations2.m ---

// clang -objc -framework Foundation  -g interpolations2.m

#import <Foundation/Foundation.h>

int main(int argc, char **argv) {
	@autoreleasepool {
		NSArray *array = [NSArray arrayWithObjects:
                           @"%1$@ %2$@ %3$@ %4$@",
                           @"%2$@ %3$@ %4$@ %1$@",
                           @"%3$@ %4$@ %1$@ %2$@",
                           @"%4$@ %1$@ %2$@ %3$@",
                           @"%1$@ %1$@ %1$@ %1$@",
                           @"%2$@ %2$@ %2$@ %2$@",
                           @"%3$@ %3$@ %3$@ %3$@",
                           @"%4$@ %4$@ %4$@ %4$@",
                           @"     %2$@ %3$@ %4$@",
                           @"%1$@      %3$@ %4$@",
                           @"%1$@ %2$@      %4$@",
                           @"%1$@ %2$@ %3$@     ",
                           @"%4$@ %3$@ %2$@ %1$@",
                           @"%4$@ %3$@ %2$@     ",
                           @"%4$@ %3$@      %1$@",
                           @"%4$@      %2$@ %1$@",
                           @"     %3$@ %2$@ %1$@",
                           nil];

     for (NSString *frmt in array) {
          NSString *string = [NSString stringWithFormat:frmt, @"%1$@", @"%2$@", @"%3$@", @"%4$@"];
          if (![frmt isEqualToString:string]) {
               NSLog(@"Problem: \"%@\" != \"%@\"", frmt, string);
          }
     }

	 char **carray = calloc(17, sizeof(char *));
	carray[0] = "%1$s %2$s %3$s %4$s";
    carray[1] = "%2$s %3$s %4$s %1$s";
    carray[2] = "%3$s %4$s %1$s %2$s";
    carray[3] = "%4$s %1$s %2$s %3$s";
    carray[4] = "%1$s %1$s %1$s %1$s";
    carray[5] = "%2$s %2$s %2$s %2$s";
    carray[6] = "%3$s %3$s %3$s %3$s";
    carray[7] = "%4$s %4$s %4$s %4$s";
    carray[8] = "     %2$s %3$s %4$s";
    carray[9] = "%1$s      %3$s %4$s";
    carray[10] = "%1$s %2$s      %4$s";
    carray[11] = "%1$s %2$s %3$s     ";
    carray[12] = "%4$s %3$s %2$s %1$s";
    carray[13] = "%4$s %3$s %2$s     ";
    carray[14] = "%4$s %3$s      %1$s";
    carray[15] = "%4$s      %2$s %1$s";
    carray[16] = "     %3$s %2$s %1$s";

	 char *cstring = calloc(20, sizeof(char));

	 for (int idx = 0; idx < 17; idx++) {
		 sprintf(cstring, carray[idx], "%1$s", "%2$s", "%3$s", "%4$s");
		 if (strncmp(cstring, carray[idx], 20) != 0) {
			 NSLog(@"CProblem: \"%s\" != \"%s\"", carray[idx], cstring);
		 }
	 }

	 return 0;
	}
}

--- EOF ---

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!