fixdep: add fstat error handling

When `fstat` fails, `st` is left uninitialised. In our case, Ben Kohler
noticed our release media builds were failing in Gentoo on x86 when building
busybox with occasional SIGBUS. This turned out to be EOVERFLOW (from 32-bit
ino_t) which wasn't being reported because nothing was checking the return value
from `fstat`.

Fix that to avoid UB (use of uninit var) and to give a more friendly
error to the user.

This actually turns out to be fixed already in the kernel from back in
2010 [0] and 2016 [1].

[0] a3ba81131a
[1] 46fe94ad18

Reported-by: Ben Kohler <bkohler@gentoo.org>
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Sam James 2024-04-23 21:10:18 +01:00 committed by Denys Vlasenko
parent dbd14c4a42
commit 480a07bd68

View File

@ -105,6 +105,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
@ -292,7 +293,10 @@ void do_config_file(char *filename)
perror(filename);
exit(2);
}
fstat(fd, &st);
if (fstat(fd, &st) < 0) {
fprintf(stderr, "fixdep: fstat %s %s\n", filename, strerror(errno));
exit(2);
}
if (st.st_size == 0) {
close(fd);
return;
@ -368,7 +372,10 @@ void print_deps(void)
perror(depfile);
exit(2);
}
fstat(fd, &st);
if (fstat(fd, &st) < 0) {
fprintf(stderr, "fixdep: fstat %s %s\n", depfile, strerror(errno));
exit(2);
}
if (st.st_size == 0) {
fprintf(stderr,"fixdep: %s is empty\n",depfile);
close(fd);