Created attachment 33148 [details]
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 , 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.
Created attachment 33150 [details]
Created attachment 33151 [details]
Created attachment 33152 [details]
C99 also has this requirement. But C89 did not.
>Values for errno are now required to be distinct positive values rather than
> non-zero values. This change is for alignment with the ISO/IEC 9899:1999
So using -std=gnu99 should allow this to not be unitilaized except GCC has no way to know you are reading from errno just yet.
(In reply to Andrew Pinski from comment #4)
> C99 also has this requirement. But C89 did not.
The warnings are "best effort" anyway. So even if the standards did *not* say that, gcc could skip the warning since existing systems all work this way anyway.
I think it could make for a nice optimization, when compiling for C99, but that is not what I'm asking for atm.
> >Values for errno are now required to be distinct positive values rather than
> > non-zero values. This change is for alignment with the ISO/IEC 9899:1999
> > standard.
> So using -std=gnu99 should allow this to not be unitilaized except GCC has
> no way to know you are reading from errno just yet.
Wouldn't it be a matter of annotating read() call with the sideffect of "return value >= 0 || errno > 0" ?