mailing list of musl libc
 help / color / mirror / 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

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ http://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git