From: Joel Rosdahl Date: Fri, 17 Jul 2020 17:46:40 +0000 (+0200) Subject: Avoid some format/free usage X-Git-Tag: v4.0~311 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a8b27d4d1da46d310d3095ddfe0362546bc47581;p=thirdparty%2Fccache.git Avoid some format/free usage --- diff --git a/src/ccache.cpp b/src/ccache.cpp index e4d3d7f36..6bb9235d6 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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) { diff --git a/src/execute.cpp b/src/execute.cpp index 16544b08e..bbf9ceb77 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -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(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) { diff --git a/src/hashutil.cpp b/src/hashutil.cpp index 1b5ba2a80..432141abc 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -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(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; }