[ipxe-devel] [PATCH] [efi] Guard strncpy with gcc warning ignore pragma

Bernhard M. Wiedemann bwiedemann at suse.de
Mon Jun 18 12:22:14 UTC 2018


From: Bruce Rogers <brogers at suse.com>

Using gcc 8 with the -Wstringop-truncation option, and with warnings
treated as errors, the following error is emitted:

util/elf2efi.c:494:2: error: 'strncpy' specified bound 8 equals destination
size [-Werror=stringop-truncation]
  strncpy ( ( char * ) new->hdr.Name, name, sizeof ( new->hdr.Name ) );
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use gcc pragmas surrounding this line of code which avoid this warning. The
strncpy usage here is correct since the destination should be null-padded
but not null-terminated.

[BR: BSC#1090355]
Signed-off-by: Bruce Rogers <brogers at suse.com>
Acked-by: Bernhard M. Wiedemann <bwiedemann at suse.de>

---
In http://lists.ipxe.org/pipermail/ipxe-devel/2018-April/006144.html

Michael Brown wrote:
> I'll happily accept patches to alter gcc's behaviour so that it does not
> report false positive warnings for these 100% correct usages of
> strncpy().

So this patch (pulled from openSUSE's qemu package) seems most appropriate.
---
 src/util/elf2efi.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c
index 6718df77..93cdda03 100644
--- a/src/util/elf2efi.c
+++ b/src/util/elf2efi.c
@@ -494,7 +494,13 @@ static struct pe_section * process_section ( struct elf_file *elf,
 	memset ( new, 0, sizeof ( *new ) + section_filesz );
 
 	/* Fill in section header details */
+/* gcc 8 warning gives false positive here - our usage is correct */
+#pragma GCC diagnostic push
+#if __GNUC__ >= 8
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+#endif
 	strncpy ( ( char * ) new->hdr.Name, name, sizeof ( new->hdr.Name ) );
+#pragma GCC diagnostic pop
 	new->hdr.Misc.VirtualSize = section_memsz;
 	new->hdr.VirtualAddress = shdr->sh_addr;
 	new->hdr.SizeOfRawData = section_filesz;
-- 
2.13.7




More information about the ipxe-devel mailing list