sprof: check pread size and offset for overflow

Collin Funk collin.funk1@gmail.com
Thu Oct 16 01:53:47 GMT 2025


DJ Delorie <dj@redhat.com> writes:

> +  /* We're depending on data that's being read from the file, so be a
> +     bit paranoid here and make sure the requests are reasonable.
> +     PREAD would have failed anyway, but this is more robust and
> +     explains what happened better.  */
> +#define PCHECK(s,l) if ((s) < 0 || (l) < 0 \
> +			|| ((s)+(l)) < 0 || ((s)+(l)) > st.st_size)				\
> +    error (EXIT_FAILURE, ERANGE, \
> +	   _("read outside of file extents %ld + %ld > %ld"),	\
> +	   (long int)(s), (long int)(l), st.st_size)

Wouldn't it be easier to just use __builtin_add_overflow, check the
result, and then do the comparison? I think that would be easier to
read.

Ideally we could use C23's ckd_add, but we support gcc 12.1 which does
not come with stdckdint.h.

glibc copies intprops.h from Gnulib, which has INT_ADD_WRAPV, but that
is just __builtin_add_overflow on all compilers glibc supports. CC'ed
Paul who can correct me if I am wrong.

Collin


More information about the Libc-alpha mailing list