Avoid snprintf

* gnulib.modules: Remove snprintf.
* lib/wordsplit.c (wordsplit_pathexpand):
Do not arbitrarily truncate diagnostic.
(wordsplit_c_quote_copy): Rewrite to avoid the need to
invoke snprintf on a temporary buffer.
This commit is contained in:
Paul Eggert
2024-08-04 01:37:07 -07:00
parent 5316938142
commit a80f364662
2 changed files with 18 additions and 21 deletions

View File

@@ -96,7 +96,6 @@ safe-read
savedir savedir
selinux-at selinux-at
setenv setenv
snprintf
stat-time stat-time
std-gnu23 std-gnu23
stdbool stdbool

View File

@@ -1880,16 +1880,18 @@ wordsplit_pathexpand (struct wordsplit *wsp)
} }
else if (wsp->ws_options & WRDSO_FAILGLOB) else if (wsp->ws_options & WRDSO_FAILGLOB)
{ {
char buf[128];
if (wsp->ws_errno == WRDSE_USERERR) if (wsp->ws_errno == WRDSE_USERERR)
free (wsp->ws_usererr); free (wsp->ws_usererr);
snprintf (buf, sizeof (buf), _("no files match pattern %s"), char const *msg = _("no files match pattern ");
pattern); idx_t msglen = strlen (msg);
char *usererr = irealloc (pattern, msglen + slen + 1);
if (!usererr)
{
free (pattern); free (pattern);
wsp->ws_usererr = strdup (buf);
if (!wsp->ws_usererr)
return _wsplt_nomem (wsp); return _wsplt_nomem (wsp);
else }
memmove (usererr + msglen, usererr, slen + 1);
wsp->ws_usererr = memcpy (usererr, msg, msglen);
return _wsplt_seterr (wsp, WRDSE_USERERR); return _wsplt_seterr (wsp, WRDSE_USERERR);
} }
free (pattern); free (pattern);
@@ -2316,14 +2318,14 @@ wordsplit_c_quote_copy (char *dst, const char *src, bool quote_hex)
*dst++ = *src; *dst++ = *src;
else else
{ {
char tmp[4]; unsigned char uc = *src;
if (quote_hex) if (quote_hex)
{ {
unsigned char c = *src; static char const hexdigit[16] = "0123456789ABCDEF";
snprintf (tmp, sizeof tmp, "%%%02X", c); *dst++ = '%';
memcpy (dst, tmp, 3); for (int i = 4; 0 <= i; i -= 4)
dst += 3; *dst++ = hexdigit[(uc >> i) & 0xf];
} }
else else
{ {
@@ -2332,12 +2334,8 @@ wordsplit_c_quote_copy (char *dst, const char *src, bool quote_hex)
if (c) if (c)
*dst++ = c; *dst++ = c;
else else
{ for (int i = 6; 0 <= i; i -= 3)
unsigned char ch = *src; *dst++ = '0' + ((uc >> i) & 7);
snprintf (tmp, sizeof tmp, "%03o", ch);
memcpy (dst, tmp, 3);
dst += 3;
}
} }
} }
} }