]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/GenVtf: silence false "stringop-overflow" warning with memcpy()
authorLaszlo Ersek <lersek@redhat.com>
Fri, 2 Mar 2018 16:11:52 +0000 (17:11 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Mon, 5 Mar 2018 21:38:35 +0000 (22:38 +0100)
gcc-8 (which is part of Fedora 28) enables the new warning
"-Wstringop-overflow" in "-Wall". This warning is documented in detail at
<https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html>; the
introduction says

> Warn for calls to string manipulation functions such as memcpy and
> strcpy that are determined to overflow the destination buffer.

It breaks the BaseTools build with:

> GenVtf.c: In function 'ConvertVersionInfo':
> GenVtf.c:132:7: error: 'strncpy' specified bound depends on the length
> of the source argument [-Werror=stringop-overflow=]
>        strncpy (TemStr + 4 - Length, Str, Length);
>        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> GenVtf.c:130:14: note: length computed here
>      Length = strlen(Str);
>               ^~~~~~~~~~~

It is a false positive because, while the bound equals the length of the
source argument, the destination pointer is moved back towards the
beginning of the destination buffer by the same amount (and this amount is
range-checked first, so we can't precede the start of the dest buffer).

Replace both strncpy() calls with memcpy().

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Cole Robinson <crobinso@redhat.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Reported-by: Cole Robinson <crobinso@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/C/GenVtf/GenVtf.c

index 65ae08eeceb80482497952c84d13fec60c2b04a3..fc7ae02203ff9716133f65c46bab7401eb086dec 100644 (file)
@@ -129,9 +129,9 @@ Returns:
   } else {\r
     Length = strlen(Str);\r
     if (Length < 4) {\r
-      strncpy (TemStr + 4 - Length, Str, Length);\r
+      memcpy (TemStr + 4 - Length, Str, Length);\r
     } else {\r
-      strncpy (TemStr, Str + Length - 4, 4);\r
+      memcpy (TemStr, Str + Length - 4, 4);\r
     }\r
   \r
     sscanf (\r