[ipxe-devel] [PATCH] elf2efi: silence GCC stringop-truncation warning

Roman Kagan rkagan at virtuozzo.com
Sat Jun 9 14:45:52 UTC 2018


PE section name is an 8-byte string field, null-padded if the string
length is less than the field size.

strncpy is a perfect means to fill it.

However, it triggers -Wstringop-truncation in recent GCC.

To silence it portably, replace strncpy with strlen + memcpy combo.

Signed-off-by: Roman Kagan <rkagan at virtuozzo.com>
---
 src/util/elf2efi.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c
index 6718df77..0057fb7b 100644
--- a/src/util/elf2efi.c
+++ b/src/util/elf2efi.c
@@ -458,6 +458,7 @@ static struct pe_section * process_section ( struct elf_file *elf,
 					     struct pe_header *pe_header ) {
 	struct pe_section *new;
 	const char *name;
+	size_t namesz;
 	size_t section_memsz;
 	size_t section_filesz;
 	unsigned long code_start;
@@ -494,7 +495,11 @@ static struct pe_section * process_section ( struct elf_file *elf,
 	memset ( new, 0, sizeof ( *new ) + section_filesz );
 
 	/* Fill in section header details */
-	strncpy ( ( char * ) new->hdr.Name, name, sizeof ( new->hdr.Name ) );
+	/* PE section name is 8 bytes max, null-padded if less */
+	namesz = strlen ( name );
+	if ( namesz > sizeof ( new->hdr.Name ) )
+		namesz = sizeof ( new->hdr.Name );
+	memcpy ( new->hdr.Name, name, namesz );
 	new->hdr.Misc.VirtualSize = section_memsz;
 	new->hdr.VirtualAddress = shdr->sh_addr;
 	new->hdr.SizeOfRawData = section_filesz;
-- 
2.17.1




More information about the ipxe-devel mailing list