hexdump: Do not trust st_size if it equals zero.

Fix for hexdump -s not being able to skip files residing in
pseudo-filesystems that advertise a zero size value.

Historically, many pseudofs-based filesystems (e.g., procfs) report
a va_size of 0 for numerous files classified as regular files.
Typically, the contents of these files are generated on demand
from kernel data as sbuf(9) strings at the time they are read.
Accurately reporting the size of these files is challenging, as it
often involves generating their contents. These pseudofs implementations
frequently report the size as 0. This is a historical behavior and also
aligns with Linux behavior. To maintain compatibility, we have chosen
to preserve the existing behavior and address it in the userland
application, rather than modifying it in the kernel (by updating the
correct value for va_size).

PR:		bin/276106
MFC after:	1 week
This commit is contained in:
Ricardo Branco 2024-01-03 21:17:58 +01:00 committed by Xin LI
parent 67082f077f
commit e23954bd42
1 changed files with 3 additions and 2 deletions

View File

@ -391,13 +391,14 @@ doskip(const char *fname, int statok)
if (statok) {
if (fstat(fileno(stdin), &sb))
err(1, "%s", fname);
if (S_ISREG(sb.st_mode) && skip > sb.st_size) {
if (S_ISREG(sb.st_mode) && skip > sb.st_size && sb.st_size > 0) {
address += sb.st_size;
skip -= sb.st_size;
return;
}
}
if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) {
if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode) || \
(S_ISREG(sb.st_mode) && sb.st_size == 0)) {
noseek();
return;
}