diff --git a/src/ucl_parser.c b/src/ucl_parser.c index 90e50f13..7feab365 100644 --- a/src/ucl_parser.c +++ b/src/ucl_parser.c @@ -2292,6 +2292,12 @@ ucl_parse_macro_value(struct ucl_parser *parser, p = chunk->pos; + if (p >= chunk->end) { + ucl_set_err(parser, UCL_ESYNTAX, "macro value is missing", + &parser->err); + return false; + } + switch (*p) { case '"': /* We have macro value encoded in quotes */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4d91bfa4..76ee100a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,6 +27,7 @@ IF(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") add_ucl_test(test_basic test_basic.c basic.test) add_ucl_test(test_speed test_speed.c speed.test) add_ucl_test(test_schema test_schema.c schema.test) + add_ucl_test(test_macro_eof test_macro_eof.c macro_eof.test) ENDIF() # Portable tests diff --git a/tests/Makefile.am b/tests/Makefile.am index d3442275..21798b6e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,6 +5,7 @@ TESTS = basic.test \ generate.test \ schema.test \ msgpack.test \ + macro_eof.test \ speed.test TESTS_ENVIRONMENT = $(SH) \ @@ -41,5 +42,9 @@ test_msgpack_SOURCES = test_msgpack.c test_msgpack_LDADD = $(common_test_ldadd) test_msgpack_CFLAGS = $(common_test_cflags) +test_macro_eof_SOURCES = test_macro_eof.c +test_macro_eof_LDADD = $(common_test_ldadd) +test_macro_eof_CFLAGS = $(common_test_cflags) + check_PROGRAMS = test_basic test_speed test_generate test_schema \ - test_streamline test_msgpack + test_streamline test_msgpack test_macro_eof diff --git a/tests/macro_eof.test b/tests/macro_eof.test new file mode 100755 index 00000000..832321b8 --- /dev/null +++ b/tests/macro_eof.test @@ -0,0 +1,3 @@ +#!/bin/sh + +${TEST_BINARY_DIR}/test_macro_eof diff --git a/tests/test_macro_eof.c b/tests/test_macro_eof.c new file mode 100644 index 00000000..f5e547e0 --- /dev/null +++ b/tests/test_macro_eof.c @@ -0,0 +1,81 @@ +/* + * Regression test: a macro whose value is missing at the very end of the + * input must be rejected without reading past the end of the chunk. + * + * Each input is placed so that its last byte is the last byte of a mapped + * page and the following page is PROT_NONE, mirroring the mmap() layout + * used by ucl_parser_add_fd(). Any overread faults even without sanitizers. + */ + +#include +#include +#include +#include +#include + +#include "ucl.h" + +struct macro_eof_case { + const char *input; + bool expect_ok; +}; + +static const struct macro_eof_case cases[] = { + {".priority(a=1)", false}, + {".priority(a=1) ", false}, + {".priority(a=1)\n", false}, + {".priority(a=1)#c", false}, + {".priority(a=1)/* c */", false}, + {"a = 1; .priority(a=1)", false}, + {".priority(a=1) 2", true}, + {".priority(a=1) \"2\"", true}, + {".priority 1", true}, +}; + +int +main(void) +{ + size_t pg = (size_t) sysconf(_SC_PAGESIZE); + unsigned char *map; + unsigned int i, failures = 0; + + map = mmap(NULL, pg * 2, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); + if (map == MAP_FAILED) { + perror("mmap"); + return EXIT_FAILURE; + } + if (mprotect(map + pg, pg, PROT_NONE) != 0) { + perror("mprotect"); + return EXIT_FAILURE; + } + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + size_t len = strlen(cases[i].input); + unsigned char *buf = map + pg - len; + struct ucl_parser *parser; + bool ok; + + memset(map, 0, pg); + memcpy(buf, cases[i].input, len); + + parser = ucl_parser_new(0); + if (parser == NULL) { + fprintf(stderr, "cannot create parser\n"); + return EXIT_FAILURE; + } + ok = ucl_parser_add_chunk(parser, buf, len); + if (ok != cases[i].expect_ok) { + fprintf(stderr, "case %u (%s): expected %s, got %s%s\n", i, + cases[i].input, cases[i].expect_ok ? "success" : "error", + ok ? "success" : "error: ", + ok ? "" : ucl_parser_get_error(parser)); + failures++; + } + ucl_parser_free(parser); + } + + munmap(map, pg * 2); + + return failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +}