]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Fix unlikely overflow in utf8_convert
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 30 Jul 2024 15:33:39 +0000 (08:33 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:41:43 +0000 (01:41 -0700)
* src/utf8.c (utf8_convert): Check for integer overflow.

src/utf8.c

index 568143203642d406318ba1649761f03cd9975d85..6dde8814290c77001966a49d59e206efd2410047 100644 (file)
@@ -81,7 +81,10 @@ utf8_convert (bool to_utf, char const *input, char **output)
     return false;
 
   inlen = strlen (input) + 1;
-  outlen = inlen * MB_LEN_MAX + 1;
+  bool overflow = ckd_mul (&outlen, inlen, MB_LEN_MAX);
+  overflow |= ckd_add (&outlen, outlen, 1);
+  if (overflow)
+    xalloc_die ();
   ob = ret = xmalloc (outlen);
   ib = (char ICONV_CONST *) input;
   /* According to POSIX, "if iconv() encounters a character in the input
@@ -90,7 +93,7 @@ utf8_convert (bool to_utf, char const *input, char **output)
      implementation-defined conversion on this character." It will "update
      the variables pointed to by the arguments to reflect the extent of the
      conversion and return the number of non-identical conversions performed".
-     On error, it returns -1. 
+     On error, it returns -1.
      In other words, non-zero return always indicates failure, either because
      the input was not fully converted, or because it was converted in a
      non-reversible way.