Skip to content

Commit

Permalink
Add schema tree nullptr check
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Oct 14, 2024
1 parent 71d0b92 commit 45bb0ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions components/core/src/clp/ffi/KeyValuePairLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ auto KeyValuePairLogEvent::create(
NodeIdValuePairs user_generated_node_id_value_pairs,
UtcOffset utc_offset
) -> OUTCOME_V2_NAMESPACE::std_result<KeyValuePairLogEvent> {
if (nullptr == auto_generated_schema_tree || nullptr == user_generated_schema_tree) {
return std::errc::invalid_argument;
}

if (auto const ret_val{validate_node_id_value_pairs(
*auto_generated_schema_tree,
auto_generated_node_id_value_pairs
Expand Down
4 changes: 3 additions & 1 deletion components/core/src/clp/ffi/KeyValuePairLogEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class KeyValuePairLogEvent {
* @param user_generated_node_id_value_pairs
* @param utc_offset
* @return A result containing the key-value pair log event or an error code indicating the
* failure. See `validate_node_id_value_pairs` for the possible error codes.
* failure, or an error code indicating the failure:
* - std::errc::invalid_argument if any of the given schema tree pointers are null.
* - Forwards `validate_node_id_value_pairs`'s return values.
*/
[[nodiscard]] static auto create(
std::shared_ptr<SchemaTree const> auto_generated_schema_tree,
Expand Down
19 changes: 19 additions & 0 deletions components/core/tests/test-ffi_KeyValuePairLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,25 @@ TEST_CASE("ffi_KeyValuePairLogEvent_create", "[ffi]") {
REQUIRE_FALSE(result.has_error());
}

SECTION("Test schema tree pointers being null") {
REQUIRE(assert_kv_pair_log_event_creation_failure(
nullptr,
user_generated_schema_tree,
{},
{},
UtcOffset{0},
std::errc::invalid_argument
));
REQUIRE(assert_kv_pair_log_event_creation_failure(
auto_generated_schema_tree,
nullptr,
{},
{},
UtcOffset{0},
std::errc::invalid_argument
));
}

SECTION("Test mismatched types") {
KeyValuePairLogEvent::NodeIdValuePairs invalid_node_id_value_pairs;
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
Expand Down

0 comments on commit 45bb0ec

Please sign in to comment.