3 ms·
No. Instead, use <type> *buffer = calloc(n, sizeof(*buffer)) as that will also protect against multiplication overflow.
by pwdisswordfish6 6y ago
No. Instead, use
<type> *buffer = calloc(n, sizeof(*buffer))
as that will also protect against multiplication overflow.
- montebicyclelo 6y agoI don't know what I'm talking about, but according to [1] (2008): > So naturally every existing calloc out there performs this overflow check and doesn’t just multiply its arguments together? Oh no. Even I was surprised. > On OS X (10.4.11) calloc(65537, 65537) returns a non-NULL pointer. This is a bug. > PHK’s code in NetBSD is typical, it just multiples the two arguments and uses the result, reckless to overflow. > Hurray for FreeBSD: they do almost exactly what I suggest, but with a fast test that is just the tiniest bit unportable. Which suggests you shouldn't rely on there being a multiplication overflow check. [1] https://drj11.wordpress.com/2008/06/04/calloc-when-multiply-overflows/ https://drj11.wordpress.com/2008/06/04/calloc-when-multiply-...