[PATCH] Fix to PR5683 - issue diagnostic for pointer subtraction with type of size zero.
John McCall
rjmccall at apple.com
Tue Apr 9 13:58:54 PDT 2013
On Apr 9, 2013, at 6:37 AM, Serge Pavlov <sepavloff at gmail.com> wrote:
> Hi rsmith,
>
> http://llvm-reviews.chandlerc.com/D637
>
> CHANGE SINCE LAST DIFF
> http://llvm-reviews.chandlerc.com/D637?vs=1554&id=1563#toc
>
> Files:
> include/clang/Basic/DiagnosticSemaKinds.td
> lib/AST/ExprConstant.cpp
> lib/Sema/SemaExpr.cpp
> test/Sema/empty1.c
>
> Index: include/clang/Basic/DiagnosticSemaKinds.td
> ===================================================================
> --- include/clang/Basic/DiagnosticSemaKinds.td
> +++ include/clang/Basic/DiagnosticSemaKinds.td
> @@ -3976,6 +3976,9 @@
> def warn_offsetof_non_standardlayout_type : ExtWarn<
> "offset of on non-standard-layout type %0">, InGroup<InvalidOffsetof>;
> def err_offsetof_bitfield : Error<"cannot compute offset of bit-field %0">;
> +def warn_sub_ptr_zero_size_types : Warning<
> + "subtraction of pointers to type %0 with zero size has undefined behavior">,
> + InGroup<PointerArith>;
>
> def warn_floatingpoint_eq : Warning<
> "comparing floating point with == or != is unsafe">,
> Index: lib/AST/ExprConstant.cpp
> ===================================================================
> --- lib/AST/ExprConstant.cpp
> +++ lib/AST/ExprConstant.cpp
> @@ -5003,6 +5003,12 @@
> if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))
> return false;
>
> + // Empty struct or union in C has size 0 (GCC extension). Meaning of
> + // pointer difference in such case is unspecified, so set ElementSize
> + // to 1 to avoid division by zero.
> + if (ElementSize.isZero())
> + ElementSize = CharUnits::One();
> +
> // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
> // and produce incorrect results when it overflows. Such behavior
> // appears to be non-conforming, but is common, so perhaps we should
> Index: lib/Sema/SemaExpr.cpp
> ===================================================================
> --- lib/Sema/SemaExpr.cpp
> +++ lib/Sema/SemaExpr.cpp
> @@ -6734,6 +6734,20 @@
> LHS.get(), RHS.get()))
> return QualType();
>
> + if (!getLangOpts().CPlusPlus) {
> + // If pointee type is a structure or union of zero size (GCC extension),
> + // the subtraction does not make sense.
> + if (!rpointee.getTypePtr()->isVoidType() &&
> + !rpointee.getTypePtr()->isFunctionType()) {
None of the uses of getTypePtr() in this patch are necessary. You can just
do rpointee->isVoidType(), etc.
John.
More information about the cfe-commits
mailing list