]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Avoid snprintf
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:37:07 +0000 (01:37 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:41:43 +0000 (01:41 -0700)
* 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.

gnulib.modules
lib/wordsplit.c

index 8f69992335cc58845b9dc2a9add18764659939fc..cbcde2fb3c3fd74d3884e96cd5a3ff908f04b41e 100644 (file)
@@ -96,7 +96,6 @@ safe-read
 savedir
 selinux-at
 setenv
-snprintf
 stat-time
 std-gnu23
 stdbool
index 780714970141edfd17558b9601fbc61e459af2b9..289f7c10c2559d30dd333007c88a6076997cf2b8 100644 (file)
@@ -1880,17 +1880,19 @@ wordsplit_pathexpand (struct wordsplit *wsp)
                }
              else if (wsp->ws_options & WRDSO_FAILGLOB)
                {
-                 char buf[128];
                  if (wsp->ws_errno == WRDSE_USERERR)
                    free (wsp->ws_usererr);
-                 snprintf (buf, sizeof (buf), _("no files match pattern %s"),
-                           pattern);
-                 free (pattern);
-                 wsp->ws_usererr = strdup (buf);
-                 if (!wsp->ws_usererr)
-                   return _wsplt_nomem (wsp);
-                 else
-                   return _wsplt_seterr (wsp, WRDSE_USERERR);
+                 char const *msg = _("no files match pattern ");
+                 idx_t msglen = strlen (msg);
+                 char *usererr = irealloc (pattern, msglen + slen + 1);
+                 if (!usererr)
+                   {
+                     free (pattern);
+                     return _wsplt_nomem (wsp);
+                   }
+                 memmove (usererr + msglen, usererr, slen + 1);
+                 wsp->ws_usererr = memcpy (usererr, msg, msglen);
+                 return _wsplt_seterr (wsp, WRDSE_USERERR);
                }
              free (pattern);
              continue;
@@ -2316,14 +2318,14 @@ wordsplit_c_quote_copy (char *dst, const char *src, bool quote_hex)
        *dst++ = *src;
       else
        {
-         char tmp[4];
+         unsigned char uc = *src;
 
          if (quote_hex)
            {
-             unsigned char c = *src;
-             snprintf (tmp, sizeof tmp, "%%%02X", c);
-             memcpy (dst, tmp, 3);
-             dst += 3;
+             static char const hexdigit[16] = "0123456789ABCDEF";
+             *dst++ = '%';
+             for (int i = 4; 0 <= i; i -= 4)
+               *dst++ = hexdigit[(uc >> i) & 0xf];
            }
          else
            {
@@ -2332,12 +2334,8 @@ wordsplit_c_quote_copy (char *dst, const char *src, bool quote_hex)
              if (c)
                *dst++ = c;
              else
-               {
-                 unsigned char ch = *src;
-                 snprintf (tmp, sizeof tmp, "%03o", ch);
-                 memcpy (dst, tmp, 3);
-                 dst += 3;
-               }
+               for (int i = 6; 0 <= i; i -= 3)
+                 *dst++ = '0' + ((uc >> i) & 7);
            }
        }
     }