]> git.ipfire.org Git - thirdparty/make.git/commitdiff
Avoid strlen calls after sprintf
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 5 Aug 2024 07:44:28 +0000 (00:44 -0700)
committerPaul Smith <psmith@gnu.org>
Mon, 2 Sep 2024 18:43:24 +0000 (14:43 -0400)
* src/file.c (file_timestamp_sprintf):
* src/function.c (func_words, func_call):
* src/job.c (child_error):
* src/main.c (define_makeflags):
* src/output.c (message, error, fatal):
Use return value from sprintf instead of calling strlen
on the resulting buffer.

src/file.c
src/function.c
src/job.c
src/main.c
src/output.c

index 77c328caa864502aff40680ad2411fafc88565ae..3b1a8d31008af611fea8cbf817caaf86fb3cfef6 100644 (file)
@@ -1027,22 +1027,20 @@ file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts)
   if (tm)
     {
       intmax_t year = tm->tm_year;
-      sprintf (p, "%04" PRIdMAX "-%02d-%02d %02d:%02d:%02d",
-               year + 1900, tm->tm_mon + 1, tm->tm_mday,
-               tm->tm_hour, tm->tm_min, tm->tm_sec);
+      p += sprintf (p, "%04" PRIdMAX "-%02d-%02d %02d:%02d:%02d",
+                    year + 1900, tm->tm_mon + 1, tm->tm_mday,
+                    tm->tm_hour, tm->tm_min, tm->tm_sec);
     }
   else if (t < 0)
-    sprintf (p, "%" PRIdMAX, (intmax_t) t);
+    p += sprintf (p, "%" PRIdMAX, (intmax_t) t);
   else
-    sprintf (p, "%" PRIuMAX, (uintmax_t) t);
-  p += strlen (p);
+    p += sprintf (p, "%" PRIuMAX, (uintmax_t) t);
 
   /* Append nanoseconds as a fraction, but remove trailing zeros.  We don't
      know the actual timestamp resolution, since clock_getres applies only to
      local times, whereas this timestamp might come from a remote filesystem.
      So removing trailing zeros is the best guess that we can do.  */
-  sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts));
-  p += strlen (p) - 1;
+  p += sprintf (p, ".%09d", FILE_TIMESTAMP_NS (ts)) - 1;
   while (*p == '0')
     p--;
   p += *p != '.';
index b88ea184477dcea5f722dcd72b170aaa4c5e7411..bf4f2abbba8a1b88efb1b8d90b045b6e484e987c 100644 (file)
@@ -737,8 +737,7 @@ func_words (char *o, char **argv, const char *funcname UNUSED)
   while (find_next_token (&word_iterator, NULL) != 0)
     ++i;
 
-  sprintf (buf, "%u", i);
-  o = variable_buffer_output (o, buf, strlen (buf));
+  o = variable_buffer_output (o, buf, sprintf (buf, "%u", i));
 
   return o;
 }
@@ -2659,8 +2658,7 @@ func_call (char *o, char **argv, const char *funcname UNUSED)
     {
       char num[INTSTR_LENGTH];
 
-      sprintf (num, "%u", i);
-      define_variable (num, strlen (num), *argv, o_automatic, 0);
+      define_variable (num, sprintf (num, "%u", i), *argv, o_automatic, 0);
     }
 
   /* If the number of arguments we have is < max_args, it means we're inside
@@ -2672,8 +2670,7 @@ func_call (char *o, char **argv, const char *funcname UNUSED)
     {
       char num[INTSTR_LENGTH];
 
-      sprintf (num, "%u", i);
-      define_variable (num, strlen (num), "", o_automatic, 0);
+      define_variable (num, sprintf (num, "%u", i), "", o_automatic, 0);
     }
 
   /* Expand the function in the context of the arguments, adding the result to
index 27fc764026eab2285cd439ae555130e5345175bc..e54a9340ca85ecde16b8942e62cc3cfc835d9dc0 100644 (file)
--- a/src/job.c
+++ b/src/job.c
@@ -557,9 +557,8 @@ child_error (struct child *child,
     {
 #define SHUFFLE_PREFIX " shuffle="
       char *a = alloca (CSTRLEN(SHUFFLE_PREFIX) + strlen (smode) + 1);
-      sprintf (a, SHUFFLE_PREFIX "%s", smode);
+      l += sprintf (a, SHUFFLE_PREFIX "%s", smode);
       smode = a;
-      l += strlen (smode);
 #undef SHUFFLE_PREFIX
     }
 
index 0367f08b8de9a99c1cb0f52d62c0e95485f715a7..e8d19382c7edfa2401d73cff403d31ca6aa7f219 100644 (file)
@@ -3587,10 +3587,11 @@ define_makeflags (int makefile)
             {
               /* Add the value if not omitted.  */
               char *buf = alloca (30);
