mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00
![]() The compiler does not know that waitid() will only ever return 0 or -1.
If waitid() would return a positive value than waitpid() would return that
same value and *status would not be initialized.
However users calling waitpid() know that the only possible return values
of it are 0 or -1. They therefore might check for errors with
'ret == -1' or 'ret < 0' and use *status otherwise. The compiler will then
warn about the usage of a potentially uninitialized variable.
Example:
$ cat test.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int ret, status;
ret = waitpid(0, &status, 0);
if (ret == -1)
return 0;
printf("status %x\n", status);
return 0;
}
$ gcc --version
gcc (GCC) 15.1.1 20250425
$ gcc -Wall -Os -Werror -nostdlib -nostdinc -static -Iusr/include -Itools/include/nolibc/ -o /dev/null test.c
test.c: In function ‘main’:
test.c:12:9: error: ‘status’ may be used uninitialized [-Werror=maybe-uninitialized]
12 | printf("status %x\n", status);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.c:6:18: note: ‘status’ was declared here
6 | int ret, status;
| ^~~~~~
cc1: all warnings being treated as errors
Avoid the warning by normalizing waitid() errors to '-1' in waitpid().
Fixes:
|
||
---|---|---|
.. | ||
auxv.h | ||
ioctl.h | ||
mman.h | ||
mount.h | ||
prctl.h | ||
random.h | ||
reboot.h | ||
resource.h | ||
stat.h | ||
syscall.h | ||
sysmacros.h | ||
time.h | ||
timerfd.h | ||
types.h | ||
utsname.h | ||
wait.h |