https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61846

            Bug ID: 61846
           Summary: gcc assumes errno might be negative and issues
                    unnecessary warning
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zbyszek at in dot waw.pl

Created attachment 33148
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33148&action=edit
sample program

I see this a lot in systemd source code, where the convention is to return a
negative integer on error. gcc warns as if errno could also be zero or negative
after a system call, and warns about unitialized variables. Simplified example
attached.

$ gcc -Wall -O3 -std=gnu99 -o readwarn readwarn.c 
readwarn.c: In function ‘main’:
readwarn.c:28:9: warning: ‘ans’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
         printf("ans: %d", ans);
         ^

POSIX states that errno values are positive [1], and read may sets errno, so it
is safe to assume that if read() returned -1, errno must have a positive value.

If I replace 'return -errno' with 'return errno > 0 ? -errno : -EIO', the
warning disappers.

[1]
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html#tag_13_10

Reply via email to