-              sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
+              int buflen = sprintf (buf, "%u",
+                                    *(unsigned int *) cs->value_ptr);
               if (!short_option (cs->c))
                 fp = variable_buffer_output (fp, "=", 1);
-              fp = variable_buffer_output (fp, buf, strlen (buf));
+              fp = variable_buffer_output (fp, buf, buflen);
             }
           break;
 
@@ -3603,10 +3604,10 @@ define_makeflags (int makefile)
               || (*(double *) cs->value_ptr != *(double *) cs->noarg_value))
             {
               char *buf = alloca (100);
-              sprintf (buf, "%g", *(double *) cs->value_ptr);
+              int buflen = sprintf (buf, "%g", *(double *) cs->value_ptr);
               if (!short_option (cs->c))
                 fp = variable_buffer_output (fp, "=", 1);
-              fp = variable_buffer_output (fp, buf, strlen (buf));
+              fp = variable_buffer_output (fp, buf, buflen);
             }
           break;
 
index 775d1410d9533761510b42b021faa02c7994e413..b7735ec4460cd1267a5f32558c8ee04658217761 100644 (file)
@@ -425,13 +425,9 @@ message (int prefix, size_t len, const char *fmt, ...)
   start = p = get_buffer (len);
 
   if (prefix)
-    {
-      if (makelevel == 0)
-        sprintf (p, "%s: ", program);
-      else
-        sprintf (p, "%s[%u]: ", program, makelevel);
-      p += strlen (p);
-    }
+    p += (makelevel == 0
+          ? sprintf (p, "%s: ", program)
+          : sprintf (p, "%s[%u]: ", program, makelevel));
 
   va_start (args, fmt);
   vsprintf (p, fmt, args);
@@ -457,13 +453,11 @@ error (const floc *flocp, size_t len, const char *fmt, ...)
           + INTSTR_LENGTH + 4 + 1 + 1);
   start = p = get_buffer (len);
 
-  if (flocp && flocp->filenm)
-    sprintf (p, "%s:%lu: ", flocp->filenm, flocp->lineno + flocp->offset);
-  else if (makelevel == 0)
-    sprintf (p, "%s: ", program);
-  else
-    sprintf (p, "%s[%u]: ", program, makelevel);
-  p += strlen (p);
+  p += (flocp && flocp->filenm
+        ? sprintf (p, "%s:%lu: ", flocp->filenm, flocp->lineno + flocp->offset)
+        : makelevel == 0
+        ? sprintf (p, "%s: ", program)
+        : sprintf (p, "%s[%u]: ", program, makelevel));
 
   va_start (args, fmt);
   vsprintf (p, fmt, args);
@@ -490,13 +484,12 @@ fatal (const floc *flocp, size_t len, const char *fmt, ...)
           + INTSTR_LENGTH + 8 + strlen (stop) + 1);
   start = p = get_buffer (len);
 
-  if (flocp && flocp->filenm)
-    sprintf (p, "%s:%lu: *** ", flocp->filenm, flocp->lineno + flocp->offset);
-  else if (makelevel == 0)
-    sprintf (p, "%s: *** ", program);
-  else
-    sprintf (p, "%s[%u]: *** ", program, makelevel);
-  p += strlen (p);
+  p += (flocp && flocp->filenm
+        ? sprintf (p, "%s:%lu: *** ", flocp->filenm,
+                   flocp->lineno + flocp->offset)
+        : makelevel == 0
+        ? sprintf (p, "%s: *** ", program)
+        : sprintf (p, "%s[%u]: *** ", program, makelevel));
 
   va_start (args, fmt);
   vsprintf (p, fmt, args);