This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] libiberty: Fix and cleanup choose_temp_base()


Hi,

reduced code size by using xasprintf().

Please review the check of mktemp(), which was formerly checking against
0 (regarding glibc man pages that was wrong). I left that check intact
(just in case I miss something) and added the check for an empty string,
as documented in the glibc man pages.

Does it make sense to further replace strlen/malloc/strcpy/strcat
sequences by [x]asprintf in order to reduce source lines and library
(binary) size ? (In the means of "is it appreciated")

A side effect is calming down static analyzers that dislike unbounded
memory accesses and thus warn about strcpy() and strcat().

Regards, Tim
From 72b324037c957e53a15cdbeefc1ff49128f0e965 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20R=C3=BChsen?= <tim.ruehsen@gmx.de>
Date: Thu, 7 Nov 2019 17:53:56 +0100
Subject: [PATCH] libiberty: Fix and cleanup choose_temp_base()

*choose-temp.c (choose_temp_base): Simplify code,
fix checking return value of mktemp().
---
 libiberty/ChangeLog     |  5 +++++
 libiberty/choose-temp.c | 17 ++++++-----------
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
index 95cb1525f2..7a7809ba47 100644
--- a/libiberty/ChangeLog
+++ b/libiberty/ChangeLog
@@ -1,3 +1,8 @@
+2019-11-07  Tim Ruehsen  <tim.ruehsen@gmx.de>
+
+        *choose-temp.c (choose_temp_base): Simplify code,
+        fix checking return value of mktemp().
+
 2019-08-08  Martin Liska  <mliska@suse.cz>
 
 	PR bootstrap/91352
diff --git a/libiberty/choose-temp.c b/libiberty/choose-temp.c
index 72c1b710bd..17bb0f27d4 100644
--- a/libiberty/choose-temp.c
+++ b/libiberty/choose-temp.c
@@ -38,7 +38,6 @@ Boston, MA 02110-1301, USA.  */
 /* Name of temporary file.
    mktemp requires 6 trailing X's.  */
 #define TEMP_FILE "ccXXXXXX"
-#define TEMP_FILE_LEN (sizeof(TEMP_FILE) - 1)
 
 /*
 
@@ -47,7 +46,7 @@ Boston, MA 02110-1301, USA.  */
 Return a prefix for temporary file names or @code{NULL} if unable to
 find one.  The current directory is chosen if all else fails so the
 program is exited if a temporary directory can't be found (@code{mktemp}
-fails).  The buffer for the result is obtained with @code{xmalloc}.
+fails).  The buffer for the result is obtained with @code{xasprintf}.
 
 This function is provided for backwards compatibility only.  Its use is
 not recommended.
@@ -59,16 +58,12 @@ not recommended.
 char *
 choose_temp_base (void)
 {
-  const char *base = choose_tmpdir ();
-  char *temp_filename;
-  int len;
+  char *temp_filename = xasprintf("%s%s", choose_tmpdir (), TEMP_FILE);
 
-  len = strlen (base);
-  temp_filename = XNEWVEC (char, len + TEMP_FILE_LEN + 1);
-  strcpy (temp_filename, base);
-  strcpy (temp_filename + len, TEMP_FILE);
-
-  if (mktemp (temp_filename) == 0)
+  /* mktemp() man page: If a unique name could not be created,
+   * template is made an empty string. */
+  if (mktemp (temp_filename) == NULL || *temp_filename == 0)
     abort ();
+
   return temp_filename;
 }
-- 
2.24.0

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]