1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 22:37:30 +08:00

errors: avoid constructing a stream before getting the last error

Constructing a stream may involve operations that change the global
error state. Avoid the streams by using `cmStrCat` instead.
This commit is contained in:
Ben Boeckel
2023-12-01 22:45:18 -05:00
parent 5cf7018af6
commit a820877d03
5 changed files with 20 additions and 28 deletions

View File

@@ -626,13 +626,11 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
// now create a CMakeLists.txt file in that directory // now create a CMakeLists.txt file in that directory
FILE* fout = cmsys::SystemTools::Fopen(outFileName, "w"); FILE* fout = cmsys::SystemTools::Fopen(outFileName, "w");
if (!fout) { if (!fout) {
std::ostringstream e; this->Makefile->IssueMessage(
/* clang-format off */ MessageType::FATAL_ERROR,
e << "Failed to open\n" cmStrCat("Failed to open\n"
" " << outFileName << "\n" " ",
<< cmSystemTools::GetLastSystemError(); outFileName, '\n', cmSystemTools::GetLastSystemError()));
/* clang-format on */
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
return cm::nullopt; return cm::nullopt;
} }

View File

@@ -385,13 +385,11 @@ static void StorePackageRegistry(cmMakefile& mf, std::string const& package,
if (entry) { if (entry) {
entry << content << "\n"; entry << content << "\n";
} else { } else {
std::ostringstream e; mf.IssueMessage(MessageType::WARNING,
/* clang-format off */ cmStrCat("Cannot create package registry file:\n"
e << "Cannot create package registry file:\n" " ",
<< " " << fname << "\n" fname, '\n',
<< cmSystemTools::GetLastSystemError() << "\n"; cmSystemTools::GetLastSystemError(), '\n'));
/* clang-format on */
mf.IssueMessage(MessageType::WARNING, e.str());
} }
} }
} }

View File

@@ -3022,16 +3022,15 @@ bool HandleCreateLinkCommand(std::vector<std::string> const& args,
// Check if the new file already exists and remove it. // Check if the new file already exists and remove it.
if (cmSystemTools::PathExists(newFileName) && if (cmSystemTools::PathExists(newFileName) &&
!cmSystemTools::RemoveFile(newFileName)) { !cmSystemTools::RemoveFile(newFileName)) {
std::ostringstream e; auto err = cmStrCat("Failed to create link '", newFileName,
e << "Failed to create link '" << newFileName "' because existing path cannot be removed: ",
<< "' because existing path cannot be removed: " cmSystemTools::GetLastSystemError(), '\n');
<< cmSystemTools::GetLastSystemError() << "\n";
if (!arguments.Result.empty()) { if (!arguments.Result.empty()) {
status.GetMakefile().AddDefinition(arguments.Result, e.str()); status.GetMakefile().AddDefinition(arguments.Result, err);
return true; return true;
} }
status.SetError(e.str()); status.SetError(err);
return false; return false;
} }

View File

@@ -119,10 +119,9 @@ std::string const& cmFileCopier::ToName(std::string const& fromName)
bool cmFileCopier::ReportMissing(const std::string& fromFile) bool cmFileCopier::ReportMissing(const std::string& fromFile)
{ {
// The input file does not exist and installation is not optional. // The input file does not exist and installation is not optional.
std::ostringstream e; this->Status.SetError(cmStrCat(this->Name, " cannot find \"", fromFile,
e << this->Name << " cannot find \"" << fromFile "\": ", cmSystemTools::GetLastSystemError(),
<< "\": " << cmSystemTools::GetLastSystemError() << "."; '.'));
this->Status.SetError(e.str());
return false; return false;
} }

View File

@@ -1707,10 +1707,8 @@ void cmake::SetTraceFile(const std::string& file)
this->TraceFile.close(); this->TraceFile.close();
this->TraceFile.open(file.c_str()); this->TraceFile.open(file.c_str());
if (!this->TraceFile) { if (!this->TraceFile) {
std::stringstream ss; cmSystemTools::Error(cmStrCat("Error opening trace file ", file, ": ",
ss << "Error opening trace file " << file << ": " cmSystemTools::GetLastSystemError()));
<< cmSystemTools::GetLastSystemError();
cmSystemTools::Error(ss.str());
return; return;
} }
std::cout << "Trace will be written to " << file << '\n'; std::cout << "Trace will be written to " << file << '\n';