changeset 32458:e516b6f4d1e0

perms.cc: Replace variable length arrays with new and delete[] There were two uses of variable length arrays in perms.cc. These did not cause problems in normal use but did show up as potentially undefined behavior with `-fsanitize=undefined` because the array length was not constrained to be positive. This changeset replaces them with explicit dynamic memory allocation using new/delete[].
author Arun Giridhar <arungiridhar@gmail.com>
date Fri, 03 Nov 2023 14:20:00 -0400
parents 9c6955ab01f0
children 29237401fe22
files libinterp/corefcn/perms.cc
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/perms.cc	Wed Nov 01 17:53:04 2023 +0100
+++ b/libinterp/corefcn/perms.cc	Fri Nov 03 14:20:00 2023 -0400
@@ -56,7 +56,7 @@
   double nr = Factorial (m);
 
   // Setup index vector filled from 0..m-1
-  int myvidx[m];
+  int* myvidx = new int[m];
   for (int i = 0; i < m; i++)
     myvidx[i] = i;
 
@@ -114,6 +114,8 @@
     }
   while (std::next_permutation (myvidx, myvidx + m, std::greater<int> ()));
 
+  delete[] myvidx;
+
   return res;
 }
 
@@ -129,7 +131,7 @@
   double nr = Factorial (m);
 
   // Setup index vector filled from 0..m-1
-  int myvidx[m];
+  int* myvidx = new int[m];
   for (int i = 0; i < m; i++)
     myvidx[i] = i;
 
@@ -176,6 +178,9 @@
       i++;
     }
   while (std::next_permutation (myvidx, myvidx + m, std::greater<int> ()));
+
+  delete[] myvidx;
+
   return res;
 }