Fix error -Werror=sign-compare 44/310244/2 accepted/tizen/unified/20240425.114243 accepted/tizen/unified/toolchain/20240427.045653 accepted/tizen/unified/x/20240426.050516
authorYoungjae Cho <y0.cho@samsung.com>
Wed, 24 Apr 2024 09:31:55 +0000 (18:31 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Wed, 24 Apr 2024 10:20:19 +0000 (19:20 +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: Iaf2c9b84dc4fcfc5ae183cb2f2f5ee56de3985b0
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
delta-verifier/delta-verifier.c
update-manager/common/sha1.c

index f3e1a34c076a93c6c5575daf59108cc16bfa4bb8..00e6ec5ca028ffab675269ba9324d3d95dba95a8 100644 (file)
@@ -185,7 +185,7 @@ static int write_info_from_file(FILE* read_file, FILE* write_file)
                                        char lowercase_name[MAX_STRING];
                                        strncpy(lowercase_name, full_var_names[i], MAX_STRING);
                                        lowercase_name[MAX_STRING - 1] = '\0';
-                                       for (int j = 0; j < strlen(lowercase_name); j++)
+                                       for (size_t j = 0; j < strlen(lowercase_name); j++)
                                                lowercase_name[j] = tolower(lowercase_name[j]);
 
                                        char to_write[MAX_STRING];
@@ -214,7 +214,7 @@ static int get_newline_count(char* buff)
 {
        int count = 0;
 
-       for (int i = 0; i < strlen(buff); i++)
+       for (size_t i = 0; i < strlen(buff); i++)
        {
                if (buff[i] == '\n')
                        count++;
index fe8da835be0550f1fb25d6f120ee36c52e1f4953..dafcf6fb15cc3266dddc7b1b84a6dd086b06fa53 100644 (file)
@@ -19,6 +19,7 @@ A million repetitions of "a"
 
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 /* for uint32_t */
 #include <stdint.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);
 }