The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=64584008139f73b07fa7c7979c41780be458b8cc
commit 64584008139f73b07fa7c7979c41780be458b8cc Author: Konstantin Belousov <k...@freebsd.org> AuthorDate: 2025-06-23 23:46:43 +0000 Commit: Konstantin Belousov <k...@freebsd.org> CommitDate: 2025-06-24 01:11:17 +0000 csu: C standard requires errno on main() entry Reported by: Greg Becker <greg.bec...@klarasystems.com> Reviewed by: emaste, kevans, markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Also see: https://reviews.freebsd.org/D50998 Differential revision: https://reviews.freebsd.org/D50997 --- lib/libc/csu/libc_start1.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/libc/csu/libc_start1.c b/lib/libc/csu/libc_start1.c index 045ea1e68141..5b08ba03ba8c 100644 --- a/lib/libc/csu/libc_start1.c +++ b/lib/libc/csu/libc_start1.c @@ -28,6 +28,7 @@ #include <sys/param.h> #include <sys/elf.h> #include <sys/elf_common.h> +#include <errno.h> #include <stdlib.h> #include "libc_private.h" @@ -169,6 +170,13 @@ __libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void), } handle_static_init(argc, argv, env); + + /* + * C17 4.3 paragraph 3: + * The value of errno in the initial thread is zero at program + * startup. + */ + errno = 0; exit(mainX(argc, argv, env)); } @@ -194,5 +202,6 @@ __libc_start1_gcrt(int argc, char *argv[], char *env[], monstartup(eprolp, etextp); handle_static_init(argc, argv, env); + errno = 0; exit(mainX(argc, argv, env)); }