Skip to content

Commit 49873ec

Browse files
committed
Improve/clarify error handling code and remove a potential integer overflow
1 parent cc5653d commit 49873ec

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

src/transform/BWT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bool BWT::forward(SliceArray<byte>& input, SliceArray<byte>& output, int count)
118118
_sa = new int[_saSize];
119119
}
120120

121-
if (_saAlgo.computeBWT(src, dst, _sa, count, _primaryIndexes, getBWTChunks(count)) <= 0)
121+
if (_saAlgo.computeBWT(src, dst, _sa, count, _primaryIndexes, getBWTChunks(count)) == false)
122122
return false;
123123

124124
input._index += count;

src/transform/BWTS.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ bool BWTS::forward(SliceArray<byte>& input, SliceArray<byte>& output, int count)
7171
int* sa = _buffer1;
7272
int* isa = _buffer2;
7373

74-
_saAlgo.computeSuffixArray(src, sa, count);
74+
if (_saAlgo.computeSuffixArray(src, sa, count) == false)
75+
return false;
7576

7677
for (int i = 0; i < count; i++)
7778
isa[sa[i]] = i;

src/transform/DivSufSort.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,18 @@ void DivSufSort::reset()
9090
memset(&_bucketB[0], 0, sizeof(int) * 65536);
9191
}
9292

93-
void DivSufSort::computeSuffixArray(const byte input[], int sa[], int length)
93+
bool DivSufSort::computeSuffixArray(const byte input[], int sa[], int length)
9494
{
9595
_buffer = reinterpret_cast<const uint8*>(&input[0]);
9696
_sa = sa;
9797
reset();
9898
const int m = sortTypeBstar(_bucketA, _bucketB, length);
99+
100+
if (m < 0)
101+
return false;
102+
99103
constructSuffixArray(_bucketA, _bucketB, length, m);
104+
return true;
100105
}
101106

102107
void DivSufSort::constructSuffixArray(int bucketA[], int bucketB[], int n, int m)
@@ -163,16 +168,20 @@ void DivSufSort::constructSuffixArray(int bucketA[], int bucketB[], int n, int m
163168
}
164169
}
165170

166-
int DivSufSort::computeBWT(const byte input[], byte output[], int bwt[], int length, int indexes[], int idxCount)
171+
bool DivSufSort::computeBWT(const byte input[], byte output[], int bwt[], int length, int indexes[], int idxCount)
167172
{
168173
_buffer = reinterpret_cast<const uint8*>(&input[0]);
169174
_sa = bwt;
170175
reset();
171176
const int m = sortTypeBstar(_bucketA, _bucketB, length);
177+
178+
if (m < 0)
179+
return false;
180+
172181
const int pIdx = constructBWT(_bucketA, _bucketB, length, m, indexes, idxCount);
173182

174183
if (pIdx < 0)
175-
return -1;
184+
return false;
176185

177186
output[0] = input[length - 1];
178187

@@ -182,7 +191,7 @@ int DivSufSort::computeBWT(const byte input[], byte output[], int bwt[], int len
182191
for (int i = pIdx + 1; i < length; i++)
183192
output[i] = byte(bwt[i]);
184193

185-
return pIdx + 1;
194+
return true;
186195
}
187196

188197
int DivSufSort::constructBWT(int bucketA[], int bucketB[], int n, int m, int indexes[], int idxCount)

src/transform/DivSufSort.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ namespace kanzi
218218

219219
~DivSufSort();
220220

221-
void computeSuffixArray(const byte input[], int sa[], int length);
221+
bool computeSuffixArray(const byte input[], int sa[], int length);
222222

223-
int computeBWT(const byte input[], byte output[], int sa[], int length, int indexes[], int idxCount = 8);
223+
bool computeBWT(const byte input[], byte output[], int sa[], int length, int indexes[], int idxCount = 8);
224224
};
225225

226226

0 commit comments

Comments
 (0)