recurse-dir: add assert for MALLOC_SIZEOF_SAFE lower bound

Coverity flags MALLOC_SIZEOF_SAFE(de) - offsetof(DirectoryEntries,
buffer) as a potential underflow when MALLOC_SIZEOF_SAFE returns 0.
After a successful malloc the return value is at least as large as
the requested size, but Coverity cannot trace this. Add an assert
to establish the lower bound.

CID#1548020

Follow-up for 6393b847f4
This commit is contained in:
Luca Boccassi
2026-04-07 23:59:16 +01:00
parent 1934c65108
commit 8fa088b886

View File

@@ -55,6 +55,8 @@ int readdir_all(int dir_fd, RecurseDirFlags flags, DirectoryEntries **ret) {
size_t bs;
ssize_t n;
/* Silence static analyzers, MALLOC_SIZEOF_SAFE is at least as large as the allocation */
assert(MALLOC_SIZEOF_SAFE(de) >= offsetof(DirectoryEntries, buffer));
bs = MIN(MALLOC_SIZEOF_SAFE(de) - offsetof(DirectoryEntries, buffer), (size_t) SSIZE_MAX);
assert(bs > de->buffer_size);