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)
Sat, Feb 28, 8:08 AM
Unknown Object (File)
Fri, Feb 27, 6:05 PM
Unknown Object (File)
Tue, Feb 17, 10:34 AM
Unknown Object (File)
Jan 16 2026, 2:35 PM
Unknown Object (File)
Jan 14 2026, 1:08 AM
Unknown Object (File)
Jan 8 2026, 10:27 AM
Unknown Object (File)
Dec 27 2025, 10:39 PM
Unknown Object (File)
Dec 25 2025, 12:42 AM
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 Not Applicable
Unit
Tests Not Applicable

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.