From b6696a10d6288483019bf0682ba06aa68bb28a27 Mon Sep 17 00:00:00 2001 From: Kia <kia@special-circumstanc.es> Date: Thu, 9 Sep 2021 17:47:17 -0600 Subject: [PATCH] other files --- Makefile | 26 ++++++++++++++++ README | 23 ++++++++++++++ bug1.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++ bug2.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bug3.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ span.c | 53 ++++++++++++++++++++++++++++++++ 6 files changed, 364 insertions(+) create mode 100755 Makefile create mode 100755 README create mode 100755 bug1.c create mode 100755 bug2.c create mode 100755 bug3.c create mode 100755 span.c diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..1fc4664 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +CFLAGS= -Wall -pedantic -std=c11 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/home/thz/include -L/home/thz/lib -ggdb +LIBS= -lhammer -lglib-2.0 + + +all: bug1 bug2 bug3 + +bug1: bug1.c + gcc $(CFLAGS) -o bug1 bug1.c $(LIBS) + +bug2: bug2.c + gcc $(CFLAGS) -o bug2 bug2.c $(LIBS) + +bug3: bug3.c + gcc $(CFLAGS) -o bug3 bug3.c $(LIBS) + +run: + @echo "[Bug1]" + @./bug1 -k + @echo "[Bug2]" + @./bug2 -k + @echo "[Bug3]" + @./bug3 -k + +clean: + rm -f bug1 bug2 bug3 + diff --git a/README b/README new file mode 100755 index 0000000..9569b77 --- /dev/null +++ b/README @@ -0,0 +1,23 @@ + +Here are 3 self-contained bug files. However, I believe that they may +all be the same bug in different guises. + +All 3 parsers work with PB_MIN, but fail with PB_LALR. +See the constant BKEND to change between the two. + +bug1.c uses: + + H_RULE(int8range, h_int_range(h_int8(), -1, 1)); + + +bug2.c uses: + + /* parse a single int16 between the values of -2 and 1 */ + H_RULE(bigint16, h_with_endianness(BYTE_BIG_ENDIAN,h_int16())); + H_RULE(int16range, h_int_range(bigint16,-2,1)); + +bug3.c uses: + + /* parse a single int16 between the values of -2 and 1 */ + H_RULE(littleint16, h_with_endianness(BYTE_LITTLE_ENDIAN,h_int16())); + H_RULE(int16range, h_int_range(littleint16,-2,1)); diff --git a/bug1.c b/bug1.c new file mode 100755 index 0000000..db90620 --- /dev/null +++ b/bug1.c @@ -0,0 +1,78 @@ +/* + * bug1 -- h_int8() issues + * + * This parser passes all tests if the backend is PB_MIN but fails if + * the backend is PB_LALR. + * + * See defines for BKEND below. + */ + +#include <glib.h> + +#include <hammer/hammer.h> +#include <hammer/glue.h> +#include <hammer/test_suite.h> + +#define BKEND PB_LALR +//#define BKEND PB_MIN + +const HParser *pp; /* the parser */ + +uint8_t input[1024]; /* the input is a buff of bytes */ + +/* The Parser */ +void init_parser() { + /* parse a single int8 between the values of -1 and 1 */ + H_RULE(int8range, h_int_range(h_uint8(), -1, 1)); + pp = h_sequence(int8range,h_end_p(),NULL); +} + +/* Passing Tests: -1,0,+1 */ +static void test_m1() { + input[0] = 0xFF; // -1 + g_check_parse_ok(pp, BKEND, input, 1); +} + +static void test_zero() { + input[0] = 0x00; // 0 + g_check_parse_ok(pp, BKEND, input, 1); +} + +static void test_p1() { + input[0] = 0x01; // +1 + g_check_parse_ok(pp, BKEND, input, 1); +} + +/* Failing Tests: -2,+2,+1+1 */ +static void test_m2() { + input[0] = 0xFE; // -2 + g_check_parse_failed(pp, BKEND, input, 1); +} + +static void test_p2() { + input[0] = 0x02; // +2 + g_check_parse_failed(pp, BKEND, input, 1); +} + +static void test_2val() { + input[0] = 0x01; // +1 + input[1] = 0x01; // +1 + g_check_parse_failed(pp, BKEND, input, 2); +} + + +void register_bug_tests() { + g_test_add_func("/pass/neg1",test_m1); + g_test_add_func("/pass/zero",test_zero); + g_test_add_func("/pass/pos1",test_p1); + g_test_add_func("/fail/neg2",test_m2); + g_test_add_func("/fail/pos2",test_p2); + g_test_add_func("/fail/2val",test_2val); +} + +int main(int argc, char *argv[]) { + init_parser(); + g_test_init(&argc,&argv,NULL); + register_bug_tests(); + return g_test_run(); +} diff --git a/bug2.c b/bug2.c new file mode 100755 index 0000000..398fbf0 --- /dev/null +++ b/bug2.c @@ -0,0 +1,92 @@ +/* + * bug2 -- h_int16() issues using big endian + * + * This parser passes all tests if the backend is PB_MIN but fails if + * the backend is PB_LALR. + * + * See defines for BKEND below. + */ +#include <hammer/hammer.h> +#include <hammer/glue.h> +#include <hammer/test_suite.h> +#include <glib.h> + +#define BKEND PB_LALR +//#define BKEND PB_MIN + +const HParser *pp; /* the parser */ + +uint8_t input[1024]; /* the input is a buff of bytes */ + +/* The Parser */ +void init_parser() { + /* parse a single int16 between the values of -2 and 1 */ + H_RULE(bigint16, h_with_endianness(BYTE_BIG_ENDIAN,h_int16())); + H_RULE(int16range, h_int_range(bigint16,-2,1)); + pp = h_sequence(int16range,h_end_p(),NULL); +} + +/* Passing Tests: -2,-1,0,+1 */ +static void test_n2() { + input[0] = 0xFF; // -2 + input[1] = 0xFE; // + g_check_parse_ok(pp, BKEND, input, 2); +} + +static void test_n1() { + input[0] = 0xFF; // -1 + input[1] = 0xFF; // + g_check_parse_ok(pp, BKEND, input, 2); +} + +static void test_zero() { + input[0] = 0x00; // 0 + input[1] = 0x00; // 0 + g_check_parse_ok(pp, BKEND, input, 2); +} + +static void test_p1() { + input[0] = 0x00; // +1 + input[1] = 0x01; + g_check_parse_ok(pp, BKEND, input, 2); +} + +/* Failing Tests: -3,+2,+1+1 */ +static void test_n3() { + input[0] = 0xFF; // -3 + input[1] = 0xFD; + g_check_parse_failed(pp, BKEND, input, 2); +} + +static void test_p2() { + input[0] = 0x00; // +2 + input[1] = 0x02; + g_check_parse_failed(pp, BKEND, input, 2); +} + +static void test_2val() { + input[0] = 0x00; // +1 + input[1] = 0x01; + input[0] = 0x00; // +1 + input[1] = 0x01; + g_check_parse_failed(pp, BKEND, input, 4); +} + + +void register_bug_tests() { + g_test_add_func("/pass/neg2",test_n2); + g_test_add_func("/pass/neg1",test_n1); + g_test_add_func("/pass/zero",test_zero); + g_test_add_func("/pass/pos1",test_p1); + + g_test_add_func("/fail/neg3",test_n3); + g_test_add_func("/fail/pos2",test_p2); + g_test_add_func("/fail/2val",test_2val); +} + +int main(int argc, char *argv[]) { + init_parser(); + g_test_init(&argc,&argv,NULL); + register_bug_tests(); + return g_test_run(); +} diff --git a/bug3.c b/bug3.c new file mode 100755 index 0000000..9bffa6e --- /dev/null +++ b/bug3.c @@ -0,0 +1,92 @@ +/* + * bug3 -- h_int16() issues using little endian + * + * This parser passes all tests if the backend is PB_MIN but fails if + * the backend is PB_LALR. + * + * See defines for BKEND below. + */ +#include <hammer/hammer.h> +#include <hammer/glue.h> +#include <hammer/test_suite.h> +#include <glib.h> + +#define BKEND PB_LALR +//#define BKEND PB_MIN + +const HParser *pp; /* the parser */ + +uint8_t input[1024]; /* the input is a buff of bytes */ + +/* The Parser */ +void init_parser() { + /* parse a single int16 between the values of -2 and 1 */ + H_RULE(littleint16, h_with_endianness(BYTE_LITTLE_ENDIAN,h_int16())); + H_RULE(int16range, h_int_range(littleint16,-2,1)); + pp = h_sequence(int16range,h_end_p(),NULL); +} + +/* Passing Tests: -2,-1,0,+1 */ +static void test_n2() { + input[0] = 0xFE; // + input[1] = 0xFF; // -2 + g_check_parse_ok(pp, BKEND, input, 2); +} + +static void test_n1() { + input[0] = 0xFF; // + input[1] = 0xFF; // -1 + g_check_parse_ok(pp, BKEND, input, 2); +} + +static void test_zero() { + input[0] = 0x00; // 0 + input[1] = 0x00; // 0 + g_check_parse_ok(pp, BKEND, input, 2); +} + +static void test_p1() { + input[0] = 0x01; + input[1] = 0x00; // +1 + g_check_parse_ok(pp, BKEND, input, 2); +} + +/* Failing Tests: -3,+2,+1+1 */ +static void test_n3() { + input[0] = 0xFD; + input[1] = 0xFF; // -3 + g_check_parse_failed(pp, BKEND, input, 2); +} + +static void test_p2() { + input[0] = 0x02; + input[1] = 0x00; // +2 + g_check_parse_failed(pp, BKEND, input, 2); +} + +static void test_2val() { + input[0] = 0x01; + input[1] = 0x00; // +1 + input[0] = 0x01; + input[1] = 0x00; // +1 + g_check_parse_failed(pp, BKEND, input, 4); +} + + +void register_bug_tests() { + g_test_add_func("/pass/neg2",test_n2); + g_test_add_func("/pass/neg1",test_n1); + g_test_add_func("/pass/zero",test_zero); + g_test_add_func("/pass/pos1",test_p1); + + g_test_add_func("/fail/neg3",test_n3); + g_test_add_func("/fail/pos2",test_p2); + g_test_add_func("/fail/2val",test_2val); +} + +int main(int argc, char *argv[]) { + init_parser(); + g_test_init(&argc,&argv,NULL); + register_bug_tests(); + return g_test_run(); +} diff --git a/span.c b/span.c new file mode 100755 index 0000000..78a6592 --- /dev/null +++ b/span.c @@ -0,0 +1,53 @@ +/* + * Integer-range parser combinator exhaustive test + * + */ +#include <hammer/hammer.h> +#include <hammer/glue.h> +#include <string.h> +#include <stdlib.h> +#define BKEND PB_LALR +//#define BKEND PB_MIN + +HParser *int16_ranger(int16_t left, int16_t right) { + H_RULE(littleint16, h_with_endianness(BYTE_BIG_ENDIAN,h_int16())); + H_RULE(int16range, h_int_range(littleint16, left, right)); + return h_sequence(int16range,h_end_p(),NULL); +} + + +int run_parse(HParser *parser, HParserBackend backend, + uint8_t *input, size_t input_len) { + assert(h_compile(parser, backend, NULL) == 0); + return h_parse(parser, input, input_len); +} + + +int main(int argc, char *argv[]) { + assert(argc == 3); + int16_t low = atoi(argv[1]); + int16_t high = atoi(argv[2]); + printf("%d %d\n", low, high); + HParser *pp = int16_ranger(low, high); + int16_t k; + + for (k=INT16_MIN; k<INT16_MAX; k++) { + uint8_t tmp1[2], tmp2[2]; +// if (0) { +// memcpy(tmp1, &k, 2); +// tmp2[0] = tmp1[1]; +// tmp2[1] = tmp1[0]; +// } else +// { + memcpy(tmp2, &k, 2); +// } + + if(!run_parse(pp, BKEND, tmp2, 2)) { + printf("parse failed %d\n", k); + } else { + printf("parse success 0x%hx\n", k); + } + } + + return 0; +} -- GitLab