1
0
mirror of https://git.code.sf.net/p/zint/code synced 2026-05-09 23:53:49 +00:00
Files
zint/backend/tests/fuzz/fuzz_gs1.c
gitlost 56fca5b2a1 raster/vector: fix BARCODE_BIND_TOP trumping BARCODE_BOX
backend/tests/fuzz: update "fuzz.h" to take `option_3` min/max;
  update `INPUT_MODE_MASK` & `OUTPUT_OPTIONS_MASK`;
  set `height`, `border_width` & `output_options` from input
  (removing `Z_FUZZ_SET_OUTPUT_OPTIONS` conditional) and call
  `ZBarcode_Encode_and_Print()` instead of `ZBarcode_Encode()`;
  support standalone builds of "fuzz_data" & "fuzz_gs1" with
  `Z_FUZZ_MAIN_READ_ARGV_1` macro;
  update "fuzz_gs1" & "fuzz_gs1.dict" to better test caret/raw
  modes;
  gen_corpora: add `output_options`, `height` & `border_width` &
  find (3rd) from fuzz_data of BIND_TOP | BIND_BOX bug
2026-03-22 15:05:53 +00:00

149 lines
5.7 KiB
C

/* fuzz_gs1.c - fuzzer for libzint (GS1-enabled symbologies, GS1_MODE) */
/*
libzint - the open source barcode library
Copyright (C) 2024-2026 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/* SPDX-License-Identifier: BSD-3-Clause */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#if 0
#define Z_FUZZ_DEBUG /* Set `symbol->debug` flag */
#endif
#include "fuzz.h"
static const int symbologies[] = {
BARCODE_AZTEC, BARCODE_CODE16K, BARCODE_CODE49, BARCODE_CODEONE, BARCODE_DATAMATRIX, BARCODE_DBAR_EXP,
BARCODE_DBAR_EXPSTK, BARCODE_DOTCODE, BARCODE_GS1_128, BARCODE_QRCODE, BARCODE_RMQR, BARCODE_ULTRA,
BARCODE_EANX_CC, BARCODE_GS1_128_CC, BARCODE_DBAR_OMN_CC, BARCODE_DBAR_LTD_CC, BARCODE_DBAR_EXP_CC,
BARCODE_UPCA_CC, BARCODE_UPCE_CC, BARCODE_DBAR_STK_CC, BARCODE_DBAR_OMNSTK_CC, BARCODE_DBAR_EXPSTK_CC,
};
#if Z_FUZZ_MAIN
/* For testing that a corpus file reproduces a bug:
cc -g -O0 -DZ_FUZZ_MAIN fuzz_gs1.c -o fuzz_data -lzint -fsanitize=address
./fuzz_gs1 <corpus-file>
*/
#include <errno.h>
#include <limits.h>
Z_FUZZ_MAIN_READ_ARGV_1(i, gs1_buf)
#else
int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
struct zint_symbol *symbol;
int i;
unsigned char *gs1_buf;
#endif
/* Ignore empty or very large input */
if (size < 1 || size > 10000) {
return 0;
}
symbol = ZBarcode_Create();
assert(symbol);
gs1_buf = (unsigned char *) malloc(size + 2);
assert(gs1_buf);
gs1_buf[0] = '['; /* Add dummy AI - along with GS1NOCHECK_MODE disables GS1 verification */
gs1_buf[1] = ']';
for (i = 0; i < ZARRAY_SIZE(symbologies); i++) {
static const char primary_ai_raw[] = "0112345678901231";
static const char primary_ai_caret[] = "^0112345678901231";
static const char primary_ai_parens[] = "(01)12345678901231";
static const char primary_ai[] = "[01]12345678901231";
static const char primary_upce[] = "12345670";
static const char primary[] = "123456789012";
const int idx = symbologies[i];
const int is_composite = ZBarcode_Cap(idx, ZINT_CAP_COMPOSITE) & ZINT_CAP_COMPOSITE;
const unsigned char *input;
int input_mode;
int length;
int ret;
assert(ZBarcode_ValidID(idx));
input = data;
length = set_symbol(symbol, idx, !is_composite /*chk_sane*/, 1 /*no_eci*/, &input, size);
if (!length) {
continue;
}
input_mode = symbol->input_mode;
symbol->output_options |= BARCODE_MEMORY_FILE;
if (is_composite) {
if (idx == BARCODE_GS1_128_CC || idx == BARCODE_DBAR_EXP_CC || idx == BARCODE_DBAR_EXPSTK_CC) {
if (input_mode & GS1RAW_MODE) {
memcpy(symbol->primary, primary_ai_raw, sizeof(primary_ai_raw));
} else if (input[0] == '^') {
memcpy(symbol->primary, primary_ai_caret, sizeof(primary_ai_caret));
} else if (input_mode & GS1PARENS_MODE) {
memcpy(symbol->primary, primary_ai_parens, sizeof(primary_ai_parens));
} else {
memcpy(symbol->primary, primary_ai, sizeof(primary_ai));
}
} else if (idx == BARCODE_UPCE_CC) {
memcpy(symbol->primary, primary_upce, sizeof(primary_upce));
} else {
memcpy(symbol->primary, primary, sizeof(primary));
}
}
/* Try it first with GS1NOCHECK_MODE */
symbol->input_mode = (input_mode & ~0x07) | GS1_MODE | GS1NOCHECK_MODE;
ret = ZBarcode_Encode_and_Print(symbol, input, length, 0 /*rotate_angle*/);
assert(ret != ZINT_ERROR_ENCODING_PROBLEM);
ZBarcode_Clear(symbol);
/* Now without GS1NOCHECK_MODE */
symbol->input_mode = (input_mode & ~0x07) | GS1_MODE;
symbol->input_mode &= ~GS1NOCHECK_MODE;
memcpy(gs1_buf + 2, input, length);
ret = ZBarcode_Encode_and_Print(symbol, gs1_buf, length + 2, 90 * (size % 3) /*rotate_angle*/);
assert(ret != ZINT_ERROR_ENCODING_PROBLEM);
}
(void) free(gs1_buf);
ZBarcode_Delete(symbol);
return 0;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* vim: set ts=4 sw=4 et : */