Skip to content

Commit e830d88

Browse files
authored
[UnsafeBufferUsage] Check for uninstantiated default arguments to prevent crash. (#188817)
Fix a crash introduced by #184899 The -Wunsafe-buffer-usage analysis was crashing when it encountered a template function with a default argument that hadn't been instantiated yet. This occurred in populateStmtsForFindingGadgets when it attempted to access the default argument of each parameter. This fix adds a check to ensure the default argument is instantiated before attempting to access it. Assisted-by: Gemini
1 parent 0d3e514 commit e830d88

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,8 @@ static void populateStmtsForFindingGadgets(SmallVector<const Stmt *> &Stmts,
29602960
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
29612961
AddStmt(FD->getBody());
29622962
for (const auto *PD : FD->parameters())
2963-
AddStmt(PD->getDefaultArg());
2963+
if (PD->hasDefaultArg() && !PD->hasUninstantiatedDefaultArg())
2964+
AddStmt(PD->getDefaultArg());
29642965
if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(FD))
29652966
llvm::append_range(
29662967
Stmts, llvm::map_range(CtorD->inits(),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -Wunsafe-buffer-usage -std=c++23 %s -verify
2+
3+
// expected-no-diagnostics
4+
5+
template <class _CharT> struct basic_string {
6+
struct __rep {
7+
__rep(int);
8+
};
9+
basic_string(_CharT, __rep = int()) {}
10+
};
11+
char16_t operators___str;
12+
decltype(sizeof(int)) operators___len;
13+
void operators() { basic_string(operators___str, operators___len); }

0 commit comments

Comments
 (0)