Skip to content

Commit c6fa115

Browse files
authored
[clang][analyzer] Relax assertion for non-default address spaces in the cstring checker (#153498)
Prevent an assertion failure in the cstring checker when library functions like memcpy are defined with non-default address spaces. Adds a test for this case.
1 parent 0a7eabc commit c6fa115

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,9 +1129,9 @@ bool CStringChecker::isFirstBufInBound(CheckerContext &C, ProgramStateRef State,
11291129
if (!ER)
11301130
return true; // cf top comment.
11311131

1132-
// FIXME: Does this crash when a non-standard definition
1133-
// of a library function is encountered?
1134-
assert(ER->getValueType() == C.getASTContext().CharTy &&
1132+
// Support library functions defined with non-default address spaces
1133+
assert(ER->getValueType()->getCanonicalTypeUnqualified() ==
1134+
C.getASTContext().CharTy &&
11351135
"isFirstBufInBound should only be called with char* ElementRegions");
11361136

11371137
// Get the size of the array.
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
// RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
2-
// RUN: -analyzer-checker=core -verify %s
2+
// RUN: -Wno-incompatible-library-redeclaration \
3+
// RUN: -analyzer-checker=core,unix -verify %s
34

45
// expected-no-diagnostics
56
//
67
// By default, pointers are 64-bits.
8+
#define ADDRESS_SPACE_64BITS __attribute__((address_space(0)))
79
#define ADDRESS_SPACE_32BITS __attribute__((address_space(3)))
810

911
int test(ADDRESS_SPACE_32BITS int *p, ADDRESS_SPACE_32BITS void *q) {
1012
return p == q; // no-crash
1113
}
14+
15+
// Make sure that the cstring checker handles non-default address spaces
16+
ADDRESS_SPACE_64BITS void *
17+
memcpy(ADDRESS_SPACE_64BITS void *,
18+
ADDRESS_SPACE_32BITS const void *,
19+
long unsigned int);
20+
21+
ADDRESS_SPACE_64BITS struct {
22+
char m[16];
23+
} n;
24+
25+
void avoid_cstring_checker_crash(ADDRESS_SPACE_32BITS char *p) {
26+
memcpy(&n.m[0], p, 4); // no-crash
27+
}

0 commit comments

Comments
 (0)