From fa9af12fc6a60915df9cb546cf3a46ee4acce3ec Mon Sep 17 00:00:00 2001 From: gitlost Date: Thu, 19 Dec 2019 00:59:51 +0000 Subject: [PATCH] raster.c: check bounds on calculating block width for UPCA --- backend/raster.c | 6 +++--- backend/tests/test_upcean.c | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/backend/raster.c b/backend/raster.c index 13d83825..25fac7bb 100644 --- a/backend/raster.c +++ b/backend/raster.c @@ -837,7 +837,7 @@ static int plot_raster_default(struct zint_symbol *symbol, int rotate_angle, int block_width = 0; do { block_width++; - } while ((i + block_width < symbol->width )&& module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); + } while ((i + block_width < symbol->width) && module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); if ((addon_latch == 0) && (r == 0) && (i > main_width)) { plot_height = (int) (row_height - 5.0); plot_yposn = (int) (row_posn - 5.0); @@ -949,7 +949,7 @@ static int plot_raster_default(struct zint_symbol *symbol, int rotate_angle, int block_width = 0; do { block_width++; - } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); + } while ((i + block_width < symbol->width) && module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); if (latch == 1) { /* a bar */ draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); @@ -968,7 +968,7 @@ static int plot_raster_default(struct zint_symbol *symbol, int rotate_angle, int block_width = 0; do { block_width++; - } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); + } while ((i + block_width < symbol->width) && module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); if (latch == 1) { /* a bar */ draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); diff --git a/backend/tests/test_upcean.c b/backend/tests/test_upcean.c index 37ae510f..a3bb734e 100644 --- a/backend/tests/test_upcean.c +++ b/backend/tests/test_upcean.c @@ -78,6 +78,46 @@ static void test_upce_length(void) testFinish(); } +// Note requires ZINT_SANITIZE to be set +static void test_upca_print(void) +{ + testStart(""); + + int ret; + struct item { + int symbology; + unsigned char* data; + int ret; + }; + // s/\/\*[ 0-9]*\*\//\=printf("\/*%3d*\/", line(".") - line("'<")) + struct item data[] = { + /* 0*/ { BARCODE_UPCA, "01234567890", 0 }, + }; + int data_size = sizeof(data) / sizeof(struct item); + + for (int i = 0; i < data_size; i++) { + + struct zint_symbol* symbol = ZBarcode_Create(); + assert_nonnull(symbol, "Symbol not created\n"); + + symbol->symbology = data[i].symbology; + int length = strlen(data[i].data); + + ret = ZBarcode_Encode(symbol, data[i].data, length); + assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret); + + strcpy(symbol->outfile, "out.gif"); + ret = ZBarcode_Print(symbol, 0); + assert_zero(ret, "i:%d %s ZBarcode_Print %s ret %d != 0\n", i, testUtilBarcodeName(data[i].symbology), symbol->outfile, ret); + + assert_zero(remove(symbol->outfile), "i:%d remove(%s) != 0\n", i, symbol->outfile); + + ZBarcode_Delete(symbol); + } + + testFinish(); +} + static void test_isbn(void) { testStart(""); @@ -215,6 +255,7 @@ static void test_vector_same(void) int main() { test_upce_length(); + test_upca_print(); test_isbn(); test_vector_same();