Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 19 Apr 2004 15:18:47 -0400 (EDT)
From:      Jonathan McGee <mcgee@wam.umd.edu>
To:        undisclosed-recipients: ;
Subject:   standards/65779: Potential bug in printf positional parameters
Message-ID:  <200404191918.i3JJIlKi005399@rac3.wam.umd.edu>
Resent-Message-ID: <200404191920.i3JJKHci059420@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         65779
>Category:       standards
>Synopsis:       Potential bug in printf positional parameters
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 19 12:20:17 PDT 2004
>Closed-Date:
>Last-Modified:
>Originator:     Jonathan McGee
>Release:        FreeBSD 5.2.1-RELEASE-p4 i386
>Organization:
>Environment:
System: FreeBSD freebie 5.2.1-RELEASE-p4 FreeBSD 5.2.1-RELEASE-p4 #0: Tue Apr 13 06:05:44 EDT 2004 root@freebie:/usr/obj/usr/src/sys/FREEBIE i386

System is build by user with CFLAGS="-O3 -pipe", CPU="p3".  After the problem
was found, it was retried with CFLAGS="-O0 -g" with no change in the
behavior.  

>Description:

While working on a port, found a potential issue with positional parameters in
FreeBSD's libc.  It seems that both sprintf and printf (the only two functions
tested) are not honoring the positional argument flags.

A minimum test case was written and attached (test-libc.c).  When built, four
integers (1, 2, 3, 4) are passed to a positional printf that should display
them in order, skipping the second.

When executed, the output expected is:
1 3 4
1 3 4
Instead, I get:
1 2 3
1 2 3

It seems rather sensitive to the ordering of printf and sprintf calls.  I can
swap them and get different behaviors (occasionally one may work).

>How-To-Repeat:

See attached test-libc.c

>Fix:

	

--- test-libc.c begins here ---
#include <stdio.h>

int main() {
	int int1 = 1;
	int int2 = 2;
	int int3 = 3;
	int int4 = 4;

	char buffer[200];
	char *format = "%1$i %3$i %4$i\n";
	printf(format, int1, int2, int3, int4);
	sprintf(buffer, format, int1, int2, int3, int4);
	printf("%s", buffer);

	return 0;
}
--- test-libc.c ends here ---



>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200404191918.i3JJIlKi005399>