Skip to content

Commit 1566baf

Browse files
committed
hfs_stat: saturate nlink stat field on overflow
On some platforms (including macOS) nlink_t is a smaller type than the folder record's valence. This represents the number of entries in a directory, so it's quite possible to overflow this on a normal volume. hfsfuse now sets st_nlink to the max possible value in this case, consistent with other drivers.
1 parent 6d0e3e4 commit 1566baf

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CONFIG_CFLAGS := $(CONFIG_CFLAGS) $(CFLAGS)
1414
CFLAGS := -D_FILE_OFFSET_BITS=64 $(CONFIG_CFLAGS)
1515

1616
# extra flags we don't want to forward to the "external" libs like libhfs/ublio/utf8proc
17-
LOCAL_CFLAGS+=-Wall -Wextra -pedantic -Wno-gnu-zero-variadic-macro-arguments -Wno-unused-parameter
17+
LOCAL_CFLAGS+=-Wall -Wextra -pedantic -Wno-gnu-zero-variadic-macro-arguments -Wno-unused-parameter -Wno-error=type-limits
1818
# older versions of gcc/clang need these as well
1919
LOCAL_CFLAGS+=-Wno-missing-field-initializers -Wno-missing-braces
2020

lib/libhfsuser/hfsuser.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,14 @@ void hfs_stat(hfs_volume* vol, hfs_catalog_keyed_record_t* key, struct stat* st,
445445
#endif
446446
}
447447
else {
448-
st->st_nlink = key->folder.valence + 2;
448+
if(generic_int_max(st->st_nlink)-2 < key->folder.valence)
449+
st->st_nlink = generic_int_max(st->st_nlink);
450+
else {
451+
//valence must be cast to the type of st_nlink to really guarantee no overflow here, but nlink_t is not always defined (e.g. mingw) hence separate ops
452+
st->st_nlink = key->folder.valence;
453+
st->st_nlink += 2;
454+
}
455+
449456
st->st_size = vol->vh.block_size;
450457
#if HAVE_STAT_BLKSIZE
451458
st->st_blksize = vol->vh.block_size;

0 commit comments

Comments
 (0)