This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PR82665 - missing value range optimization for memchr


On 20 November 2017 at 16:19, Jakub Jelinek <jakub@redhat.com> wrote:
> 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.
Hi,
I updated the patch based on POINTER_DIFF_EXPR.
Bootstrapped and tested on x86_64-unknown-linux-gnu.
Does it look OK ?

Thanks,
Prathamesh
>
>         Jakub

Attachment: pr82665-6.diff
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]