Page MenuHomeFreeBSD

lib{c,openbsd}: use ckd_mul() for overflow checking in re(c)allocarray
ClosedPublic

Authored by fuz on Oct 2 2025, 1:30 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Nov 25, 6:33 PM
Unknown Object (File)
Mon, Nov 17, 4:23 AM
Unknown Object (File)
Nov 11 2025, 5:28 PM
Unknown Object (File)
Nov 8 2025, 11:07 PM
Unknown Object (File)
Nov 6 2025, 4:59 AM
Unknown Object (File)
Oct 30 2025, 10:40 PM
Unknown Object (File)
Oct 30 2025, 11:17 AM
Unknown Object (File)
Oct 27 2025, 7:52 PM
Subscribers
None

Details

Summary

This makes the code easier to understand and slightly faster,
but requires C23. calloc() would benefit, too, but I didn't
want to touch the imported jemalloc code base.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 67487
Build 64370: arc lint + arc unit

Event Timeline

fuz requested review of this revision.Oct 2 2025, 1:30 PM
fuz created this revision.
kib added inline comments.
lib/libopenbsd/recallocarray.c
36

I would wrote

if (ckd_mul(&newsize, newnmemb, size) ||
    ckd_mul(&oldsize, oldnmemb, size)) {
             errno = EINVAL;
             return NULL;
}

since there are much simpler conditions now.

This revision is now accepted and ready to land.Oct 2 2025, 11:00 PM
lib/libopenbsd/recallocarray.c
36

This cannot be done as one needs to fail with ENOMEM while the other fails with EINVAL.

lib/libopenbsd/recallocarray.c
36

Indeed, missed that.