Fix error -Werror=sign-compare 70/310170/2
authorYoungjae Cho <y0.cho@samsung.com>
Tue, 23 Apr 2024 09:59:49 +0000 (18:59 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 23 Apr 2024 10:59:15 +0000 (19:59 +0900)
It blames comparing signed type with unsigned type.

Fix it in two step:
 1. If signed variable is definitely a nonnegative value at the
    code context, cast it into unsigned type directly.
 2. If it is unclear whether a signed variable is nonnegative, then
    add assertion for testing the variable is nonnegative. After
    that assertion, cast it into unsigned type. It helps catching
    violation that casting negative value into unsigned variable.

Change-Id: Ib4f2a2cb2ad1901518d0726de6035ac4a033bc67
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/img-verifier/img-verifier.c
src/upgrade-apply-deltafs/engine/SS_FSUpdate.c
src/upgrade-apply-deltafs/engine/SS_PatchDelta.c
src/upgrade-apply-deltafs/ua.c
src/upgrade-apply/main.c
src/upgrade-apply/patch/patch.c
src/upgrade-apply/sha1/sha1.c

index 95ae7a7b1dd7df61157d43f0697e8ccec9ac9afc..375d7426c68dd75a0e6b65d5714476669b6ba460 100644 (file)
@@ -124,13 +124,13 @@ static int read_signed_file(const char *path)
        ASSERT_RETV(signed_file.signature, ENOMEM, "Not enough memory");
 
        ret = read(signed_file_fd, signed_file.signature, signed_file.signature_size);
-       ASSERT_RETV(ret == signed_file.signature_size, errno, "Failed to read signature : %m");
+       ASSERT_RETV(ret >= 0 && (unsigned int)ret == signed_file.signature_size, errno, "Failed to read signature : %m");
 
        signed_file.certificate = malloc(signed_file.certificate_size);
        ASSERT_RETV(signed_file.certificate, ENOMEM, "Not enough memory");
 
        ret = read(signed_file_fd, signed_file.certificate, signed_file.certificate_size);
-       ASSERT_RETV(ret == signed_file.certificate_size, errno, "Failed to read certificate : %m");
+       ASSERT_RETV(ret >= 0 && (unsigned int)ret == signed_file.certificate_size, errno, "Failed to read certificate : %m");
 
        return SUCCEED;
 }
index c87205d1e880a3c9c73d12c74b68169edcdad569..dc946c6cb6566a676a702e4fdd1c6ca591c4f16a 100644 (file)
@@ -394,7 +394,7 @@ SS_WriteFile(long wHandle,
        }
 
        ret = write(wHandle, pbBuffer, dwSize);
