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.
This commit is contained in:
Paul Eggert 2024-08-05 00:44:28 -07:00 committed by Paul Smith
parent 9fee98f843
commit 8c8c7fc226
5 changed files with 29 additions and 41 deletions

View 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 != '.';

View 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

View file

@ -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
}

View 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;

View 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);