public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Leo Liang <ycliang@andestech.com>
To: <libc-help@sourceware.org>
Cc: <alankao@andestech.com>, <peterlin@andestech.com>,
	<dylan@andestech.com>, <ycliang@andestech.com>
Subject: Is it reasonable to expect errno to be 0 when entering main?
Date: Mon, 26 Sep 2022 12:07:30 +0000	[thread overview]
Message-ID: <YzGWAjzz2OhxKTGL@ubuntu01> (raw)

Hello guys,

According to the ANSI C standard "7.5 Errors <errno.h>" section 3[1], 
"The value of errno is zero at program startup,
but is never set to zero by any library function."

The "program startup" is also defined in ANSI C standard "5.1.2.2.1 Program startup" section 1[2],
"The function called at program startup is named main."

Therefore, it should be reasonable to expect errno be zero when main is executed.

However, we found that the following program would get non-zero errno under some circumstances.

```
/* errno.c */
#include <errno.h>
#include <stdio.h>

int main()
{
	printf("errno: %d\n", errno);
	return 0;
}


$ riscv64-linux-gcc -O0 -g -static -o errno_st errno.c

[   51.968765] random: fast init done
/mnt # ./errno
errno: 11

[   51.968765] random: fast init done
[  262.517056] crng init done
/mnt # ./errno
errno: 0
```

If the above program is statically-linked, and if the program is executed before "crng init done",
the errno will not be zero when entering main.

There seems to be syscall (__getrandom) before main is entered,
and if the crng is not initialized, the syscall will fail causing the errno to be set.
(_dl_get_origin -> __libc_malloc -> ptmalloc_init -> tcache_key_initialize -> __getrandom)

So we are wondering if we should set errno to zero before entering main.

Best regards,
Leo



The environment we are using is 
Linux Kernel 5.4
GLIBC 2.35
Binutil 2.38

Reference:
[1] ISO/IEC 9899:1999 "7.5 Errors <errno.h>": p.186:3 https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
[2] ISO/IEC 9899:1999 "5.1.2.2.1 Program startup": p.12:1 https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

             reply	other threads:[~2022-09-26 12:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 12:07 Leo Liang [this message]
2022-09-27  6:05 ` Florian Weimer
2022-09-27  7:15   ` Leo Liang
2022-09-27  7:31     ` Florian Weimer

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=YzGWAjzz2OhxKTGL@ubuntu01 \
    --to=ycliang@andestech.com \
    --cc=alankao@andestech.com \
    --cc=dylan@andestech.com \
    --cc=libc-help@sourceware.org \
    --cc=peterlin@andestech.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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).