From f03da2f4ba285e267cb5d1c775c78ba7a9396dd1 Mon Sep 17 00:00:00 2001 From: gitlost Date: Wed, 7 Jul 2021 13:58:04 +0100 Subject: [PATCH] CODE128: suppress cppcheck out-of-bounds warning; plus others (#233) --- backend/code.c | 2 +- backend/code1.c | 8 ++++---- backend/code128.c | 13 ++++++++----- backend/gridmtx.c | 14 +++++++------- backend/hanxin.c | 11 +++++------ backend/tests/test_big5.c | 4 ++-- backend/tests/test_gb2312.c | 16 ++++++++-------- backend/tests/test_ksx1001.c | 4 ++-- backend/tests/test_library.c | 8 ++++---- backend/tests/test_reedsol.c | 2 +- backend/tests/test_sjis.c | 10 +++++----- backend/tests/testcommon.c | 4 ++-- 12 files changed, 49 insertions(+), 47 deletions(-) diff --git a/backend/code.c b/backend/code.c index 5c3bb845..81bcde9b 100644 --- a/backend/code.c +++ b/backend/code.c @@ -380,7 +380,7 @@ INTERNAL int pharmazentral(struct zint_symbol *symbol, unsigned char source[], i check_digit = count % 11; if (symbol->debug & ZINT_DEBUG_PRINT) { - printf("PZN: %s, check digit %d\n", localstr, check_digit); + printf("PZN: %s, check digit %d\n", localstr, (int) check_digit); } if (check_digit == 10) { diff --git a/backend/code1.c b/backend/code1.c index 245f53d6..478ffdde 100644 --- a/backend/code1.c +++ b/backend/code1.c @@ -910,7 +910,7 @@ static int c1_encode(struct zint_symbol *symbol, unsigned char source[], unsigne if (debug_print) { printf("Target (%d):", tp); for (i = 0; i < tp; i++) { - printf(" [%d]", target[i]); + printf(" [%d]", (int) target[i]); } printf("\nLast Mode: %d\n", *p_last_mode); } @@ -999,7 +999,7 @@ INTERNAL int code_one(struct zint_symbol *symbol, unsigned char source[], int le if (symbol->debug & ZINT_DEBUG_PRINT) { printf("Codewords (%d): ", codewords); - for (i = 0; i < codewords * 2; i++) printf(" %d", data[i]); + for (i = 0; i < codewords * 2; i++) printf(" %d", (int) data[i]); printf("\n"); } @@ -1087,7 +1087,7 @@ INTERNAL int code_one(struct zint_symbol *symbol, unsigned char source[], int le if (symbol->debug & ZINT_DEBUG_PRINT) { printf("Codewords (%d):", data_cw + ecc_cw); - for (i = 0; i < data_cw + ecc_cw; i++) printf(" %d", data[i]); + for (i = 0; i < data_cw + ecc_cw; i++) printf(" %d", (int) data[i]); printf("\n"); } @@ -1177,7 +1177,7 @@ INTERNAL int code_one(struct zint_symbol *symbol, unsigned char source[], int le if (symbol->debug & ZINT_DEBUG_PRINT) { printf("Codewords (%d):", data_cw + ecc_length); - for (i = 0; i < data_cw + ecc_length; i++) printf(" %d", data[i]); + for (i = 0; i < data_cw + ecc_length; i++) printf(" %d", (int) data[i]); printf("\n"); } diff --git a/backend/code128.c b/backend/code128.c index c21103e5..11fc06a8 100644 --- a/backend/code128.c +++ b/backend/code128.c @@ -236,15 +236,18 @@ static void c128_set_a(const unsigned char source, char dest[], int values[], in * This set handles all characters which are not part of long numbers and not * control characters. */ -static void c128_set_b(const unsigned char source, char dest[], int values[], int *bar_chars) { - if (source > 127) { +static int c128_set_b(const unsigned char source, char dest[], int values[], int *bar_chars) { + if (source > 127 + 32) { /* Suppress cppcheck out-of-bounds warning with + 32 check */ strcat(dest, C128Table[source - 32 - 128]); values[(*bar_chars)] = source - 32 - 128; - } else { + } else if (source <= 127) { /* Suppress cppcheck out-of-bounds warning with <= 127 check */ strcat(dest, C128Table[source - 32]); values[(*bar_chars)] = source - 32; + } else { /* Should never happen */ + return 0; /* Not reached */ } (*bar_chars)++; + return 1; } /* Translate Code 128 Set C characters into barcodes @@ -651,7 +654,7 @@ INTERNAL int code_128(struct zint_symbol *symbol, unsigned char source[], int le read++; break; case 'b': - case 'B': c128_set_b(source[read], dest, values, &bar_characters); + case 'B': (void) c128_set_b(source[read], dest, values, &bar_characters); read++; break; case 'C': c128_set_c(source[read], source[read + 1], dest, values, &bar_characters); @@ -919,7 +922,7 @@ INTERNAL int ean_128_cc(struct zint_symbol *symbol, unsigned char source[], int break; case 'B': case 'b': - c128_set_b(reduced[read], dest, values, &bar_characters); + (void) c128_set_b(reduced[read], dest, values, &bar_characters); read++; break; case 'C': diff --git a/backend/gridmtx.c b/backend/gridmtx.c index ef701487..6e340552 100644 --- a/backend/gridmtx.c +++ b/backend/gridmtx.c @@ -543,7 +543,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con } if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, 13, binary, bp); @@ -608,7 +608,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con glyph += 1000; if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, 10, binary, bp); @@ -616,7 +616,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con glyph = (100 * (numbuf[0] - '0')) + (10 * (numbuf[1] - '0')) + (numbuf[2] - '0'); if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, 10, binary, bp); @@ -644,7 +644,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con } if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, glyph > 0xFF ? 16 : 8, binary, bp); sp++; @@ -670,7 +670,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con /* Mixed Mode character */ glyph = posn(EUROPIUM, (const char) gbdata[sp]); if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, 6, binary, bp); @@ -695,7 +695,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con /* Upper Case character */ glyph = posn("ABCDEFGHIJKLMNOPQRSTUVWXYZ ", (const char) gbdata[sp]); if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, 5, binary, bp); @@ -720,7 +720,7 @@ static int gm_encode(unsigned int gbdata[], const int length, char binary[], con /* Lower Case character */ glyph = posn("abcdefghijklmnopqrstuvwxyz ", (const char) gbdata[sp]); if (debug & ZINT_DEBUG_PRINT) { - printf("[%d] ", glyph); + printf("[%d] ", (int) glyph); } bp = bin_append_posn(glyph, 5, binary, bp); diff --git a/backend/hanxin.c b/backend/hanxin.c index f2731a1c..90ed91f0 100644 --- a/backend/hanxin.c +++ b/backend/hanxin.c @@ -643,7 +643,7 @@ static void calculate_binary(char binary[], const char mode[], unsigned int sour bp = bin_append_posn(source[i + position], source[i + position] > 0xFF ? 16 : 8, binary, bp); if (debug & ZINT_DEBUG_PRINT) { - printf("%d ", source[i + position]); + printf("%d ", (int) source[i + position]); } i++; @@ -862,7 +862,8 @@ static void hx_place_finder(unsigned char *grid, const int size, const int x, co /* Finder pattern for bottom right of symbol */ static void hx_place_finder_bottom_right(unsigned char *grid, const int size) { int xp, yp; - int x = size - 7, y = size - 7; + int x = size - 7; + int y = x; char finder[] = {0x75, 0x75, 0x75, 0x05, 0x7D, 0x01, 0x7F}; for (xp = 0; xp < 7; xp++) { @@ -1186,10 +1187,8 @@ static void make_picket_fence(const unsigned char fullstream[], unsigned char pi for (start = 0; start < 13; start++) { for (i = start; i < streamsize; i += 13) { - if (i < streamsize) { - picket_fence[output_position] = fullstream[i]; - output_position++; - } + picket_fence[output_position] = fullstream[i]; + output_position++; } } } diff --git a/backend/tests/test_big5.c b/backend/tests/test_big5.c index 46683b06..186792e9 100644 --- a/backend/tests/test_big5.c +++ b/backend/tests/test_big5.c @@ -66,9 +66,9 @@ static void test_big5_wctomb_zint(void) { val = val2 = 0; ret = big5_wctomb_zint(&val, i); ret2 = big5_wctomb_zint2(&val2, i); - assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", i, i, ret, ret2, val, val2); + assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", (int) i, i, ret, ret2, val, val2); if (ret2) { - assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", i, i, val, val2); + assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2); } } diff --git a/backend/tests/test_gb2312.c b/backend/tests/test_gb2312.c index 0118c159..12b367bb 100644 --- a/backend/tests/test_gb2312.c +++ b/backend/tests/test_gb2312.c @@ -74,18 +74,18 @@ static void test_gb2312_wctomb_zint(void) { ret = gb2312_wctomb_zint(&val, i); ret2 = gb2312_wctomb_zint2(&val2, i); if (i == 0xB7) { // Extra mapping middle dot U+00B7 to 0xA1A4, duplicate of U+30FB (Katakana middle dot) - assert_equal(ret, 2, "i:%d 0x%04X ret %d != 2, val 0x%04X\n", i, i, ret, val); - assert_equal(val, 0xA1A4, "i:%d 0x%04X val 0x%04X != 0xA1A4\n", i, i, val); - assert_zero(ret2, "i:%d 0x%04X ret2 %d != 0, val2 0x%04X\n", i, i, ret2, val2); + assert_equal(ret, 2, "i:%d 0x%04X ret %d != 2, val 0x%04X\n", (int) i, i, ret, val); + assert_equal(val, 0xA1A4, "i:%d 0x%04X val 0x%04X != 0xA1A4\n", (int) i, i, val); + assert_zero(ret2, "i:%d 0x%04X ret2 %d != 0, val2 0x%04X\n", (int) i, i, ret2, val2); } else if (i == 0x2014) { // Extra mapping em dash U+2014 to 0xA1AA, duplicate of U+2015 (horizontal bar) - assert_equal(ret, 2, "i:%d 0x%04X ret %d != 2, val 0x%04X\n", i, i, ret, val); - assert_equal(val, 0xA1AA, "i:%d 0x%04X val 0x%04X != 0xA1AA\n", i, i, val); - assert_zero(ret2, "i:%d 0x%04X ret2 %d != 0, val2 0x%04X\n", i, i, ret2, val2); + assert_equal(ret, 2, "i:%d 0x%04X ret %d != 2, val 0x%04X\n", (int) i, i, ret, val); + assert_equal(val, 0xA1AA, "i:%d 0x%04X val 0x%04X != 0xA1AA\n", (int) i, i, val); + assert_zero(ret2, "i:%d 0x%04X ret2 %d != 0, val2 0x%04X\n", (int) i, i, ret2, val2); } else { - assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", i, i, ret, ret2, val, val2); + assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", (int) i, i, ret, ret2, val, val2); } if (ret2) { - assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", i, i, val, val2); + assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2); } } diff --git a/backend/tests/test_ksx1001.c b/backend/tests/test_ksx1001.c index 1fecd675..5c767714 100644 --- a/backend/tests/test_ksx1001.c +++ b/backend/tests/test_ksx1001.c @@ -80,9 +80,9 @@ static void test_ksx1001_wctomb_zint(void) { val = val2 = 0; ret = ksx1001_wctomb_zint(&val, i); ret2 = ksx1001_wctomb_zint2(&val2, i); - assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", i, i, ret, ret2, val, val2); + assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", (int) i, i, ret, ret2, val, val2); if (ret2) { - assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", i, i, val, val2); + assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2); } } diff --git a/backend/tests/test_library.c b/backend/tests/test_library.c index c397f140..63c3bdce 100644 --- a/backend/tests/test_library.c +++ b/backend/tests/test_library.c @@ -847,7 +847,7 @@ static void test_clear(void) { assert_nonzero(symbol->rows, "ZBarcode_Buffer() rows 0\n"); assert_nonzero(symbol->width, "ZBarcode_Buffer() width 0\n"); - assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer() bitmap_byte_length %d != 0\n", symbol->bitmap_byte_length); + assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer() bitmap_byte_length %d != 0\n", (int) symbol->bitmap_byte_length); assert_null(symbol->vector, "ZBarcode_Buffer() vector != NULL\n"); ZBarcode_Clear(symbol); @@ -859,7 +859,7 @@ static void test_clear(void) { assert_zero(symbol->rows, "ZBarcode_Buffer() rows %d != 0\n", symbol->rows); assert_zero(symbol->width, "ZBarcode_Buffer() width %d != 0\n", symbol->width); - assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer() bitmap_byte_length %d != 0\n", symbol->bitmap_byte_length); + assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer() bitmap_byte_length %d != 0\n", (int) symbol->bitmap_byte_length); assert_null(symbol->vector, "ZBarcode_Buffer() vector != NULL\n"); // Vector @@ -883,7 +883,7 @@ static void test_clear(void) { assert_nonzero(symbol->width, "ZBarcode_Buffer_Vector() width 0\n"); assert_null(symbol->bitmap, "ZBarcode_Buffer_Vector() bitmap != NULL\n"); assert_null(symbol->alphamap, "ZBarcode_Buffer_Vector() alphamap != NULL\n"); - assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer_Vector() bitmap_byte_length %d != 0\n", symbol->bitmap_byte_length); + assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer_Vector() bitmap_byte_length %d != 0\n", (int) symbol->bitmap_byte_length); ZBarcode_Clear(symbol); @@ -891,7 +891,7 @@ static void test_clear(void) { assert_zero(symbol->rows, "ZBarcode_Buffer_Vector() rows %d != 0\n", symbol->rows); assert_zero(symbol->width, "ZBarcode_Buffer_Vector() width %d != 0\n", symbol->width); - assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer_Vector() bitmap_byte_length %d != 0\n", symbol->bitmap_byte_length); + assert_zero(symbol->bitmap_byte_length, "ZBarcode_Buffer_Vector() bitmap_byte_length %d != 0\n", (int) symbol->bitmap_byte_length); assert_null(symbol->bitmap, "ZBarcode_Buffer_Vector() bitmap != NULL\n"); assert_null(symbol->alphamap, "ZBarcode_Buffer_Vector() alphamap != NULL\n"); assert_zero(symbol->bitmap_width, "ZBarcode_Buffer_Vector() bitmap_width %d != 0\n", symbol->bitmap_width); diff --git a/backend/tests/test_reedsol.c b/backend/tests/test_reedsol.c index b5f3d2c9..bce728c7 100644 --- a/backend/tests/test_reedsol.c +++ b/backend/tests/test_reedsol.c @@ -207,7 +207,7 @@ static void test_encoding_uint(int index) { //fprintf(stderr, "exp "); for (j = 0; j < data[i].nsym; j++) fprintf(stderr, "%d ", data[i].expected[j]); fprintf(stderr, "\n"); for (j = 0; j < data[i].nsym; j++) { int k = data[i].nsym - 1 - j; - assert_equal(res[k], data[i].expected[j], "i:%d res[%d] %d != expected[%d] %d\n", i, k, res[k], j, data[i].expected[j]); + assert_equal(res[k], data[i].expected[j], "i:%d res[%d] %d != expected[%d] %d\n", i, k, (int) res[k], j, (int) data[i].expected[j]); } } diff --git a/backend/tests/test_sjis.c b/backend/tests/test_sjis.c index a018d644..2b6b2926 100644 --- a/backend/tests/test_sjis.c +++ b/backend/tests/test_sjis.c @@ -97,14 +97,14 @@ static void test_sjis_wctomb_zint(void) { ret = sjis_wctomb_zint(&val, i); ret2 = sjis_wctomb_zint2(&val2, i); if (i == 0xFF3C) { // Extra mapping full-width reverse solidus U+FF3C to 0x815F, duplicate of U+005C (backslash) - assert_equal(ret, 2, "i:%d 0x%04X ret %d != 2, val 0x%04X\n", i, i, ret, val); - assert_equal(val, 0x815F, "i:%d 0x%04X val 0x%04X != 0x815F\n", i, i, val); - assert_zero(ret2, "i:%d 0x%04X ret2 %d != 0, val2 0x%04X\n", i, i, ret2, val2); + assert_equal(ret, 2, "i:%d 0x%04X ret %d != 2, val 0x%04X\n", (int) i, i, ret, val); + assert_equal(val, 0x815F, "i:%d 0x%04X val 0x%04X != 0x815F\n", (int) i, i, val); + assert_zero(ret2, "i:%d 0x%04X ret2 %d != 0, val2 0x%04X\n", (int) i, i, ret2, val2); } else { - assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", i, i, ret, ret2, val, val2); + assert_equal(ret, ret2, "i:%d 0x%04X ret %d != ret2 %d, val 0x%04X, val2 0x%04X\n", (int) i, i, ret, ret2, val, val2); } if (ret2) { - assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", i, i, val, val2); + assert_equal(val, val2, "i:%d 0x%04X val 0x%04X != val2 0x%04X\n", (int) i, i, val, val2); } } diff --git a/backend/tests/testcommon.c b/backend/tests/testcommon.c index 3ad2ab98..7c6463e6 100644 --- a/backend/tests/testcommon.c +++ b/backend/tests/testcommon.c @@ -612,9 +612,9 @@ const char *testUtilOption3Name(int option_3) { if (high_byte) { if (option_3 & 0xFF) { - sprintf(buffer, "%s | (%d << 8)", name, high_byte); + sprintf(buffer, "%s | (%d << 8)", name, (int) high_byte); } else { - sprintf(buffer, "%d << 8", high_byte); + sprintf(buffer, "%d << 8", (int) high_byte); } return buffer; }