[ipxe-devel] [PATCH v1] [build] fix stringop truncation warning with GCC 8.x

Olaf Hering olaf at aepfle.de
Tue Jun 19 08:45:33 UTC 2018


The latest revision of GCC 8.0 adds a -Wstringop-truncation option
to detect common misuses of the strncpy and strncat functions that
may truncate the copy and leave the result without a terminating
nul character.

On its own, the warning cannot distinguish the intended and safe
uses of the functions (to fill a buffer with data) from the
incorrect and unsafe ones (to make a "bounded" copy of a string).
Because the misuses have become prevalent and the correct/intended
uses are in a minority, GCC has added a new attribute called
nonstring to annotate character arrays that aren't meant to be
treated as nul-terminated strings.  The attribute disables
the checker.  In the future, GCC will also use the attribute to
issue warnings when such arrays are passed to functions that
expect nul-terminated strings.

Signed-off-by: Olaf Hering <olaf at aepfle.de>
---

https://sourceware.org/ml/libc-alpha/2017-11/msg00336.html

 src/include/ipxe/efi/IndustryStandard/PeImage.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/include/ipxe/efi/IndustryStandard/PeImage.h b/src/include/ipxe/efi/IndustryStandard/PeImage.h
index 9499bb7f..b4a94aab 100644
--- a/src/include/ipxe/efi/IndustryStandard/PeImage.h
+++ b/src/include/ipxe/efi/IndustryStandard/PeImage.h
@@ -270,6 +270,14 @@ typedef struct {
 #define EFI_IMAGE_SUBSYSTEM_OS2_CUI     5
 #define EFI_IMAGE_SUBSYSTEM_POSIX_CUI   7
 
+#if __GNUC__ >= 8
+/* Describes a char array that is not necessarily a NUL-terminated
+   string.  */
+# define __NONSTRING __attribute__ ((__nonstring__))
+#else
+# define __NONSTRING
+#endif
+
 ///
 /// Length of ShortName.
 ///
@@ -279,6 +287,7 @@ typedef struct {
 /// Section Table. This table immediately follows the optional header.
 ///
 typedef struct {
+  __NONSTRING
   UINT8 Name[EFI_IMAGE_SIZEOF_SHORT_NAME];
   union {
     UINT32  PhysicalAddress;



More information about the ipxe-devel mailing list