[PATCH] D71714: [Sema] Fix -Warray-bounds false negative when casting an out-of-bounds array item

Bevin Hansson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 16 07:21:09 PST 2020


ebevhan added inline comments.


================
Comment at: clang/lib/Sema/SemaChecking.cpp:13384
       case Stmt::MemberExprClass: {
         expr = cast<MemberExpr>(expr)->getBase();
         break;
----------------
ilya wrote:
> rsmith wrote:
> > ilya wrote:
> > > rsmith wrote:
> > > > Hmm, don't we need to do different things for dot and arrow in this case?
> > > There are several test cases for an out of bounds access on an array member using dot and arrow operators in array-bounds.cpp. Do you have a specific test case for which you think the code is broken?
> > > There are several test cases for an out of bounds access on an array member using dot and arrow operators in array-bounds.cpp. Do you have a specific test case for which you think the code is broken?
> > 
> > Sure. There's a false negative for this:
> > 
> > ```
> > struct A { int n; };
> > A *a[4];
> > int *n = &a[4]->n;
> > ```
> > 
> > ... because we incorrectly visit the left-hand side of the `->` with `AllowOnePastEnd == 1`. The left-hand side of `->` is subject to lvalue-to-rvalue conversion, so can't be one-past-the-end regardless of the context in which the `->` appears.
> > ... because we incorrectly visit the left-hand side of the -> with AllowOnePastEnd == 1. The left-hand side of -> is subject to lvalue-to-rvalue conversion, so can't be one-past-the-end regardless of the context in which the -> appears.
> 
> Thanks for clarifying. So basically we don't want to allow one-past-end for MemberExpr?
> Thanks for clarifying. So basically we don't want to allow one-past-end for MemberExpr?

I think the point is rather that when the MemberExpr isArrow, we want to think of it as performing an implicit dereference first, and thus we should do `AllowOnePastEnd--` before recursing if that is the case.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71714/new/

https://reviews.llvm.org/D71714





More information about the cfe-commits mailing list