Skip to content

Commit

Permalink
gh-685: Avoid insertion into non-object and add test for key collision.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed May 21, 2024
1 parent 769a9e5 commit a6a48e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
27 changes: 26 additions & 1 deletion libs/utils/gtest/src/PropertiesEncodingTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1103,4 +1103,29 @@ TEST_F(PropertiesSerializationTestSuite, JsonErrorToCelixStatusTest) {
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_jsonErrorToStatus(json_error_numeric_overflow));
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_jsonErrorToStatus(json_error_item_not_found));
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_jsonErrorToStatus(json_error_index_out_of_range));
}
}

TEST_F(PropertiesSerializationTestSuite, KeyCollision) {
celix_autoptr(celix_properties_t) props = celix_properties_create();
// pick keys such that key1 appears before key2 when iterating over the properties
celix_properties_set(props, "a.b.haha.arbifdadfsfa", "value1");
celix_properties_set(props, "a.b.haha", "value2");

celix_autofree char* output = nullptr;
auto status = celix_properties_saveToString(props, CELIX_PROPERTIES_ENCODE_NESTED_STYLE | CELIX_PROPERTIES_ENCODE_ERROR_ON_COLLISIONS,
&output);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, status);

celix_autoptr(celix_properties_t) props2 = celix_properties_create();
// pick keys such that key1 appears before key2 when iterating over the properties
celix_properties_set(props2, "a.b.c", "value1");
celix_properties_set(props2, "a.b.c.d", "value2");
status = celix_properties_saveToString(props2, CELIX_PROPERTIES_ENCODE_NESTED_STYLE | CELIX_PROPERTIES_ENCODE_ERROR_ON_COLLISIONS,
&output);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, status);
status = celix_properties_saveToString(props2, CELIX_PROPERTIES_ENCODE_NESTED_STYLE, &output);
// "a.b.c.d" is silently discarded
EXPECT_STREQ(R"({"a":{"b":{"c":"value1"}}})", output);
std::cout << output << std::endl;
EXPECT_EQ(CELIX_SUCCESS, status);
}
9 changes: 6 additions & 3 deletions libs/utils/src/properties_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,12 @@ static celix_status_t celix_properties_addPropertiesEntryToJson(const celix_prop
return ENOMEM;
}
json_t* subObj = json_object_get(jsonObj, name);
if (subObj && !json_is_object(subObj) && flags & CELIX_PROPERTIES_ENCODE_ERROR_ON_COLLISIONS) {
celix_err_pushf("Invalid key collision. Key '%s' already exists.", name);
return CELIX_ILLEGAL_ARGUMENT;
if (subObj && !json_is_object(subObj)) {
if (flags & CELIX_PROPERTIES_ENCODE_ERROR_ON_COLLISIONS) {
celix_err_pushf("Invalid key collision. Key '%s' already exists.", name);
return CELIX_ILLEGAL_ARGUMENT;
}
return CELIX_SUCCESS;
}
if (!subObj) {
subObj = json_object();
Expand Down

0 comments on commit a6a48e5

Please sign in to comment.