This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/type_support_for_properties
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to 
refs/heads/feature/type_support_for_properties by this push:
     new 95460e98 Add ftell check and error injection test for properties
95460e98 is described below

commit 95460e98acbceb145bef06a10f45457f0a069d57
Author: Pepijn Noltes <[email protected]>
AuthorDate: Mon Nov 20 19:00:18 2023 +0100

    Add ftell check and error injection test for properties
---
 libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc | 10 ++++++++++
 libs/utils/src/properties.c                               |  8 +++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc 
b/libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
index 468b45f3..97057861 100644
--- a/libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
+++ b/libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
@@ -46,6 +46,7 @@ class PropertiesErrorInjectionTestSuite : public 
::testing::Test {
         celix_ei_expect_fopen(nullptr, 0, nullptr);
         celix_ei_expect_fputc(nullptr, 0, 0);
         celix_ei_expect_fseek(nullptr, 0, 0);
+        celix_ei_expect_ftell(nullptr, 0, 0);
         celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
         celix_ei_expect_celix_stringHashMap_put(nullptr, 0, 0);
     }
@@ -225,6 +226,15 @@ TEST_F(PropertiesErrorInjectionTestSuite, LoadFailureTest) 
{
     ASSERT_EQ(1, celix_err_getErrorCount());
     celix_err_resetErrors();
 
+    // When a ftell error injection is set for celix_properties_loadWithStream
+    celix_ei_expect_ftell((void*)celix_properties_loadWithStream, 0, -1);
+    // Then the celix_properties_loadWithStream call fails
+    props = celix_properties_loadWithStream(memStream);
+    ASSERT_EQ(nullptr, props);
+    // And a celix err msg is set
+    ASSERT_EQ(1, celix_err_getErrorCount());
+    celix_err_resetErrors();
+
     // When a malloc error injection is set for celix_properties_loadWithStream
     celix_ei_expect_malloc((void*)celix_properties_loadWithStream, 0, nullptr);
     // Then the celix_properties_loadWithStream call fails
diff --git a/libs/utils/src/properties.c b/libs/utils/src/properties.c
index c477c5b8..8d3a2dfa 100644
--- a/libs/utils/src/properties.c
+++ b/libs/utils/src/properties.c
@@ -486,7 +486,13 @@ celix_properties_t* celix_properties_loadWithStream(FILE* 
file) {
         celix_err_pushf("Cannot seek to end of file. Got error %i", errno);
         return NULL;
     }
-    size_t fileSize = ftell(file);
+
+    long fileSize = ftell(file);
+    if (fileSize < 0) {
+        celix_err_pushf("Cannot get file size. Got error %i", errno);
+        return NULL;
+    }
+
     rc = fseek(file, 0, SEEK_SET);
     if (rc != 0) {
         celix_err_pushf("Cannot seek to start of file. Got error %i", errno);

Reply via email to