From 2aaf7fda2742ce186fa0797e71fa1d1f690232fa Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 13 May 2019 21:10:48 +0200 Subject: [PATCH] src/history: Stop renarrowing a string in a for-loop Classic case of doing stuff in a loop that doesn't change. No idea if the compiler caught it, but I sleep easier now. --- src/history.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/history.cpp b/src/history.cpp index 3805e59f0..30982d0a0 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -1285,14 +1285,16 @@ bool history_t::rewrite_to_temporary_file(int existing_fd, int dst_fd) const { // Returns the fd of an opened temporary file, or -1 on failure static int create_temporary_file(const wcstring &name_template, wcstring *out_path) { int out_fd = -1; + char *narrow_str = nullptr; for (size_t attempt = 0; attempt < 10 && out_fd == -1; attempt++) { - char *narrow_str = wcs2str(name_template); + narrow_str = wcs2str(name_template); out_fd = fish_mkstemp_cloexec(narrow_str); - if (out_fd >= 0) { - *out_path = str2wcstring(narrow_str); - } - free(narrow_str); } + + if (out_fd >= 0) { + *out_path = str2wcstring(narrow_str); + } + free(narrow_str); return out_fd; }