From 548847e77e37be6339314840bbad7f7e9a3e4ffa Mon Sep 17 00:00:00 2001 From: Matthew Oliver Date: Thu, 31 Jul 2025 16:44:51 +1000 Subject: [PATCH] Make sure alg_sig_handle->tbl1_l is defined On a newer GCC that runs with `-Werror=maybe-uninitialized` liberasurecode can not be built because there isn't a guard around the intialisation of table 1 (tbl1_l) in alg_sig_handle which causes: In function 'init_alg_sig_w16', inlined from 'init_alg_sig' at utils/chksum/alg_sig.c:242:12: utils/chksum/alg_sig.c:201:21: error: '*alg_sig_handle.tbl1_l' may be used uninitialized [-Werror=maybe-uninitialized] 201 | alg_sig_handle->tbl1_l[i] = alg_sig_handle->mult_routines.galois_single_multiply((unsigned short) (i << 8), alpha, w); | ~~~~~~~~~~~~~~^~~~~~~~ cc1: all warnings being treated as errors make[2]: *** [Makefile:734: utils/chksum/liberasurecode_la-alg_sig.lo] Error 1 This patch wraps wraps the missing if statement guard around it, so it's only ever initialised when used. Drive by check that initialization with malloc was actually successful. Change-Id: Ib4c29fa56821ebd3b9472c5d379f449d31b5e104 Signed-off-by: Matthew Oliver --- src/utils/chksum/alg_sig.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/utils/chksum/alg_sig.c b/src/utils/chksum/alg_sig.c index c016dd8..c3147d4 100644 --- a/src/utils/chksum/alg_sig.c +++ b/src/utils/chksum/alg_sig.c @@ -183,6 +183,12 @@ alg_sig_t *init_alg_sig_w16(void *jerasure_sohandle, int sig_len) if (num_components >= 2) { alg_sig_handle->tbl1_l = (int*)malloc(sizeof(int) * num_gf_lr_table_syms); alg_sig_handle->tbl1_r = (int*)malloc(sizeof(int) * num_gf_lr_table_syms); + if (NULL == alg_sig_handle->tbl1_l || NULL == alg_sig_handle->tbl1_r) { + free(alg_sig_handle->tbl1_l); + free(alg_sig_handle->tbl1_r); + free(alg_sig_handle); + return NULL; + } } if (num_components >= 4) { @@ -190,6 +196,15 @@ alg_sig_t *init_alg_sig_w16(void *jerasure_sohandle, int sig_len) alg_sig_handle->tbl2_r = (int*)malloc(sizeof(int) * num_gf_lr_table_syms); alg_sig_handle->tbl3_l = (int*)malloc(sizeof(int) * num_gf_lr_table_syms); alg_sig_handle->tbl3_r = (int*)malloc(sizeof(int) * num_gf_lr_table_syms); + if (NULL == alg_sig_handle->tbl2_l || NULL == alg_sig_handle->tbl2_r || + NULL == alg_sig_handle->tbl3_l || NULL == alg_sig_handle->tbl3_r) { + free(alg_sig_handle->tbl2_l); + free(alg_sig_handle->tbl2_r); + free(alg_sig_handle->tbl3_l); + free(alg_sig_handle->tbl3_r); + free(alg_sig_handle); + return NULL; + } } /* @@ -198,8 +213,10 @@ alg_sig_t *init_alg_sig_w16(void *jerasure_sohandle, int sig_len) * Note that \gamme = 8 (\alpha ^ 3 MOD 2^16) */ for (i = 0; i < 256; i++) { - alg_sig_handle->tbl1_l[i] = alg_sig_handle->mult_routines.galois_single_multiply((unsigned short) (i << 8), alpha, w); - alg_sig_handle->tbl1_r[i] = alg_sig_handle->mult_routines.galois_single_multiply((unsigned short) i, alpha, w); + if (num_components >= 2) { + alg_sig_handle->tbl1_l[i] = alg_sig_handle->mult_routines.galois_single_multiply((unsigned short) (i << 8), alpha, w); + alg_sig_handle->tbl1_r[i] = alg_sig_handle->mult_routines.galois_single_multiply((unsigned short) i, alpha, w); + } if (num_components >= 4) { alg_sig_handle->tbl2_l[i] = alg_sig_handle->mult_routines.galois_single_multiply((unsigned short) (i << 8), beta, w); @@ -260,8 +277,10 @@ void destroy_alg_sig(alg_sig_t* alg_sig_handle) int num_components = alg_sig_handle->sig_len / alg_sig_handle->gf_w; - free(alg_sig_handle->tbl1_l); - free(alg_sig_handle->tbl1_r); + if (num_components >= 2) { + free(alg_sig_handle->tbl1_l); + free(alg_sig_handle->tbl1_r); + } if (num_components >= 4) { free(alg_sig_handle->tbl2_l); free(alg_sig_handle->tbl2_r);