mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Yossi Gottlieb <yossigo@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] strftime() unexpectedly modifies errno
Date: Mon, 15 Mar 2021 16:06:23 -0400	[thread overview]
Message-ID: <20210315200622.GE32655@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAMquMNTmDZnGvtPo3H0Uo-3ytGDO5EHa+RHCBTZTph6c3WFsYA@mail.gmail.com>

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

      reply	other threads:[~2021-03-15 20:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 19:43 Yossi Gottlieb
2021-03-15 20:06 ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210315200622.GE32655@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=yossigo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).