]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Avoid some format/free usage
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 17 Jul 2020 17:46:40 +0000 (19:46 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 17 Jul 2020 18:07:11 +0000 (20:07 +0200)
src/ccache.cpp
src/execute.cpp
src/hashutil.cpp

index e4d3d7f36a1a3c045f900d52a0a4fe7627da914d..6bb9235d66d72c25a5c150c6d2e274deca999750 100644 (file)
@@ -1167,12 +1167,11 @@ hash_nvcc_host_compiler(const Context& ctx,
 #endif
     for (const char* compiler : compilers) {
       if (ccbin) {
-        char* path = format("%s/%s", ccbin, compiler);
+        std::string path = fmt::format("{}/{}", ccbin, compiler);
         auto st = Stat::stat(path);
         if (st) {
-          hash_compiler(ctx, hash, st, path, false);
+          hash_compiler(ctx, hash, st, path.c_str(), false);
         }
-        free(path);
       } else {
         std::string path = find_executable(ctx, compiler, MYNAME);
         if (!path.empty()) {
@@ -1762,9 +1761,9 @@ create_initial_config_file(Config& config)
 
   unsigned max_files;
   uint64_t max_size;
-  char* stats_dir = format("%s/0", config.cache_dir().c_str());
+  std::string stats_dir = fmt::format("{}/0", config.cache_dir());
   if (Stat::stat(stats_dir)) {
-    stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
+    stats_get_obsolete_limits(stats_dir.c_str(), &max_files, &max_size);
     // STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory.
     max_files *= 16;
     max_size *= 16;
@@ -1772,7 +1771,6 @@ create_initial_config_file(Config& config)
     max_files = 0;
     max_size = config.max_size();
   }
-  free(stats_dir);
 
   FILE* f = fopen(config.primary_config_path().c_str(), "w");
   if (!f) {
index 16544b08e8441594346a73c3018edad5c7a54254..bbf9ceb77f14f284d116d21446728dc0aa08fff4 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "Config.hpp"
 #include "Context.hpp"
+#include "Fd.hpp"
 #include "SignalHandler.hpp"
 #include "Stat.hpp"
 #include "Util.hpp"
@@ -194,22 +195,36 @@ win32execute(const char* path,
   add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext, path);
   BOOL ret = FALSE;
   if (length > 8192) {
-    char* tmp_file = format("%s.tmp", path);
-    FILE* fp = create_tmp_file(&tmp_file, "w");
-    char atfile[MAX_PATH + 3];
-    fwrite(args, 1, length, fp);
-    if (ferror(fp)) {
+    auto fd_and_path = Util::create_temp_fd(path);
+    Fd fd(fd_and_path.first);
+    const char* tmp_file = fd_and_path.second.c_str();
+    if (!write_fd(*fd, args, length)) {
       cc_log("Error writing @file; this command will probably fail: %s", args);
     }
-    fclose(fp);
-    snprintf(atfile, sizeof(atfile), "\"@%s\"", tmp_file);
-    ret = CreateProcess(NULL, atfile, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+    std::string atfile = fmt::format("\"@{}\"", tmp_file);
+    ret = CreateProcess(nullptr,
+                        const_cast<char*>(atfile.c_str()),
+                        nullptr,
+                        nullptr,
+                        1,
+                        0,
+                        nullptr,
+                        nullptr,
+                        &si,
+                        &pi);
     Util::unlink_tmp(tmp_file);
-    free(tmp_file);
   }
   if (!ret) {
-    ret = CreateProcess(
-      full_path_win_ext, args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+    ret = CreateProcess(full_path_win_ext,
+                        args,
+                        nullptr,
+                        nullptr,
+                        1,
+                        0,
+                        nullptr,
+                        nullptr,
+                        &si,
+                        &pi);
   }
   if (fd_stdout != -1) {
     close(fd_stdout);
@@ -321,12 +336,15 @@ find_executable_in_path(const char* name,
 #ifdef _WIN32
     char namebuf[MAX_PATH];
     int ret =
-      SearchPath(dir.c_str(), name, NULL, sizeof(namebuf), namebuf, NULL);
+      SearchPath(dir.c_str(), name, nullptr, sizeof(namebuf), namebuf, nullptr);
     if (!ret) {
-      char* exename = format("%s.exe", name);
-      ret =
-        SearchPath(dir.c_str(), exename, NULL, sizeof(namebuf), namebuf, NULL);
-      free(exename);
+      std::string exename = fmt::format("{}.exe", name);
+      ret = SearchPath(dir.c_str(),
+                       exename.c_str(),
+                       nullptr,
+                       sizeof(namebuf),
+                       namebuf,
+                       nullptr);
     }
     (void)exclude_name;
     if (ret) {
index 1b5ba2a8040eac7c7646babe2447721006714c7c..432141abc538c9e848581b3e774e83df5f136322 100644 (file)
@@ -385,18 +385,21 @@ hash_command_output(Hash& hash, const char* command, const char* compiler)
   }
 
   // Add "echo" command.
-  bool cmd;
+  bool using_cmd_exe;
+  std::string adjusted_command;
   if (str_startswith(command, "echo")) {
-    command = format("cmd.exe /c \"%s\"", command);
-    cmd = true;
+    adjusted_command = fmt::format("cmd.exe /c \"{}\"", command);
+    using_cmd_exe = true;
   } else if (str_startswith(command, "%compiler%")
              && str_eq(compiler, "echo")) {
-    command = format("cmd.exe /c \"%s%s\"", compiler, command + 10);
-    cmd = true;
+    adjusted_command =
+      fmt::format("cmd.exe /c \"{}{}\"", compiler, command + 10);
+    using_cmd_exe = true;
   } else {
-    command = x_strdup(command);
-    cmd = false;
+    adjusted_command = command;
+    using_cmd_exe = false;
   }
+  command = adjusted_command.c_str();
 #endif
 
   Args args = Args::from_string(command);
@@ -437,21 +440,27 @@ hash_command_output(Hash& hash, const char* command, const char* compiler)
   si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
   si.dwFlags = STARTF_USESTDHANDLES;
 
-  char* win32args;
-  if (!cmd) {
+  std::string win32args;
+  if (using_cmd_exe) {
+    win32args = command; // quoted
+  } else {
     int length;
     const char* prefix = sh.empty() ? nullptr : sh.c_str();
-    win32args = win32argvtos(prefix, argv.data(), &length);
-  } else {
-    win32args = (char*)command; // quoted
+    char* args = win32argvtos(prefix, argv.data(), &length);
+    win32args = args;
+    free(args);
   }
-  BOOL ret = CreateProcess(
-    path.c_str(), win32args, NULL, NULL, 1, 0, NULL, NULL, &si, &pi);
+  BOOL ret = CreateProcess(path.c_str(),
+                           const_cast<char*>(win32args.c_str()),
+                           nullptr,
+                           nullptr,
+                           1,
+                           0,
+                           nullptr,
+                           nullptr,
+                           &si,
+                           &pi);
   CloseHandle(pipe_out[1]);
-  free(win32args);
-  if (!cmd) {
-    free((char*)command); // Original argument was replaced above.
-  }
   if (ret == 0) {
     return false;
   }