Skip to content

Commit

Permalink
perms.cc: Replace variable length arrays with new and delete[]
Browse files Browse the repository at this point in the history
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[].
  • Loading branch information
arungiridhar committed Nov 3, 2023
1 parent 7374513 commit 6de74d6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libinterp/corefcn/perms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ GetPerms (const Array<T> &ar_in, bool uniq_v, bool do_sort = false)
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;

Expand Down Expand Up @@ -114,6 +114,8 @@ GetPerms (const Array<T> &ar_in, bool uniq_v, bool do_sort = false)
}
while (std::next_permutation (myvidx, myvidx + m, std::greater<int> ()));

delete[] myvidx;

return res;
}

Expand All @@ -129,7 +131,7 @@ GetPermsNoSort (const Array<T> &ar_in, bool uniq_v = false)
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;

Expand Down Expand Up @@ -176,6 +178,9 @@ GetPermsNoSort (const Array<T> &ar_in, bool uniq_v = false)
i++;
}
while (std::next_permutation (myvidx, myvidx + m, std::greater<int> ()));

delete[] myvidx;

return res;
}

Expand Down

0 comments on commit 6de74d6

Please sign in to comment.