Browse Source

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.
Ben Boeckel 1 year ago
parent
commit
a820877d03

+ 5 - 7
Source/cmCoreTryCompile.cxx

@@ -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;
-      /* 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;
       return cm::nullopt;
     }
     }
 
 

+ 5 - 7
Source/cmExportCommand.cxx

@@ -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;
-      /* 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'));
     }
     }
   }
   }
 }
 }

+ 5 - 6
Source/cmFileCommand.cxx

@@ -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;
-    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()) {
     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;
   }
   }
 
 

+ 3 - 4
Source/cmFileCopier.cxx

@@ -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;
-  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;
   return false;
 }
 }
 
 

+ 2 - 4
Source/cmake.cxx

@@ -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;
-    ss << "Error opening trace file " << file << ": "
-       << cmSystemTools::GetLastSystemError();
-    cmSystemTools::Error(ss.str());
+    cmSystemTools::Error(cmStrCat("Error opening trace file ", file, ": ",
+                                  cmSystemTools::GetLastSystemError()));
     return;
     return;
   }
   }
   std::cout << "Trace will be written to " << file << '\n';
   std::cout << "Trace will be written to " << file << '\n';