From e2256516e818c7b996cf970538ea9b82fd6d5a1f Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Sun, 13 Oct 2024 10:47:57 -0700 Subject: [PATCH] Fix InitLLVM argv (#4405) `args_.push_back(nullptr);` can resize `args_`, invalidating `argv`. The order needs to be switched. --- common/init_llvm.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/init_llvm.cpp b/common/init_llvm.cpp index 55baf0eec447..be54e0ec8184 100644 --- a/common/init_llvm.cpp +++ b/common/init_llvm.cpp @@ -17,13 +17,13 @@ InitLLVM::InitLLVM(int& argc, char**& argv) // make a copy of the argv that LLVM produces in order to support // mutation. args_(argv, argv + argc) { - // Return our mutable copy of argv for the program to use. - argc = args_.size(); - argv = args_.data(); - - // `argv[argc]` is expected to be a null pointer. + // `argv[argc]` is expected to be a null pointer (may reallocate `args_`). args_.push_back(nullptr); + // Return our mutable copy of argv for the program to use. + argc = args_.size() - 1; + argv = args_.data(); + llvm::setBugReportMsg( "Please report issues to " "https://github.com/carbon-language/carbon-lang/issues and include the "