[PATCH] Fix to PR5683 - issue diagnostic for pointer subtraction with type of size zero.

John McCall rjmccall at apple.com
Mon Apr 8 10:50:41 PDT 2013


On Apr 8, 2013, at 10:18 AM, Serge Pavlov <sepavloff at gmail.com> wrote:
> +        // If pointee type is a structure or union of zero size (GCC extension),
> +        // the subtraction does not make sense.
> +        CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);
> +        if (ElementSize.isZero() &&
> +            (rpointee.getTypePtr()->isStructureType() ||
> +             rpointee.getTypePtr()->isUnionType())) {

This is rpointee->isRecordType(), and it's much cheaper to check that
*before* getting the type size.

Also, this is only possible in C, so please add !getLangOpts().CPlusPlus to
your check.

You should also test what happens when you try to subtract pointers of
incomplete or ill-formed type:

  struct A;
  int test(A *x, A *y) { return (y - x); }

  struct B {
    void z;
  };
  int test(B *x, B *y) { return (y - x); }

John.



More information about the cfe-commits mailing list