Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ucl_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ TESTS = basic.test \
generate.test \
schema.test \
msgpack.test \
macro_eof.test \
speed.test

TESTS_ENVIRONMENT = $(SH) \
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions tests/macro_eof.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

${TEST_BINARY_DIR}/test_macro_eof
81 changes: 81 additions & 0 deletions tests/test_macro_eof.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

#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;
}
Loading