[clang] f6c54cd - [-Wunsafe-buffer-usage] Ignore array subscript on literal zero
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 16 21:04:52 PST 2022
Author: ziqingluo-90
Date: 2022-12-16T21:04:41-08:00
New Revision: f6c54cdbc4394cd32f0dde990f217ca8f534a136
URL: https://github.com/llvm/llvm-project/commit/f6c54cdbc4394cd32f0dde990f217ca8f534a136
DIFF: https://github.com/llvm/llvm-project/commit/f6c54cdbc4394cd32f0dde990f217ca8f534a136.diff
LOG: [-Wunsafe-buffer-usage] Ignore array subscript on literal zero
Unsafe Buffer Usage analysis only warns unsafe buffer accesses but not
pointer dereferences. An array subscript on a literal zero is
equivalent to dereference a pointer thus we do not want to warn it.
Reviewed By: NoQ
Differential Revision: https://reviews.llvm.org/D138321
Added:
Modified:
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index ba459d2176ad..85449c4c5b1c 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -189,9 +189,9 @@ class ArraySubscriptGadget : public UnsafeGadget {
static Matcher matcher() {
// FIXME: What if the index is integer literal 0? Should this be
// a safe gadget in this case?
- return stmt(
- arraySubscriptExpr(hasBase(ignoringParenImpCasts(hasPointerType())))
- .bind(ArraySubscrTag));
+ return stmt(arraySubscriptExpr(hasBase(ignoringParenImpCasts(hasPointerType())),
+ unless(hasIndex(integerLiteral(equals(0)))))
+ .bind(ArraySubscrTag));
}
const ArraySubscriptExpr *getBaseStmt() const override { return ASE; }
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
index bff9fd666ac6..9b1481e8a2ed 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
@@ -29,10 +29,10 @@ void * voidPtrCall(void);
char * charPtrCall(void);
void testArraySubscripts(int *p, int **pp) {
- foo(p[0], // expected-warning{{unchecked operation on raw buffer in expression}}
- pp[0][0], // expected-warning2{{unchecked operation on raw buffer in expression}}
- 0[0[pp]], // expected-warning2{{unchecked operation on raw buffer in expression}}
- 0[pp][0] // expected-warning2{{unchecked operation on raw buffer in expression}}
+ foo(p[1], // expected-warning{{unchecked operation on raw buffer in expression}}
+ pp[1][1], // expected-warning2{{unchecked operation on raw buffer in expression}}
+ 1[1[pp]], // expected-warning2{{unchecked operation on raw buffer in expression}}
+ 1[pp][1] // expected-warning2{{unchecked operation on raw buffer in expression}}
);
if (p[3]) { // expected-warning{{unchecked operation on raw buffer in expression}}
@@ -50,11 +50,18 @@ void testArraySubscripts(int *p, int **pp) {
int a[10], b[10][10];
// Not to warn subscripts on arrays
- foo(a[0], a[1],
- 0[a], 1[a],
+ foo(a[1], 1[a],
b[3][4],
4[b][3],
4[3[b]]);
+
+ // Not to warn when index is zero
+ foo(p[0], pp[0][0], 0[0[pp]], 0[pp][0],
+ ((int*)voidPtrCall())[0],
+ 0[(int*)voidPtrCall()],
+ charPtrCall()[0],
+ 0[charPtrCall()]
+ );
}
void testArraySubscriptsWithAuto(int *p, int **pp) {
@@ -62,19 +69,19 @@ void testArraySubscriptsWithAuto(int *p, int **pp) {
auto ap1 = a;
- foo(ap1[0]); // expected-warning{{unchecked operation on raw buffer in expression}}
+ foo(ap1[1]); // expected-warning{{unchecked operation on raw buffer in expression}}
auto ap2 = p;
- foo(ap2[0]); // expected-warning{{unchecked operation on raw buffer in expression}}
+ foo(ap2[1]); // expected-warning{{unchecked operation on raw buffer in expression}}
auto ap3 = pp;
- foo(ap3[0][0]); // expected-warning2{{unchecked operation on raw buffer in expression}}
+ foo(ap3[1][1]); // expected-warning2{{unchecked operation on raw buffer in expression}}
auto ap4 = *pp;
- foo(ap4[0]); // expected-warning{{unchecked operation on raw buffer in expression}}
+ foo(ap4[1]); // expected-warning{{unchecked operation on raw buffer in expression}}
}
void testUnevaluatedContext(int * p) {
More information about the cfe-commits
mailing list