Commit 072d957f for tesseract
commit 072d957f5553f92eab6563d1ad44dbedb3cebfb5
Author: Stefan Weil <sw@weilnetz.de>
Date: Mon Sep 28 21:42:42 2026 +0200
Fix heap overflow in intsimdmatrix_test when testing a wider kernel
ExpectEqualResults sized the result buffer using the globally selected
int8 kernel's RoundOutputs, but the kernel actually being tested may round
the output to a larger size. The AVX512-VNNI kernel rounds outputs to a
multiple of 16 while the default (AVX2) rounds to 8, so for num_out values
where round_up(x,16) > round_up(x,8) the VNNI kernel wrote past the
test_result buffer, corrupting the heap (glibc 'corrupted size vs.
prev_size', abort) on the first such size.
Size the buffer for the kernel under test instead of the global one.
Production code is unaffected: it sizes v with the same kernel it calls.
Assisted-by: OpenCode / qwen3.8-27b-thinking (Alibaba Cloud)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/unittest/intsimdmatrix_test.cc b/unittest/intsimdmatrix_test.cc
index b7bc1010..e1f32e7d 100644
--- a/unittest/intsimdmatrix_test.cc
+++ b/unittest/intsimdmatrix_test.cc
@@ -68,10 +68,11 @@ protected:
GENERIC_2D_ARRAY<int8_t> w = InitRandom(num_out, num_in + 1);
std::vector<int8_t> u = RandomVector(num_in, matrix);
std::vector<TFloat> scales = RandomScales(num_out);
- int ro = num_out;
- if (IntSimdMatrix::intSimdMatrix) {
- ro = IntSimdMatrix::intSimdMatrix->RoundOutputs(ro);
- }
+ // The kernel under test may round the output to a different size than
+ // the globally selected one, so size the result buffer for it.
+ int ro = matrix.matrixDotVectorFunction != nullptr
+ ? matrix.RoundOutputs(num_out)
+ : num_out;
std::vector<TFloat> base_result(num_out);
IntSimdMatrix::MatrixDotVector(w, scales, u.data(), base_result.data());
std::vector<TFloat> test_result(ro);