Skip to content

Commit 35207ea

Browse files
author
mxms
committed
Detect when the buffer is a member access, fix tests
Add a case for when it's a member variable access and we can statically determine the size. Also add new test to ensure the change works reliably and update old tests to not expect this warning.
1 parent 8fed333 commit 35207ea

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
466466
// Array index wasn't an integer literal, let's see if it was an enum or
467467
// something similar
468468
const auto IntConst = Node.getIdx()->getIntegerConstantExpr(Finder->getASTContext());
469-
if (IntConst && *IntConst > 0 && *IntConst < size) {
469+
if (IntConst && *IntConst >= 0 && *IntConst < size) {
470470
return true;
471471
}
472472

clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ enum FooEnum {
4747
};
4848

4949
void constant_enum_safe() {
50-
int buffer[FooEnum::D] = { 0, 1, 2 };
50+
int buffer[FooEnum::D] = { 0, 1, 2 }; // expected-warning{{'buffer' is an unsafe buffer that does not perform bounds checks}}
51+
// expected-note@-1{{change type of 'buffer' to 'std::array' to label it for hardening}}
52+
buffer[A] = 0; // no-warning
5153
buffer[C] = 0; // no-warning
54+
buffer[D] = 0; // expected-note{{used in buffer access here}}
5255
}
5356

5457
void constant_enum_unsafe(FooEnum e) {

clang/test/SemaCXX/warn-unsafe-buffer-usage-field-attr.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ void test_attribute_multiple_fields (D d) {
9696

9797
int v = d.buf[0]; //expected-warning{{field 'buf' prone to unsafe buffer manipulation}}
9898

99-
//expected-warning@+1{{unsafe buffer access}}
10099
v = d.buf[5]; //expected-warning{{field 'buf' prone to unsafe buffer manipulation}}
101100
}
102101

clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,25 @@ T_t funRetT();
128128
T_t * funRetTStar();
129129

130130
void testStructMembers(struct T * sp, struct T s, T_t * sp2, T_t s2) {
131-
foo(sp->a[1], // expected-warning{{unsafe buffer access}}
131+
foo(sp->a[1],
132132
sp->b[1], // expected-warning{{unsafe buffer access}}
133-
sp->c.a[1], // expected-warning{{unsafe buffer access}}
133+
sp->c.a[1],
134134
sp->c.b[1], // expected-warning{{unsafe buffer access}}
135-
s.a[1], // expected-warning{{unsafe buffer access}}
135+
s.a[1],
136136
s.b[1], // expected-warning{{unsafe buffer access}}
137-
s.c.a[1], // expected-warning{{unsafe buffer access}}
137+
s.c.a[1],
138138
s.c.b[1], // expected-warning{{unsafe buffer access}}
139-
sp2->a[1], // expected-warning{{unsafe buffer access}}
139+
sp2->a[1],
140140
sp2->b[1], // expected-warning{{unsafe buffer access}}
141-
sp2->c.a[1], // expected-warning{{unsafe buffer access}}
141+
sp2->c.a[1],
142142
sp2->c.b[1], // expected-warning{{unsafe buffer access}}
143-
s2.a[1], // expected-warning{{unsafe buffer access}}
143+
s2.a[1],
144144
s2.b[1], // expected-warning{{unsafe buffer access}}
145-
s2.c.a[1], // expected-warning{{unsafe buffer access}}
145+
s2.c.a[1],
146146
s2.c.b[1], // expected-warning{{unsafe buffer access}}
147-
funRetT().a[1], // expected-warning{{unsafe buffer access}}
147+
funRetT().a[1],
148148
funRetT().b[1], // expected-warning{{unsafe buffer access}}
149-
funRetTStar()->a[1], // expected-warning{{unsafe buffer access}}
149+
funRetTStar()->a[1],
150150
funRetTStar()->b[1] // expected-warning{{unsafe buffer access}}
151151
);
152152
}
@@ -213,7 +213,6 @@ void testTypedefs(T_ptr_t p) {
213213
// expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
214214
foo(p[1], // expected-note{{used in buffer access here}}
215215
p[1].a[1], // expected-note{{used in buffer access here}}
216-
// expected-warning@-1{{unsafe buffer access}}
217216
p[1].b[1] // expected-note{{used in buffer access here}}
218217
// expected-warning@-1{{unsafe buffer access}}
219218
);

0 commit comments

Comments
 (0)