[PATCH] D138321: [-Wunsafe-buffer-usage] Ignore array subscript on literal zero
Ziqing Luo via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 18 12:29:57 PST 2022
ziqingluo-90 created this revision.
ziqingluo-90 added reviewers: NoQ, jkorous, t-rasmud, malavikasamak, aaron.ballman, gribozavr, xazax.hun.
Herald added a subscriber: rnkovacs.
Herald added a project: All.
ziqingluo-90 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Improving unsafe array subscript warning reporting.
For array subscripts with a literal zero index, no warning will be emitted.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D138321
Files:
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
Index: clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
===================================================================
--- clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
@@ -29,10 +29,10 @@
char * baz(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 @@
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*)bar())[0],
+ 0[(int*)bar()],
+ baz()[0],
+ 0[baz()]
+ );
}
void testArraySubscriptsWithAuto(int *p, int **pp) {
@@ -62,19 +69,19 @@
auto ap1 = a; // expected-warning{{variable 'ap1' participates in unchecked buffer operations}}
- foo(ap1[0]);
+ foo(ap1[1]);
auto ap2 = p; // expected-warning{{variable 'ap2' participates in unchecked buffer operations}}
- foo(ap2[0]);
+ foo(ap2[1]);
auto ap3 = pp; // expected-warning{{variable 'ap3' participates in unchecked buffer operations}}
- foo(ap3[0][0]); // expected-warning{{unchecked operation on raw buffer in expression}}
+ foo(ap3[1][1]); // expected-warning{{unchecked operation on raw buffer in expression}}
auto ap4 = *pp; // expected-warning{{variable 'ap4' participates in unchecked buffer operations}}
- foo(ap4[0]);
+ foo(ap4[1]);
}
void testQualifiedParameters(const int * p, const int * const q,
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===================================================================
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -228,10 +228,9 @@
}
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())))
+ arraySubscriptExpr(hasBase(ignoringParenImpCasts(hasPointerType())),
+ unless(hasIndex(integerLiteral(equals(0)))))
.bind("arraySubscr"));
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138321.476567.patch
Type: text/x-patch
Size: 3070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221118/00c77c0b/attachment.bin>
More information about the cfe-commits
mailing list