Skip to content

Commit be48c0d

Browse files
authored
[-Wunsafe-buffer-usage] Fix a bug that wrongly assumed CXXMethodDecl always has an identifier (llvm#137248)
Fix a bug in UnsafeBufferUsage.cpp that wrongly assumed that CXXMethodDecl always has an identifier. rdar://149071318
1 parent 7122d9c commit be48c0d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ static bool isNullTermPointer(const Expr *Ptr) {
675675
const CXXMethodDecl *MD = MCE->getMethodDecl();
676676
const CXXRecordDecl *RD = MCE->getRecordDecl()->getCanonicalDecl();
677677

678-
if (MD && RD && RD->isInStdNamespace())
678+
if (MD && RD && RD->isInStdNamespace() && MD->getIdentifier())
679679
if (MD->getName() == "c_str" && RD->getName() == "basic_string")
680680
return true;
681681
}

clang/test/SemaCXX/bug149071318.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
2+
// RUN: -verify %s
3+
4+
// This example uncovered a bug in UnsafeBufferUsage.cpp, where the
5+
// code assumed that a CXXMethodDecl always have an identifier.
6+
7+
int printf( const char* format, char *); // <-- Fake decl of `printf`; to reproduce the bug, this example needs an implicit cast within a printf call.
8+
9+
namespace std { // fake std namespace; to reproduce the bug, a CXXConversionDecl needs to be in std namespace.
10+
class X {
11+
char * p;
12+
public:
13+
operator char*() {return p;}
14+
};
15+
16+
class Y {
17+
public:
18+
X x;
19+
};
20+
21+
}
22+
23+
void test(std::Y &y) {
24+
// Here `y.x` involves an implicit cast and calls the overloaded cast operator, which has no identifier:
25+
printf("%s", y.x); // expected-warning{{function 'printf' is unsafe}} expected-note{{}}
26+
}

0 commit comments

Comments
 (0)