Browse Source

Fix VS2019 Code Analysis warnings

When compiling for x64, Visual Studio 2019's Code Analysis produces the following warnings:

C:\Projects\gnu-efi\lib\print.c(1380): warning C26451: Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '+' to avoid overflow (io.2).
C:\Projects\gnu-efi\lib\smbios.c(47): warning C26451: Arithmetic overflow: Using operator '+' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '+' to avoid overflow (io.2).
C:\Projects\gnu-efi\lib\str.c(289): warning C26451: Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).

Fix these by adding an explicit cast to UINTN.
Pete Batard 3 years ago
parent
commit
4ef183353c
3 changed files with 3 additions and 3 deletions
  1. 1 1
      lib/print.c
  2. 1 1
      lib/smbios.c
  3. 1 1
      lib/str.c

+ 1 - 1
lib/print.c

@@ -1377,7 +1377,7 @@ ValueToString (
         *(p1++) = (CHAR8)r + '0';
         *(p1++) = (CHAR8)r + '0';
     }
     }
 
 
-    c = (Comma ? ca[(p1 - str) % 3] : 999) + 1;
+    c = (UINTN) (Comma ? ca[(p1 - str) % 3] : 999) + 1;
     while (p1 != str) {
     while (p1 != str) {
 
 
         c -= 1;
         c -= 1;

+ 1 - 1
lib/smbios.c

@@ -44,7 +44,7 @@ LibGetSmbiosSystemGuidAndSerialNumber (
     }
     }
 
 
     Smbios.Hdr = (SMBIOS_HEADER *)SmbiosTable->TableAddress;
     Smbios.Hdr = (SMBIOS_HEADER *)SmbiosTable->TableAddress;
-    SmbiosEnd.Raw = (UINT8 *)(SmbiosTable->TableAddress + SmbiosTable->TableLength);
+    SmbiosEnd.Raw = (UINT8 *)((UINTN)SmbiosTable->TableAddress + SmbiosTable->TableLength);
     for (Index = 0; Index < SmbiosTable->TableLength ; Index++) {
     for (Index = 0; Index < SmbiosTable->TableLength ; Index++) {
         if (Smbios.Hdr->Type == 1) {
         if (Smbios.Hdr->Type == 1) {
             if (Smbios.Hdr->Length < 0x19) {
             if (Smbios.Hdr->Length < 0x19) {

+ 1 - 1
lib/str.c

@@ -286,7 +286,7 @@ xtoi (
         }
         }
 
 
         if ((c >= '0'  &&  c <= '9')  ||  (c >= 'A'  &&  c <= 'F')) {
         if ((c >= '0'  &&  c <= '9')  ||  (c >= 'A'  &&  c <= 'F')) {
-            u = (u << 4)  |  (c - (c >= 'A' ? 'A'-10 : '0'));
+            u = (u << 4)  |  ((UINTN)c - (c >= 'A' ? 'A'-10 : '0'));
         } else {
         } else {
             break;
             break;
         }
         }