4 ms·
Think calloc()---it takes two size parameters, one the size of the item, and one the number of items. In theory, this could avoid an overflow: items = fre
by spc476 4y ago
Think calloc()---it takes two size parameters, one the size of the item, and one the number of items. In theory, this could avoid an overflow:
items = fread(array,sizeof(array[0]),count,fp);
vs:
items = read(fh,array,sizeof(array[0]) * count);
That multiple could overflow. fread() (and fwrite()) return the number of items read, not the number of bytes (or characters). The standard also points out that if size is 0, or nmemb is 0, nothing is done (and 0 is returned).