]> granicus.if.org Git - vnstat/commitdiff
misc.c: silence some safe implicit conversions
authorChristian Göttsche <cgzones@googlemail.com>
Sat, 5 Oct 2019 15:38:20 +0000 (17:38 +0200)
committerChristian Göttsche <cgzones@googlemail.com>
Sat, 5 Oct 2019 15:38:20 +0000 (17:38 +0200)
Convert strlen(getrateunitprefix(...)) explicit to int as it is safe to be small

UBSan complains:

    src/misc.c:312:4: runtime error: implicit conversion from type 'unsigned long' of value 18446744073709551610 (64-bit, unsigned) to type 'int' changed the value to -6 (32-bit, signed)

Found by clang's UndefinedBehaviorSanitizer

src/misc.c

index 556d19af306689e93989979fc4c848a971981f7d..d0727642e8346a1ffc18b3f7da2b19c4e6497f5b 100644 (file)
@@ -192,7 +192,7 @@ int getunitspacing(const int len, const int index)
 
        /* tune spacing according to unit */
        /* +1 for space between number and unit */
-       l -= strlen(getunitprefix(index)) + 1;
+       l -= (int) strlen(getunitprefix(index)) + 1;
        if (l < 0) {
                l = 1;
        }
@@ -309,7 +309,7 @@ int getratespacing(const int len, const int unitmode, const int unitindex)
 {
        int l = len;
 
-       l -= strlen(getrateunitprefix(unitmode, unitindex)) + 1;
+       l -= (int) strlen(getrateunitprefix(unitmode, unitindex)) + 1;
        if (l < 0) {
                l = 1;
        }