Skip to content

Commit 8bf19ec

Browse files
authored
[clang] Fix use of dangling ptr in CommandLineTest (#119798)
If 'GeneratedArgsStorage' ever grows, contained strings may get copied and data pointers stored in 'GeneratedArgs' may become invalid, pointing to deallocated memory.
1 parent 84b0f01 commit 8bf19ec

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

clang/unittests/Frontend/CompilerInvocationTest.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ class CommandLineTest : public ::testing::Test {
3131
public:
3232
IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
3333
SmallVector<const char *, 32> GeneratedArgs;
34-
SmallVector<std::string, 32> GeneratedArgsStorage;
34+
BumpPtrAllocator Alloc;
35+
StringSaver StringPool;
3536
CompilerInvocation Invocation;
3637

3738
const char *operator()(const Twine &Arg) {
38-
return GeneratedArgsStorage.emplace_back(Arg.str()).c_str();
39+
return StringPool.save(Arg).data();
3940
}
4041

4142
CommandLineTest()
4243
: Diags(CompilerInstance::createDiagnostics(
4344
*llvm::vfs::getRealFileSystem(), new DiagnosticOptions(),
44-
new TextDiagnosticBuffer())) {}
45+
new TextDiagnosticBuffer())),
46+
StringPool(Alloc) {}
4547
};
4648

4749
template <typename M>

0 commit comments

Comments
 (0)