Skip to content

Commit

Permalink
Avoid unnecessary strlen call and check error condition in celix_prop…
Browse files Browse the repository at this point in the history
…erties_storeEscapedString.
  • Loading branch information
PengZheng committed Nov 19, 2023
1 parent 03731d6 commit caedd79
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions libs/utils/gtest/src/PropertiesTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ TEST_F(PropertiesTestSuite, PropertiesNullArgumentsTest) {
EXPECT_EQ(CELIX_SUCCESS, celix_properties_setDouble(nullptr, "key", 1.0));
EXPECT_EQ(CELIX_SUCCESS, celix_properties_setBool(nullptr, "key", true));
EXPECT_EQ(CELIX_SUCCESS, celix_properties_setVersion(nullptr, "key", nullptr));
EXPECT_EQ(CELIX_SUCCESS, celix_properties_setVersionWithoutCopy(nullptr, "key", nullptr));
celix_autoptr(celix_properties_t) copy = celix_properties_copy(nullptr);
EXPECT_NE(nullptr, copy);
}
Expand Down
5 changes: 3 additions & 2 deletions libs/utils/src/properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,12 @@ celix_properties_t* celix_properties_loadFromString(const char* input) {
*/
static int celix_properties_storeEscapedString(FILE* file, const char* str) {
int rc = 0;
for (int i = 0; i < strlen(str); i += 1) {
size_t len = strlen(str);
for (size_t i = 0; i < len && rc != EOF; i += 1) {
if (str[i] == '#' || str[i] == '!' || str[i] == '=' || str[i] == ':') {
rc = fputc('\\', file);
if (rc == EOF) {
break;
continue;
}
}
rc = fputc(str[i], file);
Expand Down

0 comments on commit caedd79

Please sign in to comment.