-       if (ret < 0 || ret != dwSize) {
+       if (ret < 0 || (unsigned int)ret != dwSize) {
                LOGE(" Failed with return value: %d\n", ret);
                LOGL(LOG_SSENGINE, "ret=%d, dwSize=%u write errno=%d\n",
                        ret, (unsigned int)dwSize, errno);
@@ -484,7 +484,7 @@ SS_ReadFile(long wHandle,
                return E_SS_READ_ERROR;
        }
 
-       if (ret != dwSize && ((ret + dwPosition) != (unsigned long)SS_GetFileSize(wHandle)))
+       if ((unsigned int)ret != dwSize && ((ret + dwPosition) != (unsigned long)SS_GetFileSize(wHandle)))
                return E_SS_READ_ERROR;
 
        pbBuffer[ret] = '\0';
index 9346bc6c6ad08cfa58bf85fa2492c84ac5ca7543..cf88b6efaadf527f15ca07ebf4361a00015b97a2 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
 #include <sys/types.h>
@@ -204,8 +205,9 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
 
                if (retry > 0) {
                        SS_GetAvailableFreeSpace(source_filename, &free_space);
+                       assert(patch_data_size > 0);
                        enough_space = (free_space > (256 << 10)) &&            // 256k (two-block) minimum
-                               (free_space > (patch_data_size * 3 / 2));          // 50% margin of error
+                               (free_space > (size_t)(patch_data_size * 3 / 2));          // 50% margin of error
                }
                if (!enough_space) {
                        LOGL(LOG_SSENGINE, "For %s: free space %ld bytes; enough %d\n", source_filename, (long)free_space,
index bc48247c2f0ee88c41fec5cee6a645784f7f193f..81b7ae53372a350fb20b08becdf109fa47daba79 100644 (file)
@@ -657,7 +657,7 @@ int set_delta_folder(void)
                snprintf(delta_folder, MAX_FOLDER_PATH, ".");   // if no "/" in delta path, assume current directory as delta folder
                return 0;
        }
-       if ((pos - s_update_data.ua_delta_path + 1) == strlen(s_update_data.ua_delta_path)) {
+       if ((size_t)(pos - s_update_data.ua_delta_path + 1) == strlen(s_update_data.ua_delta_path)) {
                print_usage("Invalid delta path");
                return -1;
        }
index 5711e7511452bc04a7c87005ebd02c130509d5cb..41620fb733826eeb5cdffdd702d46853d9f35ea1 100644 (file)
@@ -122,7 +122,7 @@ struct parse_result parse_args(int argc, char **argv)
                        break;
 
                case 3: // kind
-                       if (kind != -1)
+                       if ((int)kind != -1)
                                return (struct parse_result) { .result = PARSE_REPEATED_ARGUMENT };
                        if (strcmp(optarg, "raw") == 0)
                                kind = KIND_RAW;
@@ -170,7 +170,7 @@ struct parse_result parse_args(int argc, char **argv)
        }
 
        if (help) {
-               if (archive != NULL || dest != NULL || archive_file != NULL || patch_orig != NULL || kind != -1)
+               if (archive != NULL || dest != NULL || archive_file != NULL || patch_orig != NULL || (int)kind != -1)
                        return (struct parse_result) { .result = PARSE_REPEATED_ARGUMENT };
                return (struct parse_result) { .result = PARSE_HELP };
        }
@@ -181,7 +181,7 @@ struct parse_result parse_args(int argc, char **argv)
                return (struct parse_result) { .result = PARSE_MISSING_ARGUMENT };
        if (archive_file == NULL)
                return (struct parse_result) { .result = PARSE_MISSING_ARGUMENT };
-       if (kind == -1)
+       if ((int)kind == -1)
                return (struct parse_result) { .result = PARSE_MISSING_ARGUMENT };
 
        if (patch_orig == NULL && kind == KIND_BROTLI_PATCH)
@@ -366,7 +366,7 @@ while_done:;
        SHA1Final(correct_sha1, &context);
 
        char correct_sha1_hex[2 * SHA1_LEN + 1];
-       for (int i = 0; i < SHA1_LEN; ++i) {
+       for (unsigned int i = 0; i < SHA1_LEN; ++i) {
                correct_sha1_hex[2 * i]     = HEX_DIGITS[correct_sha1[i] / 16];
                correct_sha1_hex[2 * i + 1] = HEX_DIGITS[correct_sha1[i] % 16];
        }
index 6f2964083011a52c6bb22a48b0b772f087e26c3a..927ee39f732d3d608557dd59979792207157cdb3 100644 (file)
@@ -82,7 +82,9 @@ static int remmap(struct file_info *finfo, off_t offset)
 
     off_t new_offset = offset & (~(getpagesize()-1));
 
-    if (finfo->len - new_offset > MAX_MMAP_LEN) {
+       assert(finfo->len - new_offset >= 0);
+
+    if ((unsigned long long)(finfo->len - new_offset) > MAX_MMAP_LEN) {
         // If the rest of the file is greather than MAX_MMAP_LEN
         // then we only mmap MAX_MMAP_LEN
         finfo->mmapped_len = MAX_MMAP_LEN;
@@ -138,7 +140,10 @@ static int remmap_and_save_position(struct file_info *finfo)
 static int remmap_if_needed(struct file_info *finfo)
 {
     int res = PF_OK;
-    if (finfo->pos - (uint8_t*)finfo->ptr >= finfo->mmapped_len) {
+
+       assert(finfo->pos - (uint8_t*)finfo->ptr >= 0);
+
+    if ((size_t)(finfo->pos - (uint8_t*)finfo->ptr) >= finfo->mmapped_len) {
         res = remmap_and_save_position(finfo);
     }
     return res;
@@ -186,7 +191,9 @@ static size_t decompress_bytes(struct dec_funcs *funcs, struct bs_data *data, si
                 return PF_ERROR_DECOMPRESSION;
             }
 
-            if (r_read > data->patch_remaining) {
+                       assert(r_read >= 0);
+
+            if ((size_t)r_read > data->patch_remaining) {
                 r_read = data->patch_remaining;
             }
             data->patch_remaining -= r_read;
@@ -319,7 +326,7 @@ int read_header(struct dec_funcs *funcs, struct bs_data *data, uint8_t **buff_ou
                return PF_NOT_ENOUGH_DISK_SPACE;
        }
 
-    if (data->dest.len == -1) {
+    if (data->dest.len == (size_t)-1) {
         data->dest.len = target_size;
         if (ftruncate(data->dest.fd, data->dest.len) == -1) {
             fprintf(stderr, "Cannot change size of the destination file: %m (%d)\n", errno);
index f1264746c1e135d5d6fb95b04274ee2b98d7a4ed..8ac2f8c0fce30f802656f0911bc0c155c920699a 100644 (file)
@@ -17,6 +17,7 @@ A million repetitions of "a"
 
 #define SHA1HANDSOFF
 
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -287,8 +288,10 @@ void SHA1(
     SHA1_CTX ctx;
     unsigned int ii;
 
+    assert(len >= 0);
+
     SHA1Init(&ctx);
-    for (ii=0; ii<len; ii+=1)
+    for (ii=0; ii<(unsigned int)len; ii+=1)
         SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
     SHA1Final((unsigned char *)hash_out, &ctx);
 }