4 ms·
void *calloc(size_t count, size_t size) { assert(!multiplication_would_overflow(count, size)); size_t allocation_size = count * size; v
by kr7 10y ago
void *calloc(size_t count, size_t size) {
assert(!multiplication_would_overflow(count, size));
size_t allocation_size = count * size;
void *allocation = malloc(allocation_size);
memset(allocation, 0, allocation_size);
return allocation;
}
Should really return (void *)0 on overflow, instead of asserting.
- sp332 10y agoI think assert() is usually used by people who are going to just turn them off during a "production" build (with -NDEBUG).
- loeg 10y agoAnd GP is right that in that case, you really want calloc to return NULL rather than an overflowed (short) non-NULL allocation.
- AstralStorm 10y agoAnd a few libc implementations have this kind of stupidity in, where in Linux there is a specialised syscall for performance reasons.