[PATCH PR45098] Disallow NULL pointer in pointer arithmetic
Zdenek Dvorak
rakdver@kam.mff.cuni.cz
Thu Jun 16 15:54:00 GMT 2011
> >> diff -u gcc/tree-ssa-loop-niter.c (working copy) gcc/tree-ssa-loop-niter.c (working copy)
> >> --- gcc/tree-ssa-loop-niter.c (working copy)
> >> +++ gcc/tree-ssa-loop-niter.c (working copy)
> >> @@ -2875,6 +2875,16 @@
> >> Â Â low = lower_bound_in_type (type, type);
> >> Â Â high = upper_bound_in_type (type, type);
> >>
> >> + Â /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
> >> + Â Â produce a NULL pointer. Â The contrary would mean NULL points to an object,
> >> + Â Â while NULL is supposed to compare unequal with the address of all objects.
> >> + Â Â Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
> >> + Â Â NULL pointer since that would mean wrapping, which we assume here not to
> >> + Â Â happen. Â So, we can exclude NULL from the valid range of pointer
> >> + Â Â arithmetic. Â */
> >> + Â if (int_cst_value (low) == 0)
> >> + Â Â low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type)));
> >> +
> >> Â Â record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
> >> Â }
> >
> > OK,
>
> I think this is only valid for !flag_delete_null_pointer_checks, on
> architectures where that isn't the default we have to assume that
> NULL may point to an object.
agreed. Thanks for the correction.
Zdenek
More information about the Gcc-patches
mailing list