Hacker News new | ask | show | jobs
by wahern 53 days ago
Not sure I agree with this pattern for strings, but if you're gonna multiply for an allocation like that and are concerned about consistent idioms, use calloc or [non-standard] mallocarray, which will check the multiplication for overflow.
1 comments

After being bitten by mistakenly using int for the result of a sizeof, then overflowing the int multiplication with the count, my personal idiom is only use malloc if it's a count multiplied by a sizeof.

I just grep'ed my code base and found one counter-example, which I did on Aug 29, 2011.

Where I assumed sizeof(char) was 1. :)