Fix coverity issues 37/287737/2 accepted/tizen/unified/20230209.111248
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 3 Feb 2023 08:03:34 +0000 (09:03 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 3 Feb 2023 11:29:56 +0000 (12:29 +0100)
Change-Id: I7cd5aef567a7b46e47c356b300992a787b074ac3

delta-verifier/delta-verifier.c
update-manager/fota/fota-storage-checker.c

index 635fb6f033fee8703a92b749684d26c40e73c5c4..f3e1a34c076a93c6c5575daf59108cc16bfa4bb8 100644 (file)
@@ -18,9 +18,11 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 
 
 #include <stdio.h>
+#include <errno.h>
 #include <getopt.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <ctype.h>
 #include <unistd.h>
 #include <system_info.h>
@@ -118,10 +120,13 @@ static int write_info_from_api(FILE* write_file)
        return 0;
 }
 
-static size_t get_file_size(FILE* file)
+static off_t get_file_size(FILE* file)
 {
        fseek(file, 0, SEEK_END);
-       size_t size = ftell(file);
+       off_t size = ftello(file);
+       if (size == -1)
+               ERR_ERRNO("File size check error.");
+
        fseek(file, 0 , SEEK_SET);
 
        return size;
@@ -141,7 +146,12 @@ static int write_info_from_file(FILE* read_file, FILE* write_file)
        }
 
        size_t read_size = get_file_size(read_file);
-       char* read_buf = (char*)malloc(read_size);
+       // It doesn't matter if it's because of an error or anything else, if the returned
+       // value is equal to SIZE_MAX then we have to stop executing the function.
+       if (read_size == SIZE_MAX)
+               return -1;
+
+       char* read_buf = (char*)malloc(read_size+1);
        if (read_buf == NULL) {
                ERR_ERRNO("malloc()");
                return -1;
@@ -152,6 +162,7 @@ static int write_info_from_file(FILE* read_file, FILE* write_file)
                free(read_buf);
                return -1;
        }
+       read_buf[read_size] = '\0';
 
        const char* separators = " =\n\"";
        char* read_word = strtok(read_buf, separators);
@@ -314,23 +325,33 @@ static void print_string_arrays_verbose_mode(int device_size, char** device, int
 
 static int compare_info_files(FILE* update_info_file, FILE* device_info_file, bool verbose_output)
 {
+       int ret = 0;
        size_t update_info_size, device_info_size;
+       char* device_read_buf = NULL;
+       char* update_read_buf = NULL;
+       char** device_strings = NULL;
+       char** update_strings = NULL;
+       int device_file_lines = 0;
+       int update_file_lines = 0;
+
        update_info_size = get_file_size(update_info_file);
+       if (update_info_size == SIZE_MAX) {
+               ERR("Error checking size of the update_info file");
+               ret = -1;
+               goto cleanup;
+       }
        device_info_size = get_file_size(device_info_file);
+       if (device_info_size == SIZE_MAX) {
+               ERR("Error checking size of the device_info file");
+               ret = -1;
+               goto cleanup;
+       }
 
        // if there are no variables in update-info.ini file, delta should pass verification
        if (update_info_size == 0) {
                return 0;
        }
 
-       int ret = 0;
-       char* device_read_buf = NULL;
-       char* update_read_buf = NULL;
-       char** device_strings = NULL;
-       char** update_strings = NULL;
-       int device_file_lines = 0;
-       int update_file_lines = 0;
-
        update_read_buf = (char*)malloc(update_info_size + 1);
        if (update_read_buf == NULL) {
                ERR_ERRNO("malloc()");
index d5c8fc71ed6f2bcfb99715e9f75993f8a43885ba..bc06cdc3a9290f6ffbb3b8801ea683090ca8ad7d 100644 (file)
@@ -12,6 +12,10 @@ gchar *fota_storage_find_delta(const char* mount_path, const char *folder_name)
 
        /* Check storage have delta.tar */
        storage_delta_path = g_strjoin("/", mount_path, folder_name, FOTA_DELTA_FILENAME, NULL);
+       if (storage_delta_path == NULL) {
+               _FLOGI("Memory allocation error for storage delta path.");
+               goto verify_destroy;
+       }
        if (!g_file_test(storage_delta_path, G_FILE_TEST_EXISTS)) {
                gchar *tmp_storage_delta_path = storage_delta_path;
                storage_delta_path = g_strconcat(tmp_storage_delta_path, ".gz", NULL);
@@ -30,10 +34,8 @@ gchar *fota_storage_find_delta(const char* mount_path, const char *folder_name)
        }
 
        /* Check storage have appropriate delta */
-       if (check_is_delta_appropriate(storage_delta_path, NULL) != 0) {
-               ret = -1;
+       if (check_is_delta_appropriate(storage_delta_path, NULL) != 0)
                goto verify_destroy;
-       }
 
        _FLOGI("Success to find delta from storage(%s)", storage_delta_path);
        is_appropriate = true;