You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pe...@apache.org on 2026/08/10 04:38:51 UTC
(celix) 21/35: Enhance error handling in JSON schema processing
This is an automated email from the ASF dual-hosted git repository.
PengZheng pushed a commit to branch feature/json-schema
in repository https://gitbox.apache.org/repos/asf/celix.git
commit af09a3d799fbc4560077bea48d82c18c87ec3886
Author: PengZheng <ho...@gmail.com>
AuthorDate: Sat Aug 8 17:20:16 2026 +0800
Enhance error handling in JSON schema processing
---
libs/error_injector/jansson/CMakeLists.txt | 1 +
libs/error_injector/jansson/include/jansson_ei.h | 1 +
libs/error_injector/jansson/src/jansson_ei.cc | 7 +
libs/jansson_ext/gtest/CMakeLists.txt | 21 +
.../gtest/src/ErrorInjectionTestSuite.cc | 316 +++++++++++
.../gtest/src/SchemaErrorInjectionTestSuite.cc | 611 +++++++++++++++++++++
libs/jansson_ext/src/celix_jansson_schema.c | 179 +++++-
libs/jansson_ext/src/celix_jansson_uri.c | 5 +-
8 files changed, 1127 insertions(+), 14 deletions(-)
diff --git a/libs/error_injector/jansson/CMakeLists.txt b/libs/error_injector/jansson/CMakeLists.txt
index 4596350b0..fec7164e5 100644
--- a/libs/error_injector/jansson/CMakeLists.txt
+++ b/libs/error_injector/jansson/CMakeLists.txt
@@ -41,5 +41,6 @@ target_link_options(jansson_ei INTERFACE
LINKER:--wrap,json_pack_ex
LINKER:--wrap,json_null
LINKER:--wrap,json_loads
+ LINKER:--wrap,json_deep_copy
)
add_library(Celix::jansson_ei ALIAS jansson_ei)
diff --git a/libs/error_injector/jansson/include/jansson_ei.h b/libs/error_injector/jansson/include/jansson_ei.h
index feb9f59ef..f8a8456b8 100644
--- a/libs/error_injector/jansson/include/jansson_ei.h
+++ b/libs/error_injector/jansson/include/jansson_ei.h
@@ -40,6 +40,7 @@ CELIX_EI_DECLARE(json_dumpf, int);
CELIX_EI_DECLARE(json_pack_ex, json_t*);
CELIX_EI_DECLARE(json_null, json_t*);
CELIX_EI_DECLARE(json_loads, json_t*);
+CELIX_EI_DECLARE(json_deep_copy, json_t*);
#ifdef __cplusplus
}
diff --git a/libs/error_injector/jansson/src/jansson_ei.cc b/libs/error_injector/jansson/src/jansson_ei.cc
index 0328d40e9..6daeb35bf 100644
--- a/libs/error_injector/jansson/src/jansson_ei.cc
+++ b/libs/error_injector/jansson/src/jansson_ei.cc
@@ -138,4 +138,11 @@ json_t* __wrap_json_loads(const char* input, size_t flags, json_error_t* error)
return __real_json_loads(input, flags, error);
}
+json_t* __real_json_deep_copy(const json_t* value);
+CELIX_EI_DEFINE(json_deep_copy, json_t*)
+json_t* __wrap_json_deep_copy(const json_t* value) {
+ CELIX_EI_IMPL(json_deep_copy);
+ return __real_json_deep_copy(value);
+}
+
}
\ No newline at end of file
diff --git a/libs/jansson_ext/gtest/CMakeLists.txt b/libs/jansson_ext/gtest/CMakeLists.txt
index c4880a675..87bc4aa6c 100644
--- a/libs/jansson_ext/gtest/CMakeLists.txt
+++ b/libs/jansson_ext/gtest/CMakeLists.txt
@@ -48,3 +48,24 @@ foreach(T ${TEST_SOURCES})
add_test(NAME ${NAME} COMMAND ${NAME})
setup_target_for_coverage(${NAME} SCAN_DIR ..)
endforeach()
+
+if (EI_TESTS)
+ # Note: the error-injection tests are separated from the regular tests because
+ # they link the error-injector wrappers (--wrap) and set injection expectations
+ # that must not leak into the normal test run.
+ add_executable(test_jansson_ext_with_ei
+ src/ErrorInjectionTestSuite.cc
+ src/SchemaErrorInjectionTestSuite.cc
+ )
+ target_link_libraries(test_jansson_ext_with_ei PRIVATE
+ jansson_ext_cut
+ Celix::malloc_ei
+ Celix::string_ei
+ Celix::jansson_ei
+ Celix::string_hash_map_ei
+ GTest::gtest GTest::gtest_main
+ )
+ target_include_directories(test_jansson_ext_with_ei PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
+ add_test(NAME test_jansson_ext_with_ei COMMAND test_jansson_ext_with_ei)
+ setup_target_for_coverage(test_jansson_ext_with_ei SCAN_DIR ..)
+endif ()
diff --git a/libs/jansson_ext/gtest/src/ErrorInjectionTestSuite.cc b/libs/jansson_ext/gtest/src/ErrorInjectionTestSuite.cc
new file mode 100644
index 000000000..61277e245
--- /dev/null
+++ b/libs/jansson_ext/gtest/src/ErrorInjectionTestSuite.cc
@@ -0,0 +1,316 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include "celix_cleanup.h"
+#include "celix_jansson_pointer.h"
+#include "celix_jansson_uri.h"
+#include "celix_json_patch.h"
+#include "celix_util.h"
+#include "jansson_ei.h"
+#include "malloc_ei.h"
+#include "string_ei.h"
+
+CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(celix_json_pointer_t, celix_json_pointer_destroy)
+
+/**
+ * Error-injection tests for the OOM (out-of-memory) handling paths of
+ * celix_util, celix_jansson_pointer, celix_jansson_uri and celix_json_patch.
+ *
+ * Every allocation-failure branch (if (!ptr) return ...) is exercised by
+ * injecting a NULL return for the matching allocator with an exact caller
+ * match. The injected caller address is the function that (directly or, via
+ * `level`, indirectly) calls the wrapped allocator.
+ */
+class JanssonExtErrorInjectionTestSuite : public ::testing::Test {
+public:
+ ~JanssonExtErrorInjectionTestSuite() noexcept override {
+ celix_ei_expect_malloc(nullptr, 0, nullptr);
+ celix_ei_expect_realloc(nullptr, 0, nullptr);
+ celix_ei_expect_calloc(nullptr, 0, nullptr);
+ celix_ei_expect_strdup(nullptr, 0, nullptr);
+ celix_ei_expect_json_object(nullptr, 0, nullptr);
+ celix_ei_expect_json_deep_copy(nullptr, 0, nullptr);
+ }
+};
+
+/* ── celix_util.c ─────────────────────────────────────────────────────── */
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UtilStrbufAppendReallocFail) {
+ //Given a fresh string buffer and realloc is injected to fail in strbuf_append
+ celix_jansson_strbuf_t sb;
+ celix_jansson_strbuf_init(&sb);
+ celix_ei_expect_realloc((void*)celix_jansson_strbuf_append, 0, nullptr);
+ //Then appending should fail
+ EXPECT_EQ(-1, celix_jansson_strbuf_append(&sb, "hello", 5));
+ celix_jansson_strbuf_free(&sb);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UtilStrbufVappendfReallocFail) {
+ //Given a fresh string buffer and realloc is injected to fail in vappendf
+ celix_jansson_strbuf_t sb;
+ celix_jansson_strbuf_init(&sb);
+ celix_ei_expect_realloc((void*)celix_jansson_strbuf_vappendf, 0, nullptr);
+ //Then printf-appending should fail
+ EXPECT_EQ(-1, celix_jansson_strbuf_appendf(&sb, "%s", "hello"));
+ celix_jansson_strbuf_free(&sb);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UtilVecPushReallocFail) {
+ //Given a fresh vec and realloc is injected to fail in vec_push
+ celix_jansson_vec_t v;
+ celix_jansson_vec_init(&v);
+ celix_ei_expect_realloc((void*)celix_jansson_vec_push, 0, nullptr);
+ //Then pushing an item should fail
+ EXPECT_EQ(-1, celix_jansson_vec_push(&v, (void*)0x1));
+ celix_jansson_vec_free(&v);
+}
+
+/* ── celix_jansson_pointer.c ──────────────────────────────────────────── */
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerEnsureCapReallocFail) {
+ //Given realloc is injected to fail in ensure_cap (static, called from push)
+ celix_ei_expect_realloc((void*)celix_json_pointer_push, 1, nullptr);
+ celix_json_pointer_t p{};
+ //Then pushing a token should fail
+ EXPECT_EQ(-1, celix_json_pointer_push(&p, "a"));
+ celix_json_pointer_clear(&p);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerCreateCallocFail) {
+ //Given calloc is injected to fail in create
+ celix_ei_expect_calloc((void*)celix_json_pointer_create, 0, nullptr);
+ //Then creating a pointer should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_create("/a"));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerInitDecodeMallocFail) {
+ //Given malloc is injected to fail for the token decode in init
+ celix_ei_expect_malloc((void*)celix_json_pointer_init, 0, nullptr);
+ celix_json_pointer_t p{};
+ //Then initializing a pointer should fail
+ EXPECT_EQ(-1, celix_json_pointer_init(&p, "/a"));
+ celix_json_pointer_clear(&p);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerInitPushStrdupFail) {
+ //Given strdup is injected to fail in push while init pushes the first token
+ celix_ei_expect_strdup((void*)celix_json_pointer_push, 0, nullptr);
+ celix_json_pointer_t p{};
+ //Then initializing a pointer should fail
+ EXPECT_EQ(-1, celix_json_pointer_init(&p, "/a/b"));
+ celix_json_pointer_clear(&p);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerInitTrailingSlashPushFail) {
+ //Given strdup is injected to fail only on the second push (the empty trailing token)
+ celix_ei_expect_strdup((void*)celix_json_pointer_push, 0, nullptr, 2);
+ celix_json_pointer_t p{};
+ //Then initializing "/a/" should fail
+ EXPECT_EQ(-1, celix_json_pointer_init(&p, "/a/"));
+ celix_json_pointer_clear(&p);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerCopyCallocFail) {
+ //Given a pointer with one token
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a");
+ ASSERT_NE(nullptr, src);
+ //And calloc is injected to fail in copy
+ celix_ei_expect_calloc((void*)celix_json_pointer_copy, 0, nullptr);
+ //Then copying should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_copy(src));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerCopyEnsureCapReallocFail) {
+ //Given a pointer with one token and realloc is injected to fail in ensure_cap (called from copy)
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a");
+ ASSERT_NE(nullptr, src);
+ celix_ei_expect_realloc((void*)celix_json_pointer_copy, 1, nullptr);
+ //Then copying should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_copy(src));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerCopyStrdupFail) {
+ //Given a pointer with one token and strdup is injected to fail in copy
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a");
+ ASSERT_NE(nullptr, src);
+ celix_ei_expect_strdup((void*)celix_json_pointer_copy, 0, nullptr);
+ //Then copying should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_copy(src));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerPushStrdupFail) {
+ //Given strdup is injected to fail in push
+ celix_ei_expect_strdup((void*)celix_json_pointer_push, 0, nullptr);
+ celix_json_pointer_t p{};
+ //Then pushing a token should fail
+ EXPECT_EQ(-1, celix_json_pointer_push(&p, "abc"));
+ celix_json_pointer_clear(&p);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerToStringMallocFail) {
+ //Given a pointer with one token and malloc is injected to fail in to_string
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a");
+ ASSERT_NE(nullptr, src);
+ celix_ei_expect_malloc((void*)celix_json_pointer_to_string, 0, nullptr);
+ //Then serializing should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_to_string(src));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerEscapeMallocFail) {
+ //Given malloc is injected to fail in escape
+ celix_ei_expect_malloc((void*)celix_json_pointer_escape, 0, nullptr);
+ //Then escaping should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_escape("a/b"));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerUnescapeMallocFail) {
+ //Given malloc is injected to fail in unescape
+ celix_ei_expect_malloc((void*)celix_json_pointer_unescape, 0, nullptr);
+ //Then unescaping should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_unescape("a~1b"));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerParentCallocFail) {
+ //Given a two-token pointer and calloc is injected to fail in parent (out == nullptr, self-allocated)
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a/b");
+ ASSERT_NE(nullptr, src);
+ celix_ei_expect_calloc((void*)celix_json_pointer_parent, 0, nullptr);
+ //Then computing the parent should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_parent(src, nullptr));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerParentEnsureCapFail) {
+ //Given a two-token pointer and realloc is injected to fail in ensure_cap (called from parent)
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a/b");
+ ASSERT_NE(nullptr, src);
+ celix_ei_expect_realloc((void*)celix_json_pointer_parent, 1, nullptr);
+ //Then computing the parent should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_parent(src, nullptr));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerParentStrdupFail) {
+ //Given a two-token pointer and strdup is injected to fail in parent
+ celix_autoptr(celix_json_pointer_t) src = celix_json_pointer_create("/a/b");
+ ASSERT_NE(nullptr, src);
+ celix_ei_expect_strdup((void*)celix_json_pointer_parent, 0, nullptr);
+ //Then computing the parent should fail
+ EXPECT_EQ(nullptr, celix_json_pointer_parent(src, nullptr));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PointerConcatPushFail) {
+ //Given strdup is injected to fail in push (used by concat)
+ celix_autoptr(celix_json_pointer_t) dst = celix_json_pointer_create("");
+ celix_autoptr(celix_json_pointer_t) suffix = celix_json_pointer_create("/x");
+ ASSERT_NE(nullptr, dst);
+ ASSERT_NE(nullptr, suffix);
+ celix_ei_expect_strdup((void*)celix_json_pointer_push, 0, nullptr);
+ //Then concatenating should fail
+ EXPECT_EQ(-1, celix_json_pointer_concat(dst, suffix));
+}
+
+/* ── celix_jansson_uri.c ──────────────────────────────────────────────── */
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UriUpdateLocationMallocFail) {
+ //Given malloc is injected to fail for the location in update
+ celix_jansson_uri_t u{};
+ celix_ei_expect_malloc((void*)celix_jansson_uri_update, 0, nullptr);
+ //Then updating the URI should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_uri_update(&u, "http://example.com"));
+ celix_jansson_uri_clear(&u);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UriUpdatePercentDecodeReallocFail) {
+ //Given realloc is injected to fail in strbuf_append (used by percent_decode).
+ //The single-char fragment makes percent_decode loop exactly once, so the
+ //single injected realloc failure is not recovered by a later iteration.
+ celix_jansson_uri_t u{};
+ celix_ei_expect_realloc((void*)celix_jansson_strbuf_append, 0, nullptr);
+ //Then updating a URI with a fragment should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_uri_update(&u, "http://example.com#a"));
+ celix_jansson_uri_clear(&u);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UriUpdateInvalidPointerFragment) {
+ //Given a fragment that is not a valid JSON Pointer ("~" without escape character)
+ celix_jansson_uri_t u{};
+ //Then updating should fail with NOMEM (pointer init failure is mapped to NOMEM)
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_uri_update(&u, "http://example.com#/a~"));
+ celix_jansson_uri_clear(&u);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UriAppendCopyPushFail) {
+ //Given a URI with one pointer token and strdup is injected to fail in push while copying tokens
+ celix_jansson_uri_t u{};
+ ASSERT_EQ(0, celix_jansson_uri_init(&u, "#/a"));
+ celix_jansson_uri_t out{};
+ celix_ei_expect_strdup((void*)celix_json_pointer_push, 0, nullptr);
+ //Then appending a token should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_uri_append(&u, "x", &out));
+ celix_jansson_uri_clear(&u);
+ celix_jansson_uri_clear(&out);
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, UriAppendFinalPushFail) {
+ //Given a URI without pointer tokens and strdup is injected to fail in push for the final token
+ celix_jansson_uri_t u{};
+ ASSERT_EQ(0, celix_jansson_uri_init(&u, "http://example.com"));
+ celix_jansson_uri_t out{};
+ celix_ei_expect_strdup((void*)celix_json_pointer_push, 0, nullptr);
+ //Then appending a token should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_uri_append(&u, "x", &out));
+ celix_jansson_uri_clear(&u);
+ celix_jansson_uri_clear(&out);
+}
+
+/* ── celix_json_patch.c ───────────────────────────────────────────────── */
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PatchAddJsonObjectFail) {
+ //Given json_object is injected to fail in patch_add
+ json_auto_t* patch = json_array();
+ celix_ei_expect_json_object((void*)celix_json_patch_add, 0, nullptr);
+ //Then adding an operation should fail; the value is not consumed on failure
+ EXPECT_EQ(-1, celix_json_patch_add(patch, "/a", json_true()));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PatchReplaceJsonObjectFail) {
+ //Given json_object is injected to fail in patch_replace
+ json_auto_t* patch = json_array();
+ celix_ei_expect_json_object((void*)celix_json_patch_replace, 0, nullptr);
+ //Then replacing should fail; the value is not consumed on failure
+ EXPECT_EQ(-1, celix_json_patch_replace(patch, "/a", json_true()));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PatchRemoveJsonObjectFail) {
+ //Given json_object is injected to fail in patch_remove
+ json_auto_t* patch = json_array();
+ celix_ei_expect_json_object((void*)celix_json_patch_remove, 0, nullptr);
+ //Then removing should fail
+ EXPECT_EQ(-1, celix_json_patch_remove(patch, "/a"));
+}
+
+TEST_F(JanssonExtErrorInjectionTestSuite, PatchApplyDeepCopyFail) {
+ //Given json_deep_copy is injected to fail in patch_apply
+ json_auto_t* original = json_object();
+ json_auto_t* patch = json_array();
+ celix_ei_expect_json_deep_copy((void*)celix_json_patch_apply, 0, nullptr);
+ //Then applying the patch should fail
+ EXPECT_EQ(nullptr, celix_json_patch_apply(original, patch));
+}
diff --git a/libs/jansson_ext/gtest/src/SchemaErrorInjectionTestSuite.cc b/libs/jansson_ext/gtest/src/SchemaErrorInjectionTestSuite.cc
new file mode 100644
index 000000000..ad9de3f37
--- /dev/null
+++ b/libs/jansson_ext/gtest/src/SchemaErrorInjectionTestSuite.cc
@@ -0,0 +1,611 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include "celix_cleanup.h"
+#include "celix_jansson_schema.h"
+#include "celix_string_hash_map_ei.h"
+#include "jansson_ei.h"
+#include "malloc_ei.h"
+#include "string_ei.h"
+
+/* celix_schema.h is an internal C header without C++ linkage guards */
+extern "C" {
+#include "celix_schema.h"
+}
+
+CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(celix_jansson_schema_validator_t, celix_jansson_schema_validator_destroy)
+
+/**
+ * Error-injection tests for the OOM (out-of-memory) handling paths of
+ * celix_jansson_schema.
+ *
+ * The schema compilation/validation pipelines use several static helpers
+ * (schema_make_internal_depth, make_type_schema, obj_node_map_create), so the
+ * injected caller is matched with `level` = the number of frames between the
+ * wrapped allocator and the exported function that starts the pipeline:
+ * - make_type_schema callocs: level 2 (make_type_schema <- depth <- set_root_schema)
+ * - schema_make_internal_depth callocs: level 1 (depth <- set_root_schema)
+ * - obj_node_map_create createWithOptions: level 3 (obj_node_map_create <- make_type_schema <- depth <- set_root_schema)
+ * Ordinals only distinguish consecutive allocations from the same caller.
+ */
+class JanssonExtSchemaErrorInjectionTestSuite : public ::testing::Test {
+public:
+ ~JanssonExtSchemaErrorInjectionTestSuite() noexcept override {
+ celix_ei_expect_malloc(nullptr, 0, nullptr);
+ celix_ei_expect_realloc(nullptr, 0, nullptr);
+ celix_ei_expect_calloc(nullptr, 0, nullptr);
+ celix_ei_expect_strdup(nullptr, 0, nullptr);
+ celix_ei_expect_json_deep_copy(nullptr, 0, nullptr);
+ celix_ei_expect_celix_stringHashMap_createWithOptions(nullptr, 0, nullptr);
+ }
+
+protected:
+ static celix_jansson_schema_validator_t* makeValidator() {
+ return celix_jansson_schema_validator_create(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
+ }
+
+ static json_t* loadSchema(const char* text) {
+ json_error_t err{};
+ return json_loads(text, 0, &err);
+ }
+};
+
+static void countingErrorCb(const char*, json_t*, const char*, void* ud) {
+ int* count = static_cast<int*>(ud);
+ (*count)++;
+}
+
+/* ── path_push ────────────────────────────────────────────────────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaPathPushReallocFail) {
+ //Given realloc is injected to fail in path_push
+ celix_jansson_path_t p;
+ celix_jansson_path_init(&p);
+ celix_ei_expect_realloc((void*)celix_jansson_path_push, 0, nullptr);
+ //Then pushing a path token should fail
+ EXPECT_EQ(-1, celix_jansson_path_push(&p, "abc"));
+ celix_jansson_path_free(&p);
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaPathPushStrdupFail) {
+ //Given strdup is injected to fail in path_push
+ celix_jansson_path_t p;
+ celix_jansson_path_init(&p);
+ celix_ei_expect_strdup((void*)celix_jansson_path_push, 0, nullptr);
+ //Then pushing a path token should fail
+ EXPECT_EQ(-1, celix_jansson_path_push(&p, "abc"));
+ celix_jansson_path_free(&p);
+}
+
+/* ── Schema compilation (set_root_schema) ─────────────────────────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaBooleanNodeCallocFail) {
+ //Given a validator and calloc is injected to fail for the boolean node
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 1, nullptr);
+ //Then compiling a boolean schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, json_true(), nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaNodeCallocFail) {
+ //Given a validator and calloc is injected to fail for the type-schema node
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaTypedNodeCallocFail) {
+ //Given calloc is injected to fail for the per-type (typed) node
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 2);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaNoTypeSlotCallocFail) {
+ //Given calloc is injected to fail for the first 7-slot node (schema without "type")
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 2);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaTypeArrayNonString) {
+ //Given a "type" array containing a non-string entry (no injection needed)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":[1,\"string\"]}");
+ ASSERT_NE(nullptr, schema);
+ //Then compiling should skip the non-string entry and succeed
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_OK, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaPropertiesMapCreateFail) {
+ //Given stringHashMap creation is injected to fail for the properties map
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"properties\":{\"a\":{\"type\":\"string\"}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_celix_stringHashMap_createWithOptions((void*)celix_jansson_schema_set_root_schema, 3, nullptr);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaDependenciesMapCreateFail) {
+ //Given stringHashMap creation is injected to fail for the dependencies map
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"dependencies\":{\"a\":{\"type\":\"string\"}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_celix_stringHashMap_createWithOptions((void*)celix_jansson_schema_set_root_schema, 3, nullptr);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaRefUriDeriveReallocFail) {
+ //Given realloc is injected to fail in strbuf_append (used by percent_decode during $ref URI
+ //derivation). The single-char fragment "#x" makes percent_decode loop exactly once, so the
+ //single injected realloc failure is not recovered by a later iteration.
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema(
+ "{\"$id\":\"http://example.com/root.json\",\"properties\":{\"p\":{\"$ref\":\"#x\"}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_realloc((void*)celix_jansson_strbuf_append, 0, nullptr);
+ //Then the $ref URI derivation fails with NOMEM, which is now propagated
+ //through the properties subschema compilation (make_type_schema).
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaRefTargetPlaceholderCallocFail) {
+ //Given calloc is injected to fail for the unresolved $ref target placeholder
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"$ref\":\"#/nonexistent\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 1, nullptr);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaRefNodeCallocFail) {
+ //Given calloc is injected to fail for the $ref node (second allocation: placeholder succeeds)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"$ref\":\"#\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 1, nullptr, 2);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaGetOrCreateFileCallocFail) {
+ //Given calloc is injected to fail in get_or_create_file (first call, from root_insert)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_get_or_create_file, 0, nullptr);
+ //Then get_or_create_file returns NULL, root_insert reports NOMEM and
+ //set_root_schema now propagates it (with an error message)
+ char* errmsg = nullptr;
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, &errmsg));
+ EXPECT_NE(nullptr, errmsg);
+ free(errmsg);
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaGetOrCreateFileMapsFail) {
+ //Given stringHashMap creation is injected to fail in get_or_create_file (via static obj_node_map_create)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_celix_stringHashMap_createWithOptions((void*)celix_jansson_schema_root_get_or_create_file, 1, nullptr);
+ //Then get_or_create_file returns NULL, root_insert reports NOMEM and
+ //set_root_schema now propagates it.
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaSetRootSchemaDeepCopyFail) {
+ //Given json_deep_copy is injected to fail in set_root_schema
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ char* errmsg = nullptr;
+ celix_ei_expect_json_deep_copy((void*)celix_jansson_schema_set_root_schema, 0, nullptr);
+ //Then compiling the schema should fail with NOMEM and set an error message
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, &errmsg));
+ EXPECT_NE(nullptr, errmsg);
+ free(errmsg);
+}
+
+/* ── Validator creation ───────────────────────────────────────────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaValidatorCreateCallocFail) {
+ //Given calloc is injected to fail for the validator struct
+ celix_ei_expect_calloc((void*)celix_jansson_schema_validator_create, 0, nullptr);
+ //Then creating a validator should fail
+ EXPECT_EQ(nullptr, makeValidator());
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaValidatorCreateRootCallocFail) {
+ //Given calloc is injected to fail for the root struct (second calloc)
+ celix_ei_expect_calloc((void*)celix_jansson_schema_validator_create, 0, nullptr, 2);
+ //Then creating a validator should fail
+ EXPECT_EQ(nullptr, makeValidator());
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaValidatorCreateFilesMapFail) {
+ //Given stringHashMap creation is injected to fail for root->files
+ celix_ei_expect_celix_stringHashMap_createWithOptions((void*)celix_jansson_schema_validator_create, 0, nullptr);
+ //Then creating a validator should fail
+ EXPECT_EQ(nullptr, makeValidator());
+}
+
+/* ── Validation ───────────────────────────────────────────────────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaValidateSinkCallocFail) {
+ //Given a validator with a compiled schema
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ ASSERT_EQ(CELIX_JANSSON_SCHEMA_OK, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+ //And calloc is injected to fail for the user sink
+ celix_ei_expect_calloc((void*)celix_jansson_schema_validate, 0, nullptr);
+ //Then validating should fail
+ EXPECT_EQ(-1, celix_jansson_schema_validate(v, json_true(), nullptr, nullptr, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaValidateUriSinkCallocFail) {
+ //Given a validator with a compiled schema
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ ASSERT_EQ(CELIX_JANSSON_SCHEMA_OK, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+ //And calloc is injected to fail for the user sink
+ celix_ei_expect_calloc((void*)celix_jansson_schema_validate_uri, 0, nullptr);
+ //Then validating by URI should fail
+ EXPECT_EQ(-1, celix_jansson_schema_validate_uri(v, json_true(), "#", nullptr, nullptr, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaErrorListAddReallocFail) {
+ //Given a validator with an anyOf schema
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"anyOf\":[{\"type\":\"string\"}]}");
+ ASSERT_NE(nullptr, schema);
+ ASSERT_EQ(CELIX_JANSSON_SCHEMA_OK, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+ //And realloc is injected to fail in error_list_add (branch errors are silently dropped)
+ int errorCount = 0;
+ celix_ei_expect_realloc((void*)celix_jansson_error_list_add, 0, nullptr);
+ //When validating an integer against the anyOf(string) schema
+ json_auto_t* instance = json_integer(42);
+ int errs = celix_jansson_schema_validate(v, instance, countingErrorCb, &errorCount, nullptr);
+ //Then the branch errors are dropped and only the anyOf error is reported
+ EXPECT_EQ(1, errs);
+ EXPECT_EQ(1, errorCount);
+}
+
+/* ── OOM fixes: unchecked callocs in make_type_schema ─────────────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaTypeArrayTypedCallocFail) {
+ //Given calloc is injected to fail for the per-type node of the "type" array form
+ //(1st calloc = the type node, 2nd = the typed node)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":[\"string\"]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 2);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaRequiredArrayCallocFail) {
+ //Given calloc is injected to fail for the required array
+ //(1st = type node, 2nd = typed object node, 3rd = required array)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"required\":[\"a\"]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 3);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaPatternPropertiesCallocFail) {
+ //Given calloc is injected to fail for the patternProperties array
+ //(1st = type node, 2nd = typed object node, 3rd = patternProperties array)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"patternProperties\":{\"^a\":{}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 3);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaDefinitionsGetOrCreateFileCallocFail) {
+ //Given calloc is injected to fail in get_or_create_file, triggered by the
+ //definitions block (the first get_or_create_file call during compilation)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"definitions\":{\"a\":{}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_get_or_create_file, 0, nullptr);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+/* ── OOM fixes: DUPLICATE_URI tolerance and error propagation ─────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaRootIdEmptyDuplicateIgnored) {
+ //Given a schema with an empty top-level $id (registered during make with the
+ //"" key) and no injection
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"$id\":\"\",\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ //Then the second registration in set_root_schema returns DUPLICATE_URI,
+ //which must be tolerated
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_OK, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaPropertiesInvalidPatternPropagates) {
+ //Given a properties subschema whose pattern fails to compile (regcomp)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"properties\":{\"x\":{\"type\":\"string\",\"pattern\":\"***invalid\"}}}");
+ ASSERT_NE(nullptr, schema);
+ char* errmsg = nullptr;
+ //Then the compile error is propagated from the subschema
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_INVALID_PATTERN,
+ celix_jansson_schema_set_root_schema(v, schema, &errmsg));
+ EXPECT_NE(nullptr, errmsg);
+ free(errmsg);
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaOriginalSchemaDeepCopyFail) {
+ //Given json_deep_copy is injected to fail for the second copy (the original
+ //schema retention); the first copy succeeds (ordinal 1)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ char* errmsg = nullptr;
+ celix_ei_expect_json_deep_copy((void*)celix_jansson_schema_set_root_schema, 0, nullptr, 2);
+ //Then compiling the schema should fail with NOMEM
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, &errmsg));
+ EXPECT_NE(nullptr, errmsg);
+ free(errmsg);
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaRootIdInsertFileCallocFail) {
+ //Given calloc is injected to fail in get_or_create_file, reached through the
+ //top-level $id registration in schema_make_internal_depth
+ //(calloc <- get_or_create_file <- root_insert <- schema_make_internal_depth)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"$id\":\"http://example.com/x\",\"type\":\"string\"}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_insert, 1, nullptr);
+ //Then compiling the schema should fail with NOMEM (registration propagated)
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+/* ── OOM fixes: remaining unchecked callocs (dependencies/items/not/combos) ── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaDependenciesArrayCallocFail) {
+ //Given calloc is injected to fail for the dependencies array-form node
+ //(1st = type node, 2nd = typed object node, 3rd = the required-list node)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"dependencies\":{\"a\":[\"x\"]}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 3);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaDependenciesArrayNamesCallocFail) {
+ //Given calloc is injected to fail for the required-names array of a dependencies
+ //array-form node (1st = type node, 2nd = typed object node, 3rd = required-list
+ //node, 4th = names array)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"dependencies\":{\"a\":[\"x\"]}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 4);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaItemsTupleCallocFail) {
+ //Given calloc is injected to fail for the tuple-form items array
+ //(1st = type node, 2nd = typed array node, 3rd = items array)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"array\",\"items\":[{\"type\":\"string\"}]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 3);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaNotNodeCallocFail) {
+ //Given calloc is injected to fail for the "not" node
+ //(1st = type node, 2nd = typed object node, 3rd = not node; the subschema's
+ //own callocs do not match this caller)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"not\":{\"type\":\"string\"}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 3);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaComboNodeCallocFail) {
+ //Given calloc is injected to fail for the allOf node
+ //(1st = type node, 2nd = typed object node, 3rd = combo node)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"allOf\":[{\"type\":\"string\"}]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 3);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaComboItemsCallocFail) {
+ //Given calloc is injected to fail for the combo items array
+ //(1st = type node, 2nd = typed object node, 3rd = combo node, 4th = items array)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"allOf\":[{\"type\":\"string\"}]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 4);
+ //Then compiling the schema should fail with NOMEM instead of crashing
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaComboSubschemaFailWithNot) {
+ //Given a schema with a "not" node already pushed to the logic vec and an allOf
+ //whose subschema fails to compile (no injection needed)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"not\":{},\"allOf\":[1]}");
+ ASSERT_NE(nullptr, schema);
+ //Then the allOf compile error is propagated and the not node is released
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_INVALID_SCHEMA,
+ celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaComboNodeCallocFailWithNot) {
+ //Given calloc is injected to fail for the combo node while the logic vec
+ //already holds a "not" node (1st = type node, 2nd = typed object node,
+ //3rd = not node, 4th = combo node)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"not\":{},\"allOf\":[{\"type\":\"string\"}]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 4);
+ //Then compiling the schema should fail with NOMEM and release the not node
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaComboItemsCallocFailWithNot) {
+ //Given calloc is injected to fail for the combo items array while the logic
+ //vec already holds a "not" node (1st = type node, 2nd = typed object node,
+ //3rd = not node, 4th = combo node, 5th = items array)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"type\":\"object\",\"not\":{},\"allOf\":[{\"type\":\"string\"}]}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_set_root_schema, 2, nullptr, 5);
+ //Then compiling the schema should fail with NOMEM and release the not node
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+/* ── Remaining defensive branches and internal API NULL handling ───────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, VCombCollectingSinkAllocFail) {
+ //Given a validator with an allOf schema
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"allOf\":[{\"type\":\"integer\"}]}");
+ ASSERT_NE(nullptr, schema);
+ ASSERT_EQ(CELIX_JANSSON_SCHEMA_OK, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+ //And calloc is injected to fail for the master collecting sink
+ //(calloc <- coll_new <- v_comb <- v_type <- root_validate, so level 3)
+ json_auto_t* instance = json_integer(5);
+ int errorCount = 0;
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_validate, 3, nullptr, 1);
+ //Then validating should report the combination as failed (fail-closed)
+ EXPECT_EQ(1, celix_jansson_schema_validate(v, instance, countingErrorCb, &errorCount, nullptr));
+ EXPECT_EQ(1, errorCount);
+
+ //And calloc is injected to fail for the first branch collecting sink
+ errorCount = 0;
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_validate, 3, nullptr, 2);
+ //Then validating should report the combination as failed as well
+ EXPECT_EQ(1, celix_jansson_schema_validate(v, instance, countingErrorCb, &errorCount, nullptr));
+ EXPECT_EQ(1, errorCount);
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, RootDestroyNullIsNoOp) {
+ //Given a NULL root (no injection needed)
+ //Then destroying it should be a no-op
+ celix_jansson_schema_root_destroy(nullptr);
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, RootValidateNullArgs) {
+ //Given a NULL root
+ //Then validating should return -1
+ EXPECT_EQ(-1, celix_jansson_schema_root_validate(nullptr, "#", nullptr, nullptr));
+ //And with a zero-initialized root but NULL context it should also return -1
+ //(the ctx check fires before any root member is touched)
+ celix_jansson_schema_root_t r = {};
+ EXPECT_EQ(-1, celix_jansson_schema_root_validate(&r, "#", nullptr, nullptr));
+}
+
+/* ── OOM fixes: $id registration and document resolution ───────────────── */
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaNestedDefinitionsGetOrCreateFileCallocFail) {
+ //Given calloc is injected to fail in get_or_create_file, reached through the
+ //definitions block of a nested schema with its own $id
+ //(exercises the my_base cleanup when id_stored_in_out is false)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema =
+ loadSchema("{\"properties\":{\"p\":{\"$id\":\"http://example.com/sub\",\"definitions\":{\"a\":{}}}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_get_or_create_file, 0, nullptr);
+ //Then compiling the schema should fail with NOMEM (propagated through properties)
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
+TEST_F(JanssonExtSchemaErrorInjectionTestSuite, SchemaNestedIdInsertFileCallocFail) {
+ //Given calloc is injected to fail in get_or_create_file, reached through the
+ //$id registration of a nested schema (id_stored_in_out is false → my_base
+ //must be cleared on failure)
+ celix_autoptr(celix_jansson_schema_validator_t) v = makeValidator();
+ ASSERT_NE(nullptr, v);
+ json_auto_t* schema = loadSchema("{\"properties\":{\"p\":{\"$id\":\"http://example.com/sub\",\"type\":\"string\"}}}");
+ ASSERT_NE(nullptr, schema);
+ celix_ei_expect_calloc((void*)celix_jansson_schema_root_insert, 1, nullptr);
+ //Then compiling the schema should fail with NOMEM (registration propagated)
+ EXPECT_EQ(CELIX_JANSSON_SCHEMA_ERROR_NOMEM, celix_jansson_schema_set_root_schema(v, schema, nullptr));
+}
+
diff --git a/libs/jansson_ext/src/celix_jansson_schema.c b/libs/jansson_ext/src/celix_jansson_schema.c
index 74192ae27..c860bb1d2 100644
--- a/libs/jansson_ext/src/celix_jansson_schema.c
+++ b/libs/jansson_ext/src/celix_jansson_schema.c
@@ -305,8 +305,10 @@ int celix_jansson_type_index(json_t* value) {
return 5;
case JSON_REAL:
return 6;
+ //LCOV_EXCL_START: all legal jansson types are covered by the cases above
default:
return -1;
+ //LCOV_EXCL_STOP
}
}
@@ -964,9 +966,23 @@ static int v_comb(const celix_jansson_schema_node_t* n,
size_t old_patch = json_array_size(ctx->patch);
int count = 0;
collecting_sink_t* master = coll_new();
+ if (!master) {
+ /* Fail closed: return the same "combination failed, error emitted"
+ * convention as the other failing paths below (0 = passed) */
+ emit_error(ctx, p, "out of memory while validating combination");
+ return 1;
+ }
for (size_t i = 0; i < n->u.combination.len; i++) {
collecting_sink_t* cs = coll_new();
+ if (!cs) {
+ emit_error(ctx, p, "out of memory while validating combination");
+ /* Flush the errors collected by the earlier branches, then clean up */
+ coll_propagate(master, ctx->sink, NULL);
+ master->base.destroy(&master->base);
+ celix_json_patch_truncate(ctx->patch, old_patch);
+ return 1;
+ }
celix_jansson_validation_context_t cctx = *ctx;
cctx.sink = &cs->base;
@@ -1240,6 +1256,12 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
slot = 6;
if (slot >= 0) {
celix_jansson_schema_node_t* typed = (celix_jansson_schema_node_t*)calloc(1, sizeof(*n));
+ if (!typed) {
+ /* Earlier iterations of the array form may already have filled
+ * type_slots → unref(n) (d_type unrefs each slot) not free(n) */
+ celix_jansson_schema_unref(n);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
typed->root = root;
typed->refcount = 1;
switch (slot) {
@@ -1281,7 +1303,8 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
for (int slot = 0; slot < 7; slot++) {
celix_jansson_schema_node_t* typed = (celix_jansson_schema_node_t*)calloc(1, sizeof(*n));
if (!typed) {
- free(n);
+ /* Earlier iterations may already have filled type_slots */
+ celix_jansson_schema_unref(n);
return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
}
typed->root = root;
@@ -1410,6 +1433,11 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
if ((v = json_object_get(sch, "required"))) {
size_t rlen = json_array_size(v);
onode->u.object.required = (char**)calloc(rlen, sizeof(char*));
+ /* Check before setting required_len (d_object loops over required_len) */
+ if (rlen > 0 && !onode->u.object.required) {
+ celix_jansson_schema_unref(n);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
onode->u.object.required_len = rlen;
for (size_t i = 0; i < rlen; i++)
onode->u.object.required[i] = strdup(json_string_value(json_array_get(v, i)));
@@ -1423,15 +1451,26 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
const char* k;
json_t* pv;
json_object_foreach(v, k, pv) {
- celix_jansson_schema_node_t* ps;
- if (schema_make_internal(pv, root, base, &ps, depth + 1) == CELIX_JANSSON_SCHEMA_OK)
- celix_stringHashMap_put(onode->u.object.properties, k, ps);
+ celix_jansson_schema_node_t* ps = NULL;
+ int rc = schema_make_internal(pv, root, base, &ps, depth + 1);
+ if (rc != CELIX_JANSSON_SCHEMA_OK) {
+ /* Propagate the subschema compile error; d_object destroys the
+ * properties map and unrefs the subschemas already inserted */
+ celix_jansson_schema_unref(n);
+ return rc;
+ }
+ celix_stringHashMap_put(onode->u.object.properties, k, ps);
}
}
if ((v = json_object_get(sch, "patternProperties"))) {
size_t ppc = json_object_size(v);
onode->u.object.pattern_properties = (typeof(onode->u.object.pattern_properties))calloc(
ppc, sizeof(*onode->u.object.pattern_properties));
+ /* Check before setting pp_len (d_object loops over pp_len) */
+ if (ppc > 0 && !onode->u.object.pattern_properties) {
+ celix_jansson_schema_unref(n);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
onode->u.object.pp_len = ppc;
const char* pk;
json_t* psch;
@@ -1477,12 +1516,22 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
if (json_is_array(dv)) {
/* Array form → required list */
celix_jansson_schema_node_t* rn = (celix_jansson_schema_node_t*)calloc(1, sizeof(*rn));
+ if (!rn) {
+ celix_jansson_schema_unref(n);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
rn->vtable = &vt_required;
rn->kind = CELIX_JANSSON_SCHEMA_KIND_REQUIRED;
rn->root = root;
rn->refcount = 1;
size_t alen = json_array_size(dv);
rn->u.required.names = (char**)calloc(alen, sizeof(char*));
+ /* Check before setting len (d_required loops over len) */
+ if (alen > 0 && !rn->u.required.names) {
+ free(rn); /* rn owns nothing yet */
+ celix_jansson_schema_unref(n);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
rn->u.required.len = alen;
for (size_t ai = 0; ai < alen; ai++)
rn->u.required.names[ai] = strdup(json_string_value(json_array_get(dv, ai)));
@@ -1534,6 +1583,11 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
size_t ilen = json_array_size(v);
anode->u.array.items =
(celix_jansson_schema_node_t**)calloc(ilen, sizeof(celix_jansson_schema_node_t*));
+ /* Check before setting items_len (d_array loops over items_len) */
+ if (ilen > 0 && !anode->u.array.items) {
+ celix_jansson_schema_unref(n);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
anode->u.array.items_len = ilen;
for (size_t i = 0; i < ilen; i++) {
int rc = schema_make_internal(json_array_get(v, i), root, base,
@@ -1599,6 +1653,12 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
return rc;
}
celix_jansson_schema_node_t* nn = (celix_jansson_schema_node_t*)calloc(1, sizeof(*nn));
+ if (!nn) {
+ celix_jansson_schema_unref(sub); /* sub was compiled successfully above */
+ celix_jansson_schema_unref(n);
+ celix_jansson_vec_free(&logic);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
nn->vtable = &vt_not;
nn->kind = CELIX_JANSSON_SCHEMA_KIND_NOT;
nn->root = root;
@@ -1613,12 +1673,29 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
if ((v = json_object_get(sch, combos[ci]))) {
size_t clen = json_array_size(v);
celix_jansson_schema_node_t* cn = (celix_jansson_schema_node_t*)calloc(1, sizeof(*cn));
+ if (!cn) {
+ /* logic may already hold a not node or earlier combos */
+ for (size_t k = 0; k < logic.len; k++)
+ celix_jansson_schema_unref((celix_jansson_schema_node_t*)logic.items[k]);
+ celix_jansson_schema_unref(n);
+ celix_jansson_vec_free(&logic);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
cn->vtable = &vt_comb;
cn->kind = ckinds[ci];
cn->root = root;
cn->refcount = 1;
cn->u.combination.items =
(celix_jansson_schema_node_t**)calloc(clen, sizeof(celix_jansson_schema_node_t*));
+ /* Check before setting len (d_comb loops over len) */
+ if (clen > 0 && !cn->u.combination.items) {
+ free(cn); /* cn is not registered in logic yet */
+ for (size_t k = 0; k < logic.len; k++)
+ celix_jansson_schema_unref((celix_jansson_schema_node_t*)logic.items[k]);
+ celix_jansson_schema_unref(n);
+ celix_jansson_vec_free(&logic);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
cn->u.combination.len = clen;
for (size_t j = 0; j < clen; j++) {
int rc = schema_make_internal(json_array_get(v, j), root, base,
@@ -1629,6 +1706,10 @@ static int make_type_schema(json_t* sch, celix_jansson_schema_root_t* root,
celix_jansson_schema_unref(cn->u.combination.items[k]);
free(cn->u.combination.items);
free(cn);
+ /* vec_free only frees the array, not the elements: unref the
+ * not/earlier-combo nodes pushed into logic so far */
+ for (size_t k = 0; k < logic.len; k++)
+ celix_jansson_schema_unref((celix_jansson_schema_node_t*)logic.items[k]);
celix_jansson_schema_unref(n);
celix_jansson_vec_free(&logic);
return rc;
@@ -1688,8 +1769,10 @@ static int schema_make_internal_depth(json_t* sch,
celix_jansson_uri_t* eff_base_out,
celix_jansson_schema_node_t** out,
int depth) {
+ //LCOV_EXCL_START: defensive — all call sites pass non-NULL arguments
if (!sch || !root || !out)
return CELIX_JANSSON_SCHEMA_ERROR_INVALID_ARGUMENT;
+ //LCOV_EXCL_STOP
/* Guard against infinite recursion for self-referencing schemas */
if (depth > 20)
return CELIX_JANSSON_SCHEMA_ERROR_INVALID_SCHEMA;
@@ -1737,7 +1820,8 @@ static int schema_make_internal_depth(json_t* sch,
n->refcount = 1;
n->u.boolean.value = json_boolean_value(sch);
*out = n;
- if (has_id) (void)celix_jansson_schema_root_insert(root, &my_base, n);
+ /* No $id registration: a boolean schema cannot carry $id (json_object_get
+ * returns NULL for non-objects, so has_id is always false here) */
return CELIX_JANSSON_SCHEMA_OK;
}
if (!json_is_object(sch))
@@ -1749,6 +1833,12 @@ static int schema_make_internal_depth(json_t* sch,
char* base_loc = effective_base ? celix_jansson_uri_location(effective_base) : strdup("");
celix_jansson_schema_file_t* sf = celix_jansson_schema_root_get_or_create_file(root, base_loc);
free(base_loc);
+ if (!sf) {
+ /* Mirror the my_base cleanup of the other failing paths */
+ if (has_id && !id_stored_in_out)
+ celix_jansson_uri_clear(&my_base);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
const char* dk;
json_t* dv;
json_object_foreach(defs, dk, dv) {
@@ -1844,14 +1934,28 @@ static int schema_make_internal_depth(json_t* sch,
free(rloc); free(rfra);
celix_jansson_uri_clear(&ref_uri);
- if (has_id) (void)celix_jansson_schema_root_insert(root, &my_base, rn);
+ /* No $id registration: draft-7 treats $id alongside $ref as not a real
+ * $id (JSON Reference: members other than $ref are ignored — see the
+ * !refv guard above), so has_id is always false here */
*out = rn;
return CELIX_JANSSON_SCHEMA_OK;
}
int rc = make_type_schema(sch, root, effective_base, out, depth);
- if (rc == CELIX_JANSSON_SCHEMA_OK && has_id && *out)
- (void)celix_jansson_schema_root_insert(root, &my_base, *out);
+ if (rc == CELIX_JANSSON_SCHEMA_OK && has_id && *out) {
+ int ir = celix_jansson_schema_root_insert(root, &my_base, *out);
+ if (ir == CELIX_JANSSON_SCHEMA_ERROR_NOMEM) {
+ /* The registry did not ref *out; unref it and clear it so the caller
+ * does not take ownership on the error path */
+ celix_jansson_schema_unref(*out);
+ *out = NULL;
+ if (!id_stored_in_out)
+ celix_jansson_uri_clear(&my_base);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
+ /* DUPLICATE_URI is benign: with $id:"" the registration above already
+ * happened and set_root_schema's root_insert must tolerate it as well */
+ }
if (has_id && !id_stored_in_out)
celix_jansson_uri_clear(&my_base);
return rc;
@@ -2023,8 +2127,18 @@ static celix_jansson_schema_node_t* resolve_document_fragment(
celix_json_pointer_clear(&ptr);
}
}
- (void)celix_jansson_schema_root_insert(root, &full_uri, sch);
+ int ir = celix_jansson_schema_root_insert(root, &full_uri, sch);
celix_jansson_uri_clear(&full_uri);
+ //LCOV_EXCL_START: unreachable — the file for `location` was created before
+ //the walk (get_or_create_file always hits the cache, so insert cannot OOM)
+ if (ir == CELIX_JANSSON_SCHEMA_ERROR_NOMEM) {
+ /* The registry did not ref sch (NOMEM precedes the ref); unref it and
+ * return NULL instead of a dangling pointer */
+ celix_jansson_schema_unref(sch);
+ return NULL;
+ }
+ //LCOV_EXCL_STOP
+ /* DUPLICATE_URI is benign: the fragment may already be registered */
celix_jansson_schema_unref(sch); /* registry holds the owning ref now */
return sch;
@@ -2063,7 +2177,21 @@ static int compile_external_document(celix_jansson_schema_root_t* root, const ch
}
/* Register root at retrieval URI — auto-resolves waiting fragment="" placeholder */
- (void)celix_jansson_schema_root_insert(root, &retr, sch);
+ int ir = celix_jansson_schema_root_insert(root, &retr, sch);
+ //LCOV_EXCL_START: unreachable — compile_external_document is only called for
+ //files that already exist (the placeholder creation created them), so
+ //get_or_create_file always hits the cache and insert cannot OOM
+ if (ir == CELIX_JANSSON_SCHEMA_ERROR_NOMEM) {
+ /* The registry did not ref sch; propagate the OOM instead of returning a
+ * fake OK that would leave the placeholder unresolved forever */
+ json_decref(doc); /* doc has not been transferred to sf->document */
+ celix_jansson_uri_clear(&retr);
+ celix_jansson_schema_unref(sch);
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
+ //LCOV_EXCL_STOP
+ /* DUPLICATE_URI is benign: a document whose own $id equals the retrieval URI
+ * was already registered during make */
/* Retain document for fragment resolution */
celix_jansson_schema_file_t* sf = celix_jansson_schema_root_get_or_create_file(root, location);
@@ -2072,9 +2200,11 @@ static int compile_external_document(celix_jansson_schema_root_t* root, const ch
sf->document = doc; /* ownership transferred */
free(sf->base_uri);
sf->base_uri = strdup(location);
+ //LCOV_EXCL_START: unreachable — callers guarantee the file already exists
} else {
json_decref(doc);
}
+ //LCOV_EXCL_STOP
celix_jansson_uri_clear(&retr);
celix_jansson_schema_unref(sch);
@@ -2274,8 +2404,7 @@ int celix_jansson_schema_root_insert(celix_jansson_schema_root_t* root,
static void celix_jansson_schema_file_free(void* value) {
celix_jansson_schema_file_t* sf = (celix_jansson_schema_file_t*)value;
- if (!sf)
- return;
+ assert(sf != NULL);
celix_stringHashMap_destroy(sf->schemas); /* values freed via creation-time removed callback */
celix_stringHashMap_destroy(sf->unresolved);
for (size_t i = 0; i < sf->retained.len; i++)
@@ -2451,6 +2580,15 @@ int celix_jansson_schema_set_root_schema(celix_jansson_schema_validator_t* v, js
}
root->original_schema = json_deep_copy(schema);
+ if (!root->original_schema) {
+ /* The first copy (L2446) is still owned by this frame; the decref at
+ * the end of the compile path (after schema_make_internal_depth) is not
+ * reached here, so release it explicitly. */
+ json_decref(copy);
+ if (errmsg)
+ *errmsg = strdup(celix_jansson_schema_strerror(CELIX_JANSSON_SCHEMA_ERROR_NOMEM));
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
celix_jansson_uri_t root_base;
celix_jansson_uri_init(&root_base, "");
@@ -2466,7 +2604,22 @@ int celix_jansson_schema_set_root_schema(celix_jansson_schema_validator_t* v, js
}
root->root = sch;
- (void)celix_jansson_schema_root_insert(root, &root_base, sch);
+ int rir = celix_jansson_schema_root_insert(root, &root_base, sch);
+ if (rir == CELIX_JANSSON_SCHEMA_ERROR_NOMEM) {
+ /* root_insert's NOMEM happens before its registry ref (L2256), so the
+ * registry holds no reference; root->root still owns the only one.
+ * Unref it and leave the root in the same clean state as the other
+ * failing paths below (root->root == NULL). */
+ celix_jansson_schema_unref(root->root);
+ root->root = NULL;
+ celix_jansson_uri_clear(&root_base);
+ if (errmsg)
+ *errmsg = strdup(celix_jansson_schema_strerror(CELIX_JANSSON_SCHEMA_ERROR_NOMEM));
+ return CELIX_JANSSON_SCHEMA_ERROR_NOMEM;
+ }
+ /* DUPLICATE_URI is benign here: root->files was cleared above (L2444), so the
+ * only way the "" key already exists is the top-level $id:"" registration done
+ * during make (L1853); the registry already holds a reference in that case. */
{
char* rloc = celix_jansson_uri_location(&root_base);
diff --git a/libs/jansson_ext/src/celix_jansson_uri.c b/libs/jansson_ext/src/celix_jansson_uri.c
index 89c327b30..3988438e9 100644
--- a/libs/jansson_ext/src/celix_jansson_uri.c
+++ b/libs/jansson_ext/src/celix_jansson_uri.c
@@ -212,7 +212,10 @@ int celix_jansson_uri_derive(const celix_jansson_uri_t* base, const char* uri_st
}
/* Now resolve uri_str relative to this */
- return celix_jansson_uri_update(out, uri_str);
+ int rc = celix_jansson_uri_update(out, uri_str);
+ if (rc != 0)
+ celix_jansson_uri_clear(out); /* release the copied base components on failure */
+ return rc;
}
int celix_jansson_uri_append(const celix_jansson_uri_t* u, const char* token, celix_jansson_uri_t* out) {