Skip to content

Commit 5cd6787

Browse files
committed
Fix #847: missing range checks in statespace_*.h files
CodeQL scans flagged lines like 353 in `lib/statespace_avx512.h`, reporting that the use of offset `m` should follow the range check: ```C++ while (rs[m] < csum && m < num_samples) { ``` In more detail: > The program contains an and-expression where the array access is defined before the range check. Consequently the array is accessed without any bounds checking. The range check does not protect the program from segmentation faults caused by attempts to read beyond the end of a buffer. The same error exists in the following files: * statespace_basic.h * statespace_sse.h * statespace_avx512.h * statespace_avx.h
1 parent 7932f78 commit 5cd6787

4 files changed

Lines changed: 4 additions & 4 deletions

File tree

lib/statespace_avx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class StateSpaceAVX :
399399
double re = p[16 * k + j];
400400
double im = p[16 * k + 8 + j];
401401
csum += re * re + im * im;
402-
while (rs[m] < csum && m < num_samples) {
402+
while (m < num_samples && rs[m] < csum) {
403403
bitstrings.emplace_back(8 * k + j);
404404
++m;
405405
}

lib/statespace_avx512.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class StateSpaceAVX512 :
350350
double re = p[32 * k + j];
351351
double im = p[32 * k + 16 + j];
352352
csum += re * re + im * im;
353-
while (rs[m] < csum && m < num_samples) {
353+
while (m < num_samples && rs[m] < csum) {
354354
bitstrings.emplace_back(16 * k + j);
355355
++m;
356356
}

lib/statespace_basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class StateSpaceBasic :
218218
double re = p[2 * k];
219219
double im = p[2 * k + 1];
220220
csum += re * re + im * im;
221-
while (rs[m] < csum && m < num_samples) {
221+
while (m < num_samples && rs[m] < csum) {
222222
bitstrings.emplace_back(k);
223223
++m;
224224
}

lib/statespace_sse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class StateSpaceSSE :
359359
double re = p[8 * k + j];
360360
double im = p[8 * k + 4 + j];
361361
csum += re * re + im * im;
362-
while (rs[m] < csum && m < num_samples) {
362+
while (m < num_samples && rs[m] < csum) {
363363
bitstrings.emplace_back(4 * k + j);
364364
++m;
365365
}

0 commit comments

Comments
 (0)