Subject: lib/5057: strftime(3) has a bug about `%k'
To: None <gnats-bugs@gnats.netbsd.org>
From: NAKAJIMA Yoshihiro <nakayosh@kcn.or.jp>
List: netbsd-bugs
Date: 02/24/1998 23:34:12
>Number:         5057
>Category:       lib
>Synopsis:       strftime(3) has a bug about `%k'
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people (Library Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Feb 24 06:35:02 1998
>Last-Modified:
>Originator:     NAKAJIMA Yoshihiro
>Organization:
>Release:        NetBSD-current 98/02/23
>Environment:
System: NetBSD asura 1.3D NetBSD 1.3D (ASURA) #3: Tue Feb 24 21:15:07 JST 1998 nakayosh@asura:/usr/src/sys/arch/i386/compile/ASURA i386


>Description:
	strftime(3) has a bug about treatment of `%k'.
	if tm_hour is zero, strftime("%k") gives `  ' instead of ` 0'.

>How-To-Repeat:

% cat test.c
#include <stdio.h>
#include <time.h>

int main(void)
{
        struct tm tm;
        char buffer[16];

        tm.tm_hour = 0;
        strftime(buffer, 16, "%k", &tm);
        printf("`%s'\n", buffer);

        return 0;
}
% cc test.c
% ./a.out
`  '

>Fix:

diff -c src/lib/libc/time/strftime.c.orig src/lib/libc/time/strftime.c
*** src/lib/libc/time/strftime.c.orig	Tue Jul 22 20:13:35 1997
--- src/lib/libc/time/strftime.c	Tue Feb 24 21:41:39 1998
***************
*** 313,320 ****
  	static char buf[10];
  	register char *p;
  
! 	for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  		*p-- = n % 10 + '0';
  	while (p > buf && digits-- > 0)
  		*p-- = pad;
  	return (_add(++p));
--- 313,324 ----
  	static char buf[10];
  	register char *p;
  
! 	p = buf + sizeof(buf) - 2;
! 	do {
  		*p-- = n % 10 + '0';
+ 		n /= 10;
+ 		--digits;
+ 	} while (n > 0 && p > buf);
  	while (p > buf && digits-- > 0)
  		*p-- = pad;
  	return (_add(++p));

>Audit-Trail:
>Unformatted: