On Mon, Nov 20, 2017 at 04:13:49PM +0530, Prathamesh Kulkarni wrote:
> Hi,
> The attached patch tries to fix PR82665 by adding value-range for 'n'
> to [0, PTRDIFF_MAX - 1] in the following case:
> def = memchr(arg, 0, sz);
> n = def - arg
> 
> where def and arg are char *. I suppose it's safe to assume that if
> arg is char *, then
> memchr(arg, 0, sz) would return a non NULL pointer ?

I don't think it is safe, at least not until we have the POINTER_DIFF_EXPR.
Because
char *def = memchr (arg, 0, sz);
uintptr_t n = (uintptr_t) def - (uintptr_t) arg;
is valid even if def is NULL and you can't differentiate between original
pointer difference which would invoke UB if def was NULL and the case where
user did the subtraction in an integral type.

        Jakub

Reply via email to