Skip to content

[-Wunsafe-buffer-usage] Fix a bug that wrongly assumed CXXMethodDecl always has an identifier #137248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/Analysis/UnsafeBufferUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ static bool isNullTermPointer(const Expr *Ptr) {
const CXXMethodDecl *MD = MCE->getMethodDecl();
const CXXRecordDecl *RD = MCE->getRecordDecl()->getCanonicalDecl();

if (MD && RD && RD->isInStdNamespace())
if (MD && RD && RD->isInStdNamespace() && MD->getIdentifier())
if (MD->getName() == "c_str" && RD->getName() == "basic_string")
return true;
}
Expand Down
26 changes: 26 additions & 0 deletions clang/test/SemaCXX/bug149071318.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
// RUN: -verify %s

// This example uncovered a bug in UnsafeBufferUsage.cpp, where the
// code assumed that a CXXMethodDecl always have an identifier.

int printf( const char* format, char *); // <-- Fake decl of `printf`; to reproduce the bug, this example needs an implicit cast within a printf call.

namespace std { // fake std namespace; to reproduce the bug, a CXXConversionDecl needs to be in std namespace.
class X {
char * p;
public:
operator char*() {return p;}
};

class Y {
public:
X x;
};

}

void test(std::Y &y) {
// Here `y.x` involves an implicit cast and calls the overloaded cast operator, which has no identifier:
printf("%s", y.x); // expected-warning{{function 'printf' is unsafe}} expected-note{{}}
}
Loading