mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] strftime() unexpectedly modifies errno
@ 2021-03-15 19:43 Yossi Gottlieb
  2021-03-15 20:06 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Yossi Gottlieb @ 2021-03-15 19:43 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 555 bytes --]

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);
}

[-- Attachment #2: Type: text/html, Size: 776 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] strftime() unexpectedly modifies errno
  2021-03-15 19:43 [musl] strftime() unexpectedly modifies errno Yossi Gottlieb
@ 2021-03-15 20:06 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2021-03-15 20:06 UTC (permalink / raw)
  To: Yossi Gottlieb; +Cc: musl

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-15 20:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-15 19:43 [musl] strftime() unexpectedly modifies errno Yossi Gottlieb
2021-03-15 20:06 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).