Skip to content

Commit 4d7dafd

Browse files
authored
Fix UB in f2reduce for cols == 0 (#5401)
The shift exponent in `uint64_t final_b = (1ull << (cols - 1)) - 1;` underflows for `cols == 0`. We can skip converting to rref for `cols <= 1` as well to avoid the issue.
1 parent 8c97e9f commit 4d7dafd

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

lib/Tools/LinearLayout.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ std::unique_ptr<uint64_t[]> getMatrix(const LinearLayout &layout) {
117117
// outDim as columns. In other words, finds the number of linearly-independent
118118
// bases for this output dimension.
119119
int getMatrixRank(std::unique_ptr<uint64_t[]> m, int numRows, int numCols) {
120-
// f2reduce underflows if the number of cols is 0, return the rank early in
121-
// this case.
122-
if (numCols == 0) {
123-
return 0;
124-
}
125120
// stride is specified in number of 64-bit words per row, and we pack our
126121
// matrix so that there's only one uint64_t per row.
127122
assert(numCols <= 64);

third_party/f2reduce/f2reduce.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ namespace f2reduce {
470470

471471
void inplace_rref_strided(uint64_t *matrix, uint64_t rows, uint64_t cols, uint64_t stride) {
472472

473-
if (rows <= 1) {
474-
// If the matrix has 0 or 1 rows, it must already be in RREF:
473+
if (rows <= 1 || cols == 0) {
474+
// If the matrix has 0 or 1 rows or 0 columns, it must already be in RREF:
475475
return;
476476
}
477477

0 commit comments

Comments
 (0)