Skip to content

dont set errno = 0 #129

Open
Open
@rurban

Description

@rurban
Owner

According to the C Standard (e.g., C99:7.5p3),

"The value of errno is zero at program startup, but is never set to zero by any library function."

$ git grep -n 'errno = 0' src

src/io/fopen_s.c:100:    errno = 0;
src/io/fprintf_s.c:95:    errno = 0;
src/io/freopen_s.c:104:    errno = 0;
src/io/fscanf_s.c:136:    errno = 0;
src/io/gets_s.c:148:    errno = 0;
src/io/scanf_s.c:136:    errno = 0;
src/io/sscanf_s.c:130:    errno = 0;
src/io/tmpfile_s.c:98:    errno = 0;
src/io/vfprintf_s.c:101:    errno = 0;
src/io/vfscanf_s.c:131:    errno = 0;
src/io/vprintf_s.c:87:    errno = 0;
src/io/vscanf_s.c:123:    errno = 0;
src/io/vsscanf_s.c:130:    errno = 0;
src/misc/bsearch_s.c:156:    errno = 0;
src/os/getenv_s.c:142:    errno = 0;
src/os/gmtime_s.c:124:    errno = 0;
src/os/localtime_s.c:126:    errno = 0;
src/str/strtok_s.c:259:    errno = 0;
src/str/vsnprintf_s.c:1186:    errno = 0;
src/str/vsprintf_s.c:115:        errno = 0;
src/wchar/fwprintf_s.c:112:    errno = 0;
src/wchar/fwscanf_s.c:108:        errno = 0;
src/wchar/fwscanf_s.c:137:    errno = 0;
src/wchar/mbsrtowcs_s.c:201:            errno = 0;
src/wchar/mbstowcs_s.c:183:    errno = 0;
src/wchar/mbstowcs_s.c:199:            errno = 0;
src/wchar/snwprintf_s.c:191:    errno = 0;
src/wchar/swprintf_s.c:192:    errno = 0;
src/wchar/swprintf_s.c:202:        errno = 0;
src/wchar/swprintf_s.c:217:            errno = 0; /* EOVERFLOW */
src/wchar/swscanf_s.c:137:    errno = 0;
src/wchar/vfwprintf_s.c:118:    errno = 0;
src/wchar/vfwscanf_s.c:112:        errno = 0;
src/wchar/vfwscanf_s.c:141:    errno = 0;
src/wchar/vsnwprintf_s.c:190:    errno = 0;
src/wchar/vswprintf_s.c:174:    errno = 0;
src/wchar/vswprintf_s.c:198:            errno = 0;
src/wchar/vswscanf_s.c:135:    errno = 0;
src/wchar/vwprintf_s.c:107:    errno = 0;
src/wchar/vwscanf_s.c:128:    errno = 0;
src/wchar/wcstok_s.c:267:    errno = 0;
src/wchar/wscanf_s.c:124:    errno = 0;

From https://www.bugseng.com/blog/c-standard-library-cannot-be-trusted-blindly

Activity

self-assigned this
on Feb 20, 2023
jleffler

jleffler commented on Feb 20, 2023

@jleffler
added a commit that references this issue on Dec 22, 2023
added a commit that references this issue on Mar 8, 2024
added this to the 3.9 milestone on Mar 8, 2024
added a commit that references this issue on Mar 8, 2024
rurban

rurban commented on Dec 26, 2024

@rurban
OwnerAuthor

According to the C Standard (e.g., C99:7.5p3),

"The value of errno is zero at program startup, but is never set to zero by any library function."

But this a libc, not just a common library. And the libc mandates to set errno on all cases a syscall fails. And to clear it before each syscall.

modified the milestones: 3.9, 3.10, 3.10 on Dec 26, 2024

7 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rurban@jleffler

      Issue actions

        dont set errno = 0 · Issue #129 · rurban/safeclib