// Copyright 2018 David Robillard // SPDX-License-Identifier: ISC #undef NDEBUG #include "serd/serd.h" #include #include static const size_t min_stack_size = 4U * sizeof(size_t) + 238U; static const size_t max_stack_size = 2048U; static SerdStatus test_size(SerdWorld* const world, const char* const str, const SerdSyntax syntax, const SerdReaderFlags flags, const size_t stack_size) { SerdLimits limits = serd_world_limits(world); limits.reader_stack_size = stack_size; serd_world_set_limits(world, limits); SerdSink* sink = serd_sink_new(NULL, NULL, NULL, NULL); SerdEnv* const env = serd_env_new(NULL, serd_empty_string()); SerdReader* const reader = serd_reader_new(world, syntax, flags, env, sink); if (!reader) { return SERD_BAD_STACK; } SerdNode* string_name = serd_node_new(NULL, serd_a_string("string")); const char* position = str; SerdInputStream in = serd_open_input_string(&position); serd_reader_start(reader, &in, string_name, 1); const SerdStatus st = serd_reader_read_document(reader); serd_close_input(&in); serd_node_free(NULL, string_name); serd_reader_free(reader); serd_env_free(env); serd_sink_free(sink); return st; } static void test_all_sizes(SerdWorld* const world, const char* const str, const SerdSyntax syntax, const SerdReaderFlags flags) { // Ensure reading with the maximum stack size succeeds SerdStatus st = test_size(world, str, syntax, flags, max_stack_size); assert(!st); // Test with an increasingly smaller stack for (size_t size = max_stack_size; size > min_stack_size; --size) { if ((st = test_size(world, str, syntax, flags, size))) { assert(st == SERD_BAD_STACK); } } assert(st == SERD_BAD_STACK); } static void test_ntriples_overflow(void) { static const char* const test_strings[] = { " .", " \"literal\" .", " _:blank .", " \"\"@en .", NULL, }; SerdWorld* const world = serd_world_new(NULL); for (const char* const* t = test_strings; *t; ++t) { test_all_sizes(world, *t, SERD_NTRIPLES, 0U); } serd_world_free(world); } static void test_turtle_overflow(void) { static const char* const test_strings[] = { " .", " " " .", " 1234 .", " (1 2 3 4) .", " (((((((42))))))) .", " \"literal\" .", " _:blank .", " true .", " \"\"@en .", "?subject ?predicate ?object .", "(((((((((42))))))))) .", "@prefix eg: .", "@base .", "@prefix eg: . \neg:s eg:p eg:o .\n", "@prefix ug.dot: . \nug.dot:s ug.dot:p ug.dot:o .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) " " " " " .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) " " "\"typed\"^^ .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" " " "\"typed\"^^eg:Datatype .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" " eg:foo .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix prefix: .\n" "prefix:subject prefix:predicate prefix:object .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" "eg:s eg:p [ eg:p [ eg:p [ eg:p [ eg:p []]]]] .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" "eg:s eg:p ( 1 2 3 ( 4 5 6 ( 7 8 9 ) ) ) .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" " eg:%99 .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix øøøøøøøøø: .\n" " øøøøøøøøø:p " "øøøøøøøøø:o .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) " " " " " .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) " " "\"typed\"^^ .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" " " "\"typed\"^^eg:Datatype .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" " eg:foo .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix prefix: .\n" "prefix:subject prefix:predicate prefix:object .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix prefix: .\n" " " "prefix:predicate prefix:object ; prefix:p prefix:o .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" "eg:s eg:p [ eg:p [ eg:p [ eg:p [ eg:p eg:o ] ] ] ] .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" "eg:s eg:p ( 1 2 3 ( 4 5 6 ( 7 8 9 ) ) ) .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix eg: .\n" " eg:%99 .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@prefix øøøøøøøøø: .\n" " øøøøøøøøø:p " "øøøøøøøøø:o .\n", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@base .\n" " .", // NOLINTNEXTLINE(bugprone-suspicious-missing-comma) "@base .\n" " .", NULL, }; SerdWorld* const world = serd_world_new(NULL); for (const char* const* t = test_strings; *t; ++t) { test_all_sizes(world, *t, SERD_TURTLE, SERD_READ_VARIABLES); } serd_world_free(world); } int main(void) { test_ntriples_overflow(); test_turtle_overflow(); return 0; }