From: Youngjae Cho Date: Tue, 23 Apr 2024 09:59:49 +0000 (+0900) Subject: Fix error -Werror=sign-compare X-Git-Tag: accepted/tizen/unified/20240424.063531~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=86a329a668b7cc08176bf1f8c3d72652b6fe6caa;p=platform%2Fcore%2Fsystem%2Fupgrade.git Fix error -Werror=sign-compare 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 --- diff --git a/src/img-verifier/img-verifier.c b/src/img-verifier/img-verifier.c index 95ae7a7..375d742 100644 --- a/src/img-verifier/img-verifier.c +++ b/src/img-verifier/img-verifier.c @@ -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; } diff --git a/src/upgrade-apply-deltafs/engine/SS_FSUpdate.c b/src/upgrade-apply-deltafs/engine/SS_FSUpdate.c index c87205d..dc946c6 100644 --- a/src/upgrade-apply-deltafs/engine/SS_FSUpdate.c +++ b/src/upgrade-apply-deltafs/engine/SS_FSUpdate.c @@ -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'; diff --git a/src/upgrade-apply-deltafs/engine/SS_PatchDelta.c b/src/upgrade-apply-deltafs/engine/SS_PatchDelta.c index 9346bc6..cf88b6e 100644 --- a/src/upgrade-apply-deltafs/engine/SS_PatchDelta.c +++ b/src/upgrade-apply-deltafs/engine/SS_PatchDelta.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -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, diff --git a/src/upgrade-apply-deltafs/ua.c b/src/upgrade-apply-deltafs/ua.c index bc48247..81b7ae5 100644 --- a/src/upgrade-apply-deltafs/ua.c +++ b/src/upgrade-apply-deltafs/ua.c @@ -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; } diff --git a/src/upgrade-apply/main.c b/src/upgrade-apply/main.c index 5711e75..41620fb 100644 --- a/src/upgrade-apply/main.c +++ b/src/upgrade-apply/main.c @@ -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]; } diff --git a/src/upgrade-apply/patch/patch.c b/src/upgrade-apply/patch/patch.c index 6f29640..927ee39 100644 --- a/src/upgrade-apply/patch/patch.c +++ b/src/upgrade-apply/patch/patch.c @@ -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); diff --git a/src/upgrade-apply/sha1/sha1.c b/src/upgrade-apply/sha1/sha1.c index f126474..8ac2f8c 100644 --- a/src/upgrade-apply/sha1/sha1.c +++ b/src/upgrade-apply/sha1/sha1.c @@ -17,6 +17,7 @@ A million repetitions of "a" #define SHA1HANDSOFF +#include #include #include @@ -287,8 +288,10 @@ void SHA1( SHA1_CTX ctx; unsigned int ii; + assert(len >= 0); + SHA1Init(&ctx); - for (ii=0; ii