]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(long_time_expected_width): Use x2nrealloc, not alloca,
authorJim Meyering <jim@meyering.net>
Thu, 10 Mar 2005 12:07:31 +0000 (12:07 +0000)
committerJim Meyering <jim@meyering.net>
Thu, 10 Mar 2005 12:07:31 +0000 (12:07 +0000)
so format string abuse cannot provoke stack overflow.
(print_long_format): Likewise.

src/ls.c

index f9cf259bd9e0e9a58f0e525e0c83be4ef9e55a23..8aa1d46ac6c60982819bff24d615ffa662efd5bb 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -3059,12 +3059,20 @@ long_time_expected_width (void)
          len = nstrftime (buf, bufsize, fmt, tm, 0, 0);
          if (len || ! *buf)
            break;
-         buf = alloca (bufsize *= 2);
+         if (buf == initbuf)
+           {
+             buf = NULL;
+             bufsize *= 2;
+           }
+         buf = x2nrealloc (buf, &bufsize, sizeof *buf);
        }
 
       width = mbsnwidth (buf, len, 0);
       if (width < 0)
        width = 0;
+
+      if (buf != initbuf)
+       free (buf);
     }
 
   return width;
@@ -3349,8 +3357,16 @@ print_long_format (const struct fileinfo *f)
                         when_local, 0, when_ns);
          if (s || ! *p)
            break;
-         newbuf = alloca (bufsize *= 2);
-         memcpy (newbuf, buf, p - buf);
+         if (buf == init_bigbuf)
+           {
+             bufsize *= 2;
+             newbuf = xmalloc (bufsize);
+             memcpy (newbuf, buf, p - buf);
+           }
+         else
+           {
+             newbuf = x2nrealloc (buf, &bufsize, sizeof *buf);
+           }
          p = newbuf + (p - buf);
          buf = newbuf;
        }
@@ -3374,6 +3390,8 @@ print_long_format (const struct fileinfo *f)
     }
 
   DIRED_FPUTS (buf, stdout, p - buf);
+  if (buf != init_bigbuf)
+    free (buf);
   print_name_with_quoting (f->name, FILE_OR_LINK_MODE (f), f->linkok,
                           &dired_obstack);