From caedd795072c71ef938ae2939f07c4ac7b78fb20 Mon Sep 17 00:00:00 2001 From: PengZheng Date: Sun, 19 Nov 2023 14:48:59 +0800 Subject: [PATCH] Avoid unnecessary strlen call and check error condition in celix_properties_storeEscapedString. --- libs/utils/gtest/src/PropertiesTestSuite.cc | 1 + libs/utils/src/properties.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/utils/gtest/src/PropertiesTestSuite.cc b/libs/utils/gtest/src/PropertiesTestSuite.cc index f77abf82a..d9b4e8a2e 100644 --- a/libs/utils/gtest/src/PropertiesTestSuite.cc +++ b/libs/utils/gtest/src/PropertiesTestSuite.cc @@ -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); } diff --git a/libs/utils/src/properties.c b/libs/utils/src/properties.c index a8e3063f0..acaf8535b 100644 --- a/libs/utils/src/properties.c +++ b/libs/utils/src/properties.c @@ -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);