QRCODE: fix out-of-bounds crash due to incorrect mode costings for

GS1 percents in `qr_in_alpha()`, ticket #300, props Andre Maute;
  also defensively re-calc version 40 mode to prevent possible
  further crashes
common: move `debug_print_escape()` from library to common
This commit is contained in:
gitlost 2023-11-30 09:12:11 +00:00
parent db92c7de57
commit 888db0bf00
13 changed files with 445 additions and 205 deletions

View file

@ -1,6 +1,6 @@
/*
libzint - the open source barcode library
Copyright (C) 2020-2022 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2020-2023 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -86,11 +86,11 @@ static void test_large(const testCtx *const p_ctx) {
};
int data_size = ARRAY_SIZE(data);
int i, length, ret;
struct zint_symbol *symbol;
struct zint_symbol *symbol = NULL;
char data_buf[ZINT_MAX_DATA_LEN + 1];
testStart("test_large");
testStartSymbol("test_large", &symbol);
for (i = 0; i < data_size; i++) {
@ -141,11 +141,11 @@ static void test_reader_init(const testCtx *const p_ctx) {
};
int data_size = ARRAY_SIZE(data);
int i, length, ret;
struct zint_symbol *symbol;
struct zint_symbol *symbol = NULL;
char escaped[1024];
testStart("test_reader_init");
testStartSymbol("test_reader_init", &symbol);
for (i = 0; i < data_size; i++) {
@ -163,7 +163,8 @@ static void test_reader_init(const testCtx *const p_ctx) {
if (p_ctx->generate) {
printf(" /*%3d*/ { %s, %s, %s, \"%s\", %s, %d, %d, \"%s\", \"%s\" },\n",
i, testUtilInputModeName(data[i].input_mode), testUtilOutputOptionsName(data[i].output_options), testUtilOption3Name(data[i].option_3),
i, testUtilInputModeName(data[i].input_mode), testUtilOutputOptionsName(data[i].output_options),
testUtilOption3Name(BARCODE_ULTRA, data[i].option_3),
testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
testUtilErrorName(data[i].ret), symbol->rows, symbol->width, symbol->errtxt, data[i].comment);
} else {
@ -266,11 +267,11 @@ static void test_input(const testCtx *const p_ctx) {
};
int data_size = ARRAY_SIZE(data);
int i, length, ret;
struct zint_symbol *symbol;
struct zint_symbol *symbol = NULL;
char escaped[1024];
testStart("test_input");
testStartSymbol("test_input", &symbol);
for (i = 0; i < data_size; i++) {
@ -293,7 +294,8 @@ static void test_input(const testCtx *const p_ctx) {
if (p_ctx->generate) {
printf(" /*%3d*/ { %s, %d, %d, %d, %s, { %d, %d, \"%s\" }, \"%s\", %s, \"%s\", \"%s\" },\n",
i, testUtilInputModeName(data[i].input_mode), data[i].eci, data[i].option_1, data[i].option_2, testUtilOption3Name(data[i].option_3),
i, testUtilInputModeName(data[i].input_mode), data[i].eci, data[i].option_1, data[i].option_2,
testUtilOption3Name(BARCODE_ULTRA, data[i].option_3),
data[i].structapp.index, data[i].structapp.count, data[i].structapp.id,
testUtilEscape(data[i].data, length, escaped, sizeof(escaped)), testUtilErrorName(data[i].ret), symbol->errtxt, data[i].comment);
} else {
@ -776,7 +778,7 @@ static void test_encode(const testCtx *const p_ctx) {
};
int data_size = ARRAY_SIZE(data);
int i, length, ret;
struct zint_symbol *symbol;
struct zint_symbol *symbol = NULL;
char escaped[1024];
char bwipp_buf[32768];
@ -784,7 +786,7 @@ static void test_encode(const testCtx *const p_ctx) {
int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
testStart("test_encode");
testStartSymbol("test_encode", &symbol);
for (i = 0; i < data_size; i++) {
@ -803,7 +805,8 @@ static void test_encode(const testCtx *const p_ctx) {
if (p_ctx->generate) {
printf(" /*%3d*/ { %s, %d, %d, %d, %s, { %d, %d, \"%s\" }, \"%s\", %s, %d, %d, %d, \"%s\",\n",
i, testUtilInputModeName(data[i].input_mode), data[i].eci, data[i].option_1, data[i].option_2, testUtilOption3Name(data[i].option_3),
i, testUtilInputModeName(data[i].input_mode), data[i].eci, data[i].option_1, data[i].option_2,
testUtilOption3Name(BARCODE_ULTRA, data[i].option_3),
data[i].structapp.index, data[i].structapp.count, data[i].structapp.id,
testUtilEscape(data[i].data, length, escaped, sizeof(escaped)), testUtilErrorName(data[i].ret),
symbol->rows, symbol->width, data[i].bwipp_cmp, data[i].comment);
@ -1017,7 +1020,7 @@ static void test_encode_segs(const testCtx *const p_ctx) {
};
int data_size = ARRAY_SIZE(data);
int i, j, seg_count, ret;
struct zint_symbol *symbol;
struct zint_symbol *symbol = NULL;
char escaped[1024];
char bwipp_buf[32768];
@ -1025,7 +1028,7 @@ static void test_encode_segs(const testCtx *const p_ctx) {
int do_bwipp = (debug & ZINT_DEBUG_TEST_BWIPP) && testUtilHaveGhostscript(); /* Only do BWIPP test if asked, too slow otherwise */
testStart("test_encode_segs");
testStartSymbol("test_encode_segs", &symbol);
for (i = 0; i < data_size; i++) {
@ -1051,7 +1054,8 @@ static void test_encode_segs(const testCtx *const p_ctx) {
int length1 = data[i].segs[1].length == -1 ? (int) ustrlen(data[i].segs[1].source) : data[i].segs[1].length;
int length2 = data[i].segs[2].length == -1 ? (int) ustrlen(data[i].segs[2].source) : data[i].segs[2].length;
printf(" /*%3d*/ { %s, %d, %d, %s, { %d, %d, \"%s\" }, { { TU(\"%s\"), %d, %d }, { TU(\"%s\"), %d, %d }, { TU(\"%s\"), %d, %d } }, %s, %d, %d, %d, \"%s\",\n",
i, testUtilInputModeName(data[i].input_mode), data[i].option_1, data[i].option_2, testUtilOption3Name(data[i].option_3),
i, testUtilInputModeName(data[i].input_mode), data[i].option_1, data[i].option_2,
testUtilOption3Name(BARCODE_ULTRA, data[i].option_3),
data[i].structapp.index, data[i].structapp.count, data[i].structapp.id,
testUtilEscape((const char *) data[i].segs[0].source, length, escaped, sizeof(escaped)), data[i].segs[0].length, data[i].segs[0].eci,
testUtilEscape((const char *) data[i].segs[1].source, length1, escaped1, sizeof(escaped1)), data[i].segs[1].length, data[i].segs[1].eci,