Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20210315200622.GE32655@brightrain.aerifal.cx>
Date: Mon, 15 Mar 2021 16:06:23 -0400
From: Rich Felker <dalias@...c.org>
To: Yossi Gottlieb <yossigo@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: strftime() unexpectedly modifies errno

On Mon, Mar 15, 2021 at 09:43:36PM +0200, Yossi Gottlieb wrote:
> It seems like strftime() unexpectedly modifies errno, which is always set
> to EINVAL when it returns. Looks like it's not related to any specific
> format.
> 
> Here's an example:
> 
> #include <stdio.h>
> #include <sys/time.h>
> #include <time.h>
> #include <errno.h>
> 
> int main(int argc, char *argv[])
> {
>     time_t now = time(NULL);
>     struct tm *tm = localtime(&now);
>     char buf[100];
> 
>     errno = 0;
>     size_t len = strftime(buf, sizeof(buf), "%d %b %Y %H:%M:%S.", tm);
> 
>     printf("len=%zu\n", len);
>     printf("buf=%s\n", buf);
>     printf("errno=%d\n", errno);
> }

This isn't unexpected. Any function except for a few specific ones
documented not to can modify errno as a side effect of success.
Inspecting errno is only meaningful immediately after failure.

The relevant text in the C language (C11) is 7.5 Errors <errno.h>, ΒΆ3:

"The value of errno may be set to nonzero by a library function call
whether or not there is an error, provided the use of errno is not
documented in the description of the function in this International
Standard."

There's equivalent text in POSIX as well.

Rich

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.