libisu-internal: properly handle strdup memory allocation errors 10/318210/2
authorAdam Michalski <a.michalski2@partner.samsung.com>
Wed, 25 Sep 2024 09:47:09 +0000 (11:47 +0200)
committerAdam Michalski <a.michalski2@partner.samsung.com>
Wed, 25 Sep 2024 14:50:52 +0000 (16:50 +0200)
libisu-internal.c: properly handle memory allocation errors when
using strdup function which can fail (as it may return NULL if
insufficient memory was available).

Change-Id: I4b2ed05ddb574bb445191d346dd589d8d324e391

src/libisu/libisu-internal.c

index 28d77d1f6e3fa0b8a9c06900da00a4890dcb134d..47e9151c29386a736e17a2ef94e1ebcfbec93081 100644 (file)
@@ -234,6 +234,10 @@ isu_result isu_dbus_call(const char *method, const char *parameter, char **outpa
         if (outparam) {
             if (version && version[0] != '\0') {
                 *outparam = g_strdup(version);
+                if (*outparam == NULL) {
+                    SLOGE("Memory allocation error (%d): %m");
+                    res = ISU_RES_ERR_INTERNAL;
+                }
             } else {
                 *outparam = NULL;
             }
@@ -293,6 +297,13 @@ isu_result isu_dbus_list_call(struct _isu_pkg_list **list) {
             prev->next = element;
         }
         prev = element;
+        if (element->name == NULL) {
+            SLOGE("Memory allocation error (%d): %m");
+            isu_list_free(*list);
+            *list = NULL;
+            res = ISU_RES_ERR_INTERNAL;
+            goto exit;
+        }
     }
 exit:
     if (body != NULL) {
@@ -323,6 +334,7 @@ isu_result isu_pkg_info_call(char *pkg_name, struct _isu_pkg_info **pkg_info)
     service_array = g_variant_get_strv(array, &len);
     struct _isu_pkg_info *_pkg_info = malloc(sizeof(*_pkg_info));
     if (_pkg_info == NULL) {
+        SLOGE("Memory allocation error (%d): %m");
         res = ISU_RES_ERR_INTERNAL;
         goto exit;
     }
@@ -337,6 +349,12 @@ isu_result isu_pkg_info_call(char *pkg_name, struct _isu_pkg_info **pkg_info)
     }
     for (size_t i = 0; i < len; i++) {
         _pkg_info->service_files[i] = strdup(service_array[i]);
+        if (_pkg_info->service_files[i] == NULL) {
+            SLOGE("Memory allocation error (%d): %m");
+            isu_pkg_info_free(_pkg_info);
+            res = ISU_RES_ERR_INTERNAL;
+            goto exit;
+        }
     }
 
     *pkg_info = _pkg_info;
@@ -823,11 +841,20 @@ isu_result isu_install_dir(const char *path)
     return isu_dbus_call(DBUS_METHOD_INSTALL_DIR, path, NULL);
 }
 
-int isu_version_compare_internal(const char *ver_a, const char *ver_b)
+isu_result isu_version_compare_internal(const char *ver_a, const char *ver_b, int *comp_result)
 {
+    assert(ver_a);
+    assert(ver_b);
+    assert(comp_result);
+
     __attribute__ ((__cleanup__(cleanup_char_ptr))) char *ver_a_copy = strdup(ver_a);
     __attribute__ ((__cleanup__(cleanup_char_ptr))) char *ver_b_copy = strdup(ver_b);
 
+    if (ver_a_copy == NULL || ver_b_copy == NULL) {
+        *comp_result = 0;
+        return ISU_RES_ERR_INTERNAL;
+    }
+
     const char delim[] = ".";
 
     char *saveptr_a, *saveptr_b;
@@ -839,16 +866,19 @@ int isu_version_compare_internal(const char *ver_a, const char *ver_b)
         int num_b = token_b ? atoi(token_b) : 0;
 
         if (num_a > num_b) {
-            return 1;
+            *comp_result = 1;
+            return ISU_RES_OK;
         } else if (num_a < num_b) {
-            return -1;
+            *comp_result = -1;
+            return ISU_RES_OK;
         }
 
         token_a = strtok_r(NULL, delim, &saveptr_a);
         token_b = strtok_r(NULL, delim, &saveptr_b);
     }
 
-    return 0;
+    *comp_result = 0;
+    return ISU_RES_OK;
 }
 
 isu_result isu_get_version_internal(const char *pkgId, char **version)
@@ -864,20 +894,38 @@ isu_result isu_get_version_internal(const char *pkgId, char **version)
     snprintf(buff, sizeof(buff), "%s/%s/isu.cfg", ISU_PKG_PATH, pkgId);
     struct _isu_pkg_info *rw_pkg_info = get_pkg_info(buff);
 
+    bool bError = false;
     if (ro_pkg_info == NULL) {
         if (rw_pkg_info == NULL) {
             *version = NULL;
         } else {
             *version = g_strdup(rw_pkg_info->version);
+            if (*version == NULL) {
+                bError = true;
+            }
         }
     } else {
         if (rw_pkg_info == NULL) {
             *version = g_strdup(ro_pkg_info->version);
+            if (*version == NULL) {
+                bError = true;
+            }
         } else {
-            if (isu_version_compare_internal(ro_pkg_info->version, rw_pkg_info->version) <= 0) {
+            int comp_res;
+            if (isu_version_compare_internal(ro_pkg_info->version, rw_pkg_info->version, &comp_res) != ISU_RES_OK) {
+                *version = NULL;
+                bError = true;
+            }
+            if (comp_res <= 0) {
                 *version = g_strdup(rw_pkg_info->version);
+                if (*version == NULL) {
+                    bError = true;
+                }
             } else {
                 *version = g_strdup(ro_pkg_info->version);
+                if (*version == NULL) {
+                    bError = true;
+                }
             }
         }
     }
@@ -887,7 +935,10 @@ isu_result isu_get_version_internal(const char *pkgId, char **version)
     isu_pkg_info_free(ro_pkg_info);
     isu_pkg_info_free(rw_pkg_info);
 
-    return *version ? ISU_RES_OK : ISU_RES_ERR_NOT_EXIST;
+    if (bError)
+        return ISU_RES_ERR_INTERNAL;
+    else
+        return *version ? ISU_RES_OK : ISU_RES_ERR_NOT_EXIST;
 }
 
 bool is_valid_isu_pkg(const char *path)