Skip to content

Commit 090dc77

Browse files
authored
[-Wunsafe-buffer-usage] Fix a bug and suppress libc warnings for C files (#109496)
- Fix a bug in UnsafeBufferUsage.cpp related to casting to PointerType - Suppress -Wunsafe-buffer-usage-in-libc-call for C files (rdar://117182250)
1 parent 19ecded commit 090dc77

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ AST_MATCHER_P(Stmt, ignoreUnsafeBufferInContainer,
250250

251251
AST_MATCHER_P(Stmt, ignoreUnsafeLibcCall, const UnsafeBufferUsageHandler *,
252252
Handler) {
253-
return Handler->ignoreUnsafeBufferInLibcCall(Node.getBeginLoc());
253+
if (Finder->getASTContext().getLangOpts().CPlusPlus)
254+
return Handler->ignoreUnsafeBufferInLibcCall(Node.getBeginLoc());
255+
return true; /* Only warn about libc calls for C++ */
254256
}
255257

256258
AST_MATCHER_P(CastExpr, castSubExpr, internal::Matcher<Expr>, innerMatcher) {
@@ -784,12 +786,12 @@ AST_MATCHER_P(CallExpr, hasUnsafePrintfStringArg,
784786
return false; // possibly some user-defined printf function
785787

786788
ASTContext &Ctx = Finder->getASTContext();
787-
QualType FristParmTy = FD->getParamDecl(0)->getType();
789+
QualType FirstParmTy = FD->getParamDecl(0)->getType();
788790

789-
if (!FristParmTy->isPointerType())
791+
if (!FirstParmTy->isPointerType())
790792
return false; // possibly some user-defined printf function
791793

792-
QualType FirstPteTy = (cast<PointerType>(FristParmTy))->getPointeeType();
794+
QualType FirstPteTy = FirstParmTy->castAs<PointerType>()->getPointeeType();
793795

794796
if (!Ctx.getFILEType()
795797
.isNull() && //`FILE *` must be in the context if it is fprintf
@@ -865,7 +867,7 @@ AST_MATCHER(CallExpr, hasUnsafeSnprintfBuffer) {
865867
if (!FirstParmTy->isPointerType())
866868
return false; // Not an snprint
867869

868-
QualType FirstPteTy = cast<PointerType>(FirstParmTy)->getPointeeType();
870+
QualType FirstPteTy = FirstParmTy->castAs<PointerType>()->getPointeeType();
869871
const Expr *Buf = Node.getArg(0), *Size = Node.getArg(1);
870872

871873
if (FirstPteTy.isConstQualified() || !Buf->getType()->isPointerType() ||

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2581,7 +2581,8 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
25812581
!Diags.isIgnored(diag::warn_unsafe_buffer_variable, SourceLocation()) ||
25822582
!Diags.isIgnored(diag::warn_unsafe_buffer_usage_in_container,
25832583
SourceLocation()) ||
2584-
!Diags.isIgnored(diag::warn_unsafe_buffer_libc_call, SourceLocation())) {
2584+
(!Diags.isIgnored(diag::warn_unsafe_buffer_libc_call, SourceLocation()) &&
2585+
S.getLangOpts().CPlusPlus /* only warn about libc calls in C++ */)) {
25852586
CallableVisitor(CallAnalyzers).TraverseTranslationUnitDecl(TU);
25862587
}
25872588
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
22
// RUN: -verify %s
3+
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
4+
// RUN: -verify %s -x objective-c++
35
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
46
// RUN: -verify %s
57

@@ -56,6 +58,11 @@ namespace std {
5658
}
5759

5860
void f(char * p, char * q, std::span<char> s, std::span<char> s2) {
61+
typedef FILE * _Nullable aligned_file_ptr_t __attribute__((align_value(64)));
62+
typedef char * _Nullable aligned_char_ptr_t __attribute__((align_value(64)));
63+
aligned_file_ptr_t fp;
64+
aligned_char_ptr_t cp;
65+
5966
memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
6067
std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
6168
__builtin_memcpy(p, q, 64); // expected-warning{{function '__builtin_memcpy' is unsafe}}
@@ -71,9 +78,11 @@ void f(char * p, char * q, std::span<char> s, std::span<char> s2) {
7178
printf("%s%d", // expected-warning{{function 'printf' is unsafe}}
7279
p, // expected-note{{string argument is not guaranteed to be null-terminated}} note attached to the unsafe argument
7380
*p);
81+
printf(cp, p, *p); // expected-warning{{function 'printf' is unsafe}} // expected-note{{string argument is not guaranteed to be null-terminated}}
7482
sprintf(q, "%s%d", "hello", *p); // expected-warning{{function 'sprintf' is unsafe}} expected-note{{change to 'snprintf' for explicit bounds checking}}
7583
swprintf(q, "%s%d", "hello", *p); // expected-warning{{function 'swprintf' is unsafe}} expected-note{{change to 'snprintf' for explicit bounds checking}}
7684
snprintf(q, 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
85+
snprintf(cp, 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
7786
snprintf(s.data(), s2.size(), "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
7887
snwprintf(s.data(), s2.size(), "%s%d", "hello", *p); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
7988
snwprintf_s( // expected-warning{{function 'snwprintf_s' is unsafe}}
@@ -84,15 +93,18 @@ void f(char * p, char * q, std::span<char> s, std::span<char> s2) {
8493
sscanf(p, "%s%d", "hello", *p); // expected-warning{{function 'sscanf' is unsafe}}
8594
sscanf_s(p, "%s%d", "hello", *p); // expected-warning{{function 'sscanf_s' is unsafe}}
8695
fprintf((FILE*)p, "%P%d%p%i hello world %32s", *p, *p, p, *p, p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
96+
fprintf(fp, "%P%d%p%i hello world %32s", *p, *p, p, *p, p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
8797
wprintf(L"hello %s", p); // expected-warning{{function 'wprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
8898

99+
89100
char a[10], b[11];
90101
int c[10];
91102
std::wstring WS;
92103

93104
snprintf(a, sizeof(b), "%s", __PRETTY_FUNCTION__); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
94105
snprintf((char*)c, sizeof(c), "%s", __PRETTY_FUNCTION__); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
95106
fprintf((FILE*)p, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // no warn
107+
fprintf(fp, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // no warn
96108
printf("%s%d", "hello", *p); // no warn
97109
snprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // no warn
98110
snprintf(s.data(), s.size_bytes(), "%s%d", __PRETTY_FUNCTION__, *p); // no warn
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -Wunsafe-buffer-usage %s -verify %s -x c
2+
// RUN: %clang_cc1 -Wunsafe-buffer-usage %s -verify %s -x objective-c
3+
4+
void* __asan_memcpy(void *dst,const void *src, unsigned long size);
5+
6+
void f(int *p, int *q) {
7+
8+
__asan_memcpy(p, q, 10); // no libc warn in C
9+
++p[5]; // expected-warning{{unsafe buffer access}}
10+
}

0 commit comments

Comments
 (0)