Skip to content

Commit 1a00a60

Browse files
committed
[-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 (cherry picked from commit be48c0d)
1 parent 5441e0c commit 1a00a60

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
@@ -1350,7 +1350,7 @@ static bool isNullTermPointer(const Expr *Ptr, ASTContext &Ctx) {
13501350
const CXXMethodDecl *MD = MCE->getMethodDecl();
13511351
const CXXRecordDecl *RD = MCE->getRecordDecl()->getCanonicalDecl();
13521352

1353-
if (MD && RD && RD->isInStdNamespace())
1353+
if (MD && RD && RD->isInStdNamespace() && MD->getIdentifier())
13541354
if (MD->getName() == "c_str" && RD->getName() == "basic_string")
13551355
return true;
13561356
}

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)