From 8c8c7fc2269df027d1591a6e4a4873f773a2d53f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 5 Aug 2024 00:44:28 -0700 Subject: [PATCH] Avoid strlen calls after sprintf * 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 | 14 ++++++-------- src/function.c | 9 +++------ src/job.c | 3 +-- src/main.c | 9 +++++---- src/output.c | 35 ++++++++++++++--------------------- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/file.c b/src/file.c index 77c328ca..3b1a8d31 100644 --- a/src/file.c +++ b/src/file.c @@ -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 != '.'; diff --git a/src/function.c b/src/function.c index b88ea184..bf4f2abb 100644 --- a/src/function.c +++ b/src/function.c @@ -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 diff --git a/src/job.c b/src/job.c index 27fc7640..e54a9340 100644 --- 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 } diff --git a/src/main.c b/src/main.c index 0367f08b..e8d19382 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/output.c b/src/output.c index 775d1410..b7735ec4 100644 --- a/src/output.c +++ b/src/output.c @@ -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); -- 2.47.3