From 2967d2fb713c1f0df2d1c237fa6709f1a6c34e2c Mon Sep 17 00:00:00 2001 From: evgeny Date: Sat, 10 Oct 2026 19:38:08 +0200 Subject: [PATCH] Fix matrix inverse syntax and make bounded conversions explicit --- lib/reed_solomon.c | 8 ++++---- tests/test_reed_solomon.c | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/reed_solomon.c b/lib/reed_solomon.c index aa984b7d..a7fee4c1 100644 --- a/lib/reed_solomon.c +++ b/lib/reed_solomon.c @@ -47,7 +47,7 @@ static int rs_invert(struct rs_codec *ctx, const uint8_t *const rows[]) { other[j] = tmp; } } - uint8_t inverse = ctx->gf_exp[255 - ctx->gf_log[row[col]]; + uint8_t inverse = ctx->gf_exp[255 - ctx->gf_log[row[col]]]; for (unsigned j = col; j < stride; j++) row[j] = rs_multiply(ctx, row[j], inverse); for (unsigned i = 0; i < k; i++) { if (i == col) continue; @@ -101,7 +101,7 @@ int rs_init(struct rs_codec **out, unsigned n, unsigned r, size_t block_size) { } unsigned k = n - r; size_t bytes = sizeof(struct rs_codec) + n * k + 2 * k * k; - struct rs_codec *ctx = u_calloc(1, bytes); + struct rs_codec *ctx = u_calloc(1, (uint32_t)bytes); if (!ctx) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "reed_solomon: allocation failed n=%u r=%u block_size=%zu bytes=%zu", n, r, block_size, bytes); return RS_ERR_MEMORY; @@ -114,8 +114,8 @@ int rs_init(struct rs_codec **out, unsigned n, unsigned r, size_t block_size) { ctx->work = ctx->storage + n * k; unsigned value = 1; for (unsigned i = 0; i < 255; i++) { - ctx->gf_exp[i] = value; - ctx->gf_log[value] = i; + ctx->gf_exp[i] = (uint8_t)value; + ctx->gf_log[value] = (uint8_t)i; value <<= 1; if (value & 256) value ^= 0x11d; } diff --git a/tests/test_reed_solomon.c b/tests/test_reed_solomon.c index 7705ba66..8e4e9dd9 100644 --- a/tests/test_reed_solomon.c +++ b/tests/test_reed_solomon.c @@ -71,10 +71,10 @@ static void fixture_init(struct fixture *f, unsigned n, unsigned r, size_t block f->input_size = rs_input_size(f->ctx); CHECK(f->input_size == f->k * (block_size - RS_HEADER_SIZE)); f->storage_size = n * (block_size + 2 * GUARD); - f->input_storage = u_malloc(f->input_size + 2 * GUARD); - f->output_storage = u_malloc(f->input_size + 2 * GUARD); - f->storage = u_malloc(f->storage_size); - f->saved = u_malloc(f->storage_size); + f->input_storage = u_malloc((uint32_t)(f->input_size + 2 * GUARD)); + f->output_storage = u_malloc((uint32_t)(f->input_size + 2 * GUARD)); + f->storage = u_malloc((uint32_t)f->storage_size); + f->saved = u_malloc((uint32_t)f->storage_size); CHECK(f->input_storage && f->output_storage && f->storage && f->saved); memset(f->input_storage, MARKER, f->input_size + 2 * GUARD); memset(f->output_storage, MARKER, f->input_size + 2 * GUARD); @@ -293,8 +293,8 @@ static void test_stream(void) { struct fixture f; fixture_init(&f, 16, 2, 101, 0); size_t length = 3 * f.input_size + 17; - uint8_t *source = u_malloc(length); - uint8_t *result = u_malloc(length); + uint8_t *source = u_malloc((uint32_t)length); + uint8_t *result = u_malloc((uint32_t)length); CHECK(source && result); for (size_t i = 0; i < length; i++) source[i] = (uint8_t)(i * 17 + i / 251); size_t offset = 0;