resrc: sprintf sanitizer null destination pointer

Message ID ZMuTJ/KyWwzFX8MZ@squeak.grove.modra.org
State Accepted
Headers
Series resrc: sprintf sanitizer null destination pointer |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Alan Modra Aug. 3, 2023, 11:44 a.m. UTC
  * resrc.c (read_rc_file): Use stpcpy rather than sprintf
	followed by strlen.  Tidy.
  

Patch

diff --git a/binutils/resrc.c b/binutils/resrc.c
index 3ea9813d8bd..0d7a6e1cdbc 100644
--- a/binutils/resrc.c
+++ b/binutils/resrc.c
@@ -441,29 +441,23 @@  read_rc_file (const char *filename, const char *preprocessor,
     {
       char *edit, *dir;
 
-      if (filename[0] == '/'
-	  || filename[0] == '\\'
-	  || filename[1] == ':')
-        /* Absolute path.  */
-	edit = dir = xstrdup (filename);
-      else
+      edit = dir = xmalloc (strlen (filename) + 3);
+      if (filename[0] != '/'
+	  && filename[0] != '\\'
+	  && filename[1] != ':')
 	{
 	  /* Relative path.  */
-	  edit = dir = xmalloc (strlen (filename) + 3);
-	  sprintf (dir, "./%s", filename);
+	  *edit++ = '.';
+	  *edit++ = '/';
 	}
+      edit = stpcpy (edit, filename);
 
       /* Walk dir backwards stopping at the first directory separator.  */
-      edit += strlen (dir);
       while (edit > dir && (edit[-1] != '\\' && edit[-1] != '/'))
-	{
-	  --edit;
-	  edit[0] = 0;
-	}
+	--edit;
 
       /* Cut off trailing slash.  */
-      --edit;
-      edit[0] = 0;
+      *--edit = 0;
 
       /* Convert all back slashes to forward slashes.  */
       while ((edit = strchr (dir, '\\')) != NULL)