The code

void stuff(void);
void f(int *p, int x)
{
        int *q = p + x;
        if (!q)
                stuff();
}

should never call stuff() - the test is unnecessary since pointer +/- integer is
undefined when the pointer does not point to an object or just past the end of
one (6.5.6 paragraph 8). This is important in cases such as:

static inline struct foo *lookup(struct foo *table, int x)
{
    if (match(table, x))
        return table + x;
    else
        return NULL;
}
...
    struct foo *e = lookup(tbl, x);
    if (e) ...

The code that calls the above function ends up checking for NULL twice: once
inside the (inlined) function, and one after the call. Were Q = P +- I
recognised as implying that P != NULL, Q != NULL (as we are allowed to do
according to the Standard), then the extraneous NULL test could be eliminated.

-- 
           Summary: pointer +- integer is never NULL
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mattias at virtutech dot se
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22485

Reply via email to