Commit cb57c95a authored by Sebastien Alaiwan's avatar Sebastien Alaiwan Committed by sebastien alaiwan
Browse files

Fix potential integer overflow in range checking

Change-Id: I20b951bb67f4c2147055751a8cd9534ceddc7215
parent 07e33f4e
...@@ -15,20 +15,23 @@ ...@@ -15,20 +15,23 @@
void range_check_func(int32_t stage, const int32_t *input, const int32_t *buf, void range_check_func(int32_t stage, const int32_t *input, const int32_t *buf,
int32_t size, int8_t bit) { int32_t size, int8_t bit) {
const int32_t maxValue = (1 << (bit - 1)) - 1; const int64_t maxValue = (1LL << (bit - 1)) - 1;
const int32_t minValue = -(1 << (bit - 1)); const int64_t minValue = -(1LL << (bit - 1));
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
if (buf[i] < minValue || buf[i] > maxValue) { if (buf[i] < minValue || buf[i] > maxValue) {
int buf_bit = get_max_bit(abs(buf[i])) + 1; fprintf(stderr, "Error: coeffs contain out-of-range values\n");
printf("======== %s %d overflow ========\n", __FILE__, __LINE__); fprintf(stderr, "stage: %d\n", stage);
printf("stage: %d node: %d\n", stage, i); fprintf(stderr, "node: %d\n", i);
printf("bit: %d buf_bit: %d buf[i]: %d\n", bit, buf_bit, buf[i]); fprintf(stderr, "allowed range: [%d;%d]\n", minValue, maxValue);
printf("input:\n"); fprintf(stderr, "coeffs: ");
fprintf(stderr, "[");
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
printf("%d,", input[j]); if (j > 0) fprintf(stderr, ", ");
fprintf(stderr, "%d", input[j]);
} }
printf("\n"); fprintf(stderr, "]\n");
assert(0); assert(0);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment