1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-15 12:16:40 +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
FILE* fout = cmsys::SystemTools::Fopen(outFileName, "w");
if (!fout) {
std::ostringstream e;
/* clang-format off */
e << "Failed to open\n"
" " << outFileName << "\n"
<< cmSystemTools::GetLastSystemError();
/* clang-format on */
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("Failed to open\n"
" ",
outFileName, '\n', cmSystemTools::GetLastSystemError()));
return cm::nullopt;
}

View File

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

View File

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

View File

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

View File

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