From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from Atcsqr.andestech.com (60-248-80-70.hinet-ip.hinet.net [60.248.80.70]) by sourceware.org (Postfix) with ESMTPS id 833ED3858D32 for ; Mon, 26 Sep 2022 12:08:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 833ED3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=andestech.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andestech.com Received: from mail.andestech.com (ATCPCS16.andestech.com [10.0.1.222]) by Atcsqr.andestech.com with ESMTP id 28QC7uYq043853 for ; Mon, 26 Sep 2022 20:07:56 +0800 (+08) (envelope-from ycliang@andestech.com) Received: from ubuntu01 (10.0.12.75) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.498.0; Mon, 26 Sep 2022 20:07:55 +0800 Date: Mon, 26 Sep 2022 12:07:30 +0000 From: Leo Liang To: CC: , , , Subject: Is it reasonable to expect errno to be 0 when entering main? Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline User-Agent: Mutt/2.0.5 (2021-01-21) X-Originating-IP: [10.0.12.75] X-DNSRBL: X-MAIL:Atcsqr.andestech.com 28QC7uYq043853 X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,KAM_DMARC_STATUS,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hello guys, According to the ANSI C standard "7.5 Errors " 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 #include 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 ": 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