]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
basenc: allocate buffers on heap
authorAssaf Gordon <assafgordon@gmail.com>
Sat, 5 Jan 2019 23:22:41 +0000 (16:22 -0700)
committerAssaf Gordon <assafgordon@gmail.com>
Sun, 6 Jan 2019 00:14:07 +0000 (17:14 -0700)
Allocate the encoding/decoding buffers dynamically on the heap instead
of using variable-length-array (VLA) on the stack.
Discussed in https://lists.gnu.org/r/coreutils/2019-01/msg00004.html .

* src/basenc.c (do_encode,do_decode): Allocate inbuf/outbuf using
xmalloc, and free if using LINT.

src/basenc.c

index c25dc49f929e61aad0b481aa07e51cbf6ba6fa2a..5ec7bf4a59c67aaf01c07a74ddc1b632a2366f95 100644 (file)
@@ -976,10 +976,12 @@ static void
 do_encode (FILE *in, FILE *out, uintmax_t wrap_column)
 {
   size_t current_column = 0;
-  char inbuf[ENC_BLOCKSIZE];
-  char outbuf[BASE_LENGTH (ENC_BLOCKSIZE)];
+  char *inbuf, *outbuf;
   size_t sum;
 
+  inbuf = xmalloc (ENC_BLOCKSIZE);
+  outbuf = xmalloc (BASE_LENGTH (ENC_BLOCKSIZE));
+
   do
     {
       size_t n;
@@ -1010,16 +1012,21 @@ do_encode (FILE *in, FILE *out, uintmax_t wrap_column)
 
   if (ferror (in))
     die (EXIT_FAILURE, errno, _("read error"));
+
+  IF_LINT (free (inbuf));
+  IF_LINT (free (outbuf));
 }
 
 static void
 do_decode (FILE *in, FILE *out, bool ignore_garbage)
 {
-  char inbuf[BASE_LENGTH (DEC_BLOCKSIZE)];
-  char outbuf[DEC_BLOCKSIZE];
+  char *inbuf, *outbuf;
   size_t sum;
   struct base_decode_context ctx;
 
+  inbuf = xmalloc (BASE_LENGTH (DEC_BLOCKSIZE));
+  outbuf = xmalloc (DEC_BLOCKSIZE);
+
 #if BASE_TYPE == 42
   ctx.inbuf = NULL;
 #endif
@@ -1077,6 +1084,8 @@ do_decode (FILE *in, FILE *out, bool ignore_garbage)
 #if BASE_TYPE == 42
   IF_LINT (free (ctx.inbuf));
 #endif
+  IF_LINT (free (inbuf));
+  IF_LINT (free (outbuf));
 }
 
 int