sprof: check pread size and offset for overflow

Paul Eggert eggert@cs.ucla.edu
Thu Oct 16 16:21:03 GMT 2025


On 2025-10-15 19:22, DJ Delorie wrote:
> I want to do more than just check the sum, I want to check that every
> aspect of this math is "inside" the file.  Any bad data is an error.
> ...
> #define PCHECK(s,l) if ((s) < 0  || (s) > st.st_size			\
> 			|| (l) < 0 || (l) > st.st_size			\
> 			|| ((s)+(l)) < 0 || ((s)+(l)) > st.st_size)	\

The problem is that the last line does not do what you want. If S+L 
overflows, behavior is undefined. This is why Collin suggested using 
__builtin_add_overflow (or C23 ckd_add).


More information about the Libc-alpha mailing list