LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 44343 - -Warray-bounds false negative when casting an out-of-bounds array item
Summary: -Warray-bounds false negative when casting an out-of-bounds array item
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Frontend (show other bugs)
Version: trunk
Hardware: PC Linux
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-19 08:49 PST by Ilya Mirsky
Modified: 2021-02-03 05:57 PST (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ilya Mirsky 2019-12-19 08:49:50 PST
For the following code in array-bounds.cpp

  unsigned int arr[2] = {0, 1};
  int i1 = arr[4];
  int i2 = (int)(arr[4]);
  int i3 = static_cast<int>(arr[4]);

Clang warns only for `i1`'s assignment. When the array item is casted, no out of bounds warning is presented.

  $ clang -cc1 -Warray-bounds array-bounds.cpp
  array-bounds.cpp:2:10: warning: array index 4 is past the end of the array 
  (which contains 2 elements)
  int i1 = arr[4];
           ^   ~
  array-bounds.cpp:1:1: note: array 'arr' declared here
  unsigned int arr[2] = {0, 1};
  ^
  1 warning generated.


This is not a duplicate of PR10771, as in the latter the subscript operator is applied on the casted array, whereas in this case, the subscript operator is applied first and then casting applied on the result.
Comment 1 Ilya Mirsky 2019-12-19 09:29:24 PST
A fix is up for review: https://reviews.llvm.org/D71714
Comment 2 Vince Bridgers 2021-02-03 05:57:42 PST
Landed the following commit

commit e48f444751cf781c42934b242b81f549da77bad0
Author: Ilya Mirsky <ilya.mirsky@ericsson.com>
Date:   Sun Jan 17 18:02:44 2021 -0600
    [Sema] Fix -Warray-bounds false negative when casting an out-of-bounds array item
    Patch by Ilya Mirsky!
    Fixes: http://llvm.org/PR44343
    Reviewed By: rsmith
    Differential Revision: https://reviews.llvm.org/D71714

rsmith requested to carefully watch this change, and be prepared to revert if necessary. Closing this ticket for now.