Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <Zj-Ny2wTG_PcMCd7@album.bayer.uni.cx>
Date: Sat, 11 May 2024 17:24:59 +0200
From: Petr Pisar <petr.pisar@...as.cz>
To: musl@...ts.openwall.com
Subject: strptime(s, "%Y-%j", &tm) does not update month and day

Hello,

When debugging a libisds test failure with musl-1.2.5
<https://bugs.gentoo.org/show_bug.cgi?id=928107>, I found that musl's
strptime() does not handle "%Y-%j" properly. It accepts the format, it parses
the input string, it returns a correct pointer past the input string, but it
does not update tm.tm_mon and tm.tm_mday fileds of the third argument.

A reproducer:

#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
#include <stddef.h>

int main(void) {
    const char *input = "2001-34"; /* 34th day of 2001 year, i.e. 2001-02-03 */
    struct tm time = {.tm_year=42, .tm_mon=42, .tm_mday=42, }; /* To detect changes */
    char *ret = strptime(input, "%Y-%j", &time);
    printf("Expected: ret=%p(%p+%td) tm_year=%d, tm_mon=%d, tm_mday=%d\n",
            input + 7, input, (ptrdiff_t)7, 101, 1, 3);
    printf("Got:      ret=%p(%p+%td) tm_year=%d, tm_mon=%d, tm_mday=%d\n",
            ret, input, ret - input, time.tm_year, time.tm_mon, time.tm_mday);
    return 0;
}

$ gcc test.c && ./a.out
Expected: ret=0x560c1cd64007(0x560c1cd64000+7) tm_year=101, tm_mon=1, tm_mday=3
Got:      ret=0x560c1cd64007(0x560c1cd64000+7) tm_year=101, tm_mon=42, tm_mday=42

Compare to glibc-2.38:

$ gcc test.c && ./a.out
Expected: ret=0x55fd94c4f00f(0x55fd94c4f008+7) tm_year=101, tm_mon=1, tm_mday=3
Got:      ret=0x55fd94c4f00f(0x55fd94c4f008+7) tm_year=101, tm_mon=1, tm_mday=3

Tested on x86_64 platform with gcc-13.2.1_p20240210, binutils-2.42 and
musl-1.2.5

-- Petr

Download attachment "signature.asc" of type "application/pgp-signature" (834 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.