Blob Blame History Raw
From 6948a203653e014198c40847c8dd5bde03e0fe9a Mon Sep 17 00:00:00 2001
From: Iker Pedrosa <ipedrosa@redhat.com>
Date: Wed, 6 Mar 2024 10:42:37 +0100
Subject: [PATCH] Fix static analyzer detected issues (#196)

* lib: fix resource leak

The code that was analyzed and the existing one have diverged a little
bit but the problem still exists.

```
Error: RESOURCE_LEAK (CWE-772):
libeconf-0.4.1/lib/libeconf.c:312: alloc_arg: "econf_readDirsHistory" allocates memory that is stored into "key_files".
libeconf-0.4.1/lib/libeconf.c:321: leaked_storage: Variable "key_files" going out of scope leaks the storage it points to.
319| comment);
320| if (error != ECONF_SUCCESS)
321|-> return error;
322|
323| // Merge the list of acquired key_files into merged_file
```

Resolves: https://issues.redhat.com/browse/RHEL-24989

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>

* lib: fix resource leak

```
Error: RESOURCE_LEAK (CWE-772):

libeconf-0.4.1/util/econftool.c:185: alloc_arg: "econf_getExtValue" allocates memory that is stored into "value".
libeconf-0.4.1/util/econftool.c:189: leaked_storage: Variable "value" going out of scope leaks the storage it points to.
187| fprintf(stderr, "%d: %s\n", econf_error, econf_errString(econf_error));
188| econf_free(keys);
189|-> return econf_error;
190| }
191| if (value != NULL) {
```

Resolves: https://issues.redhat.com/browse/RHEL-24989

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>

* lib: fix uninitialized variable

```
Error: UNINIT (CWE-457):
libeconf-0.4.1/lib/libeconf.c:161: var_decl: Declaring variable "key_file" without initializer.
libeconf-0.4.1/lib/libeconf.c:247: uninit_use_in_call: Using uninitialized value "key_file" when calling "econf_freeFile".
245|     *key_files = calloc(*size, sizeof(econf_file*));
246|     if (*key_files == NULL) {
247|->     econf_freeFile(key_file);
248|       return ECONF_NOMEM;
249|     }
```

Resolves: https://issues.redhat.com/browse/RHEL-24989

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>

---------

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
---
 lib/libeconf.c     | 4 +++-
 lib/libeconf_ext.c | 8 ++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/libeconf.c b/lib/libeconf.c
index 3d2a024..3dfe51e 100644
--- a/lib/libeconf.c
+++ b/lib/libeconf.c
@@ -285,7 +285,7 @@ econf_err econf_readDirsHistoryWithCallback(econf_file ***key_files,
 {
   const char *suffix, *default_dirs[3] = {NULL, NULL, NULL};
   char *distfile, *etcfile, *cp;
-  econf_file *key_file;
+  econf_file *key_file = NULL;
   econf_err error;
 
   *size = 0;
@@ -396,6 +396,8 @@ econf_err econf_readDirsHistoryWithCallback(econf_file ***key_files,
   if (configure_dirs == NULL)
   {
     free(suffix_d);
+    free(*key_files);
+    *key_files = NULL;
     return ECONF_NOMEM;
   }
   configure_dirs[0] = suffix_d;
diff --git a/lib/libeconf_ext.c b/lib/libeconf_ext.c
index 137b869..93adab9 100644
--- a/lib/libeconf_ext.c
+++ b/lib/libeconf_ext.c
@@ -90,15 +90,19 @@ econf_getExtValue(econf_file *kf, const char *group,
     {
       /* one quoted string only */
       (*result)->values = realloc ((*result)->values, sizeof (char*) * ++n_del);
-      if ((*result)->values == NULL)
+      if ((*result)->values == NULL) {
+        econf_freeExtValue(*result);
         return ECONF_NOMEM; /* memory allocation failed */
+      }
       (*result)->values[n_del-1] = strdup(value_string);
     } else {
       /* splitting into a character array */
       while ((line = strsep(&value_string, "\n")) != NULL) {
         (*result)->values = realloc ((*result)->values, sizeof (char*) * ++n_del);
-        if ((*result)->values == NULL)
+        if ((*result)->values == NULL) {
+          econf_freeExtValue(*result);
           return ECONF_NOMEM; /* memory allocation failed */
+        }
         (*result)->values[n_del-1] = strdup(trim(line));
       }
     }
-- 
2.44.0