Skip to content

Commit 441411c

Browse files
committed
BUG: fix UB - strides can be negative, so use signed types for strides math
UBSan report: ``` ../pywt/_extensions/c/wt.template.c:105:43: runtime error: addition of unsigned offset to 0x60d00022bbd0 overflowed to 0x60d00022bba0 pywt/tests/test_multidim.py::test_dwdtn_idwtn_allwavelets PASSED SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../pywt/_extensions/c/wt.template.c:105:43 Fatal ```
1 parent e681add commit 441411c

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

pywt/_extensions/c/wt.template.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ int CAT(TYPE, _downcoef_axis)(const TYPE * const restrict input, const ArrayInfo
7777
}
7878

7979
for (i = 0; i < num_loops; ++i){
80-
size_t j;
81-
size_t input_offset = 0, output_offset = 0;
80+
pywt_index_t j, axis_idx, input_offset = 0, output_offset = 0;
8281
const TYPE * input_row;
8382
TYPE * output_row;
8483

@@ -88,7 +87,7 @@ int CAT(TYPE, _downcoef_axis)(const TYPE * const restrict input, const ArrayInfo
8887
for (j = 0; j < output_info.ndim; ++j){
8988
size_t j_rev = output_info.ndim - 1 - j;
9089
if (j_rev != axis){
91-
size_t axis_idx = reduced_idx % output_info.shape[j_rev];
90+
axis_idx = reduced_idx % output_info.shape[j_rev];
9291
reduced_idx /= output_info.shape[j_rev];
9392

9493
input_offset += (axis_idx * input_info.strides[j_rev]);
@@ -232,8 +231,7 @@ int CAT(TYPE, _idwt_axis)(const TYPE * const restrict coefs_a, const ArrayInfo *
232231
}
233232

234233
for (i = 0; i < num_loops; ++i){
235-
size_t j;
236-
size_t a_offset = 0, d_offset = 0, output_offset = 0;
234+
pywt_index_t j, axis_idx, a_offset = 0, d_offset = 0, output_offset = 0;
237235
TYPE * output_row;
238236

239237
// Calculate offset into linear buffer
@@ -242,7 +240,7 @@ int CAT(TYPE, _idwt_axis)(const TYPE * const restrict coefs_a, const ArrayInfo *
242240
for (j = 0; j < output_info.ndim; ++j){
243241
size_t j_rev = output_info.ndim - 1 - j;
244242
if (j_rev != axis){
245-
size_t axis_idx = reduced_idx % output_info.shape[j_rev];
243+
axis_idx = reduced_idx % output_info.shape[j_rev];
246244
reduced_idx /= output_info.shape[j_rev];
247245

248246
if (have_a)

0 commit comments

Comments
 (0)