From c11ab5528d5777c5a8c3a8784816224b019d390a Mon Sep 17 00:00:00 2001 From: gitlost Date: Sun, 23 Feb 2025 19:48:55 +0000 Subject: [PATCH] general: suppress some CodeQL warnings CODABLOCKF: tidy some comments --- backend/codablock.c | 20 ++++++++++---------- backend/dmatrix.c | 2 +- backend/dxfilmedge.c | 5 +++-- backend/gridmtx.c | 2 -- backend/library.c | 2 +- backend/ultra.c | 2 +- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/backend/codablock.c b/backend/codablock.c index 623ca1c6..67e7d537 100644 --- a/backend/codablock.c +++ b/backend/codablock.c @@ -69,7 +69,7 @@ typedef struct sCharacterSetTable { } CharacterSetTable; /* Find the possible Code-128 Character sets for a character - * The result is an or of CodeA, CodeB, CodeC, CodeFNC1, CodeFNC4 depending on the + * The result is an OR of CodeA, CodeB, CodeC, CodeFNC1, CodeFNC4 depending on the * possible Code 128 character sets. */ static int GetPossibleCharacterSet(unsigned char C) { @@ -87,13 +87,11 @@ static int GetPossibleCharacterSet(unsigned char C) { } /* Create a Table with the following information for each Data character: - * int CharacterSet is an or of CodeA, CodeB, CodeC, CodeFNC1, CodeFNC4, - * depending on which character set is applicable. - * (Result of GetPossibleCharacterSet) - * int AFollowing,BFollowing The number of source characters you still may encode - * in this character set. - * int CFollowing The number of characters encodable in CodeC if we - * start here. + * int CharacterSet is an OR of CodeA, CodeB, CodeC, CodeFNC1, CodeFNC4, + * depending on which character set is applicable. + * (Result of GetPossibleCharacterSet) + * int AFollowing, BFollowing The number of source characters you still may encode in this character set. + * int CFollowing The number of characters encodable in CodeC if we start here. */ static void CreateCharacterSetTable(CharacterSetTable T[], unsigned char *data, const int dataLength) { int charCur; @@ -260,6 +258,7 @@ static int Columns2Rows(struct zint_symbol *symbol, CharacterSetTable *T, const if (isFNC4) { /* So skip FNC4 and shift value instead */ --emptyColumns; ++charCur; + assert(charCur < dataLength); /* FNC4s always followed by char */ } pSet[charCur] |= CShift; } else { @@ -292,6 +291,7 @@ static int Columns2Rows(struct zint_symbol *symbol, CharacterSetTable *T, const if (isFNC4) { /* So skip FNC4 and shift value instead */ --emptyColumns; ++charCur; + assert(charCur < dataLength); /* FNC4s always followed by char */ } pSet[charCur] |= CShift; } else { @@ -684,7 +684,7 @@ INTERNAL int codablockf(struct zint_symbol *symbol, unsigned char source[], int /* >>> Build C128 code numbers */ /* The C128 column count contains Start (2CW), Row ID, Checksum, Stop */ - pOutput = (unsigned char *) z_alloca(columns * rows); + pOutput = (unsigned char *) z_alloca((size_t) columns * (size_t) rows); pOutPos = pOutput; charCur = 0; /* >> Loop over rows */ @@ -780,7 +780,7 @@ INTERNAL int codablockf(struct zint_symbol *symbol, unsigned char source[], int --emptyColumns; } /* >> End Criteria */ - if ((pSet[charCur] & CFill) || (pSet[charCur] & CEnd)) { + if (charCur < dataLength && ((pSet[charCur] & CFill) || (pSet[charCur] & CEnd))) { /* Fill line but leave space for checks in last line */ if (rowCur == rows - 1) { emptyColumns -= 2; diff --git a/backend/dmatrix.c b/backend/dmatrix.c index 60558a5f..33bd4f5d 100644 --- a/backend/dmatrix.c +++ b/backend/dmatrix.c @@ -1904,7 +1904,7 @@ static int dm_ecc200(struct zint_symbol *symbol, struct zint_seg segs[], const i const int NC = W - 2 * (W / FW); const int NR = H - 2 * (H / FH); int x, y, *places; - if (!(places = (int *) calloc(NC * NR, sizeof(int)))) { + if (!(places = (int *) calloc((size_t) NC * (size_t) NR, sizeof(int)))) { return errtxt(ZINT_ERROR_MEMORY, symbol, 718, "Insufficient memory for placement array"); } dm_placement(places, NR, NC); diff --git a/backend/dxfilmedge.c b/backend/dxfilmedge.c index 1893ab55..e50ae6a2 100644 --- a/backend/dxfilmedge.c +++ b/backend/dxfilmedge.c @@ -138,7 +138,8 @@ static int dx_parse_code(struct zint_symbol *symbol, const unsigned char *source int dx_extract; /* DX format is either 4 digits (DX Extract, eg: 1271) or 6 digits (DX Full, eg: 012710) */ if (debug_print) printf("No \"-\" separator, computing from DX Extract (4 digits) or DX Full (6 digits)\n"); - if (dx_length == 5 || dx_length > 6) { + assert(dx_length <= 6); /* I.e. DX_MAX_DX_INFO_LENGTH, guaranteed above */ + if (dx_length == 5) { return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 980, "DX number \"%s\" is incorrect; expected 4 digits (DX extract) or 6 digits (DX full)", dx_info); } @@ -154,7 +155,7 @@ static int dx_parse_code(struct zint_symbol *symbol, const unsigned char *source } /* Compute the DX parts 1 and 2 from the DX extract */ dx_extract = to_int((const unsigned char *) dx_info, dx_length); - assert(dx_extract != -1); + assert(dx_extract != -1); /* All digits (no hyphen) & length 6 max - can't fail */ if (dx_extract < 16 || dx_extract > 2047) { return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 981, "DX extract \"%d\" out of range (16 to 2047)", dx_extract); diff --git a/backend/gridmtx.c b/backend/gridmtx.c index c9f61d44..653b7714 100644 --- a/backend/gridmtx.c +++ b/backend/gridmtx.c @@ -875,8 +875,6 @@ static void gm_add_ecc(const char binary[], const int data_posn, const int layer } data_size = block_size - ecc_size; - /* printf("block %d/%d: data %d / ecc %d\n", i + 1, (b1 + b2), data_size, ecc_size);*/ - for (j = 0; j < data_size; j++) { data_block[j] = data[wp]; wp++; diff --git a/backend/library.c b/backend/library.c index 29bbddb5..1218c43a 100644 --- a/backend/library.c +++ b/backend/library.c @@ -1531,7 +1531,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, const char *filename) { } /* Allocate memory */ - buffer = (unsigned char *) malloc(fileLen); + buffer = (unsigned char *) malloc((size_t) fileLen); if (!buffer) { if (file_opened) { (void) fclose(file); diff --git a/backend/ultra.c b/backend/ultra.c index 788c405f..fbdb0cf3 100644 --- a/backend/ultra.c +++ b/backend/ultra.c @@ -1141,7 +1141,7 @@ INTERNAL int ultra(struct zint_symbol *symbol, struct zint_seg segs[], const int total_width = columns + 6; /* Build symbol */ - pattern = (char *) z_alloca(total_height * total_width); + pattern = (char *) z_alloca((size_t) total_height * (size_t) total_width); for (i = 0; i < (total_height * total_width); i++) { pattern[i] = 'W';