Skip to content

Commit 541a56e

Browse files
authored
Fix segfault when required is not an array (#518)
See: sourcemeta/jsonschema#500 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent a323f9b commit 541a56e

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/compiler/default_compiler_draft4.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ auto compiler_draft4_validation_required(const Context &context,
541541
const DynamicContext &dynamic_context,
542542
const Instructions &current)
543543
-> Instructions {
544-
assert(schema_context.schema.at(dynamic_context.keyword).is_array());
544+
if (!schema_context.schema.at(dynamic_context.keyword).is_array()) {
545+
return {};
546+
}
545547

546548
if (schema_context.schema.defines("type") &&
547549
schema_context.schema.at("type").is_string() &&

test/evaluator/evaluator_draft4_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,29 @@ TEST(Evaluator_draft4, required_5_exhaustive) {
402402
"The value was expected to be of type object");
403403
}
404404

405+
TEST(Evaluator_draft4, required_6) {
406+
// This is purposely invalid
407+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
408+
"$schema": "http://json-schema.org/draft-04/schema#",
409+
"type": "object",
410+
"properties": {
411+
"foo": {
412+
"required": "bar"
413+
}
414+
}
415+
})JSON")};
416+
417+
const sourcemeta::core::JSON instance{sourcemeta::core::parse_json(R"JSON({
418+
"foo": {}
419+
})JSON")};
420+
421+
EVALUATE_WITH_TRACE_FAST_SUCCESS(schema, instance, 1);
422+
EVALUATE_TRACE_PRE(0, AssertionTypeStrict, "/type", "#/type", "");
423+
EVALUATE_TRACE_POST_SUCCESS(0, AssertionTypeStrict, "/type", "#/type", "");
424+
EVALUATE_TRACE_POST_DESCRIBE(instance, 0,
425+
"The value was expected to be of type object");
426+
}
427+
405428
TEST(Evaluator_draft4, allOf_1) {
406429
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
407430
"$schema": "http://json-schema.org/draft-04/schema#",

0 commit comments

Comments
 (0)