From: Mateusz Moscicki Date: Fri, 3 Feb 2023 08:03:34 +0000 (+0100) Subject: Fix coverity issues X-Git-Tag: accepted/tizen/unified/20230209.111248^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=70d431802041f86ae92ef5090245ff3effc18f9d;p=platform%2Fcore%2Fsystem%2Fupdate-control.git Fix coverity issues Change-Id: I7cd5aef567a7b46e47c356b300992a787b074ac3 --- diff --git a/delta-verifier/delta-verifier.c b/delta-verifier/delta-verifier.c index 635fb6f..f3e1a34 100644 --- a/delta-verifier/delta-verifier.c +++ b/delta-verifier/delta-verifier.c @@ -18,9 +18,11 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. #include +#include #include #include #include +#include #include #include #include @@ -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()"); diff --git a/update-manager/fota/fota-storage-checker.c b/update-manager/fota/fota-storage-checker.c index d5c8fc7..bc06cdc 100644 --- a/update-manager/fota/fota-storage-checker.c +++ b/update-manager/fota/fota-storage-checker.c @@ -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;