[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#465427: libc6: strftime gets confused by the invalid format specifier "%20#%"

Package: libc6
Version: 2.7-6
Severity: minor

I used the following program to probe the behaviour of strftime: <file src="strftime.c">

#include <time.h>
#include <stdio.h>
const int bufsize = 4096;

int main(int count, char *args[])
	char buf[bufsize];
	time_t now = time(NULL);
	struct tm rom, *when = localtime_r(&now, &rom);
	int i = 1;
	if (when == 0)
		return 1;

	while (i < count)
		size_t len = strftime(buf, bufsize, args[i++], when);
		if (len)
	return 0;

</file> (I wanted to know what modifiers it supported to the various
format specifiers; I could only see E and O mentioned in either POSIX
or glibc's own manual page, but there are obvious uses for field
widths on %A and %B, for example, and systematic support for '0' and '
' would enable a simplification of the spec (e.g. we'd only need one
of H, k), so I was pleased to find field widths supported and curious
to see what, if anything else, is.)

I found that printf's '#', '-' and '0' modifiers are honoured (but '+'
and ' ' aren't).  These don't really make sense, aside from "%0l"
(ell) and "%0k" - and even these are mere aliases fo "%I" and "%H", so
not actually useful.  This is harmless.

However, when I tried the malformed format string "|%20#%|" it emitted
|                %20#%|
in which it's converted %20# to a string of width 20 (thanks to the
field width), then decided not to treat it as a format specifier after
all, so used %20# as the "content" right-justified within those 20
characters.  For contrast, snprintf simply emitted the format
specifier verbatim - as I expected, given that this is how invalid
format specifiers are generally handled by both functions.

This behaviour is inconsistent with the general handling of invalid
format specifiers, although it probably isn't strictly a bug - I
imagine the spec says the behaviour on undefined format specifiers is
undefined.  However, it does suggest some confusion in the
implementation, so seems worth reporting upstream.

It's not important or urgent, though !

-- System Information:
Debian Release: lenny/sid
  APT prefers testing
  APT policy: (500, 'testing'), (500, 'stable')
Architecture: i386 (i686)

Kernel: Linux 2.6.22-3-686 (SMP w/2 CPU cores)
Locale: LANG=en_GB.ISO-8859-15, LC_CTYPE=en_GB.ISO-8859-15 (charmap=ISO-8859-15)
Shell: /bin/sh linked to /bin/bash

Versions of packages libc6 depends on:
ii  libgcc1                 1:4.3-20080116-1 GCC support library

libc6 recommends no packages.

-- debconf information:

Reply to: