[ipxe-devel] [PATCH] [efi] use correct bound in strncpy to ensure NUL-termination

Bruce Rogers brogers at suse.com
Mon Apr 23 15:42:20 UTC 2018


Using gcc8 with the [-Werror=stringop-truncation] option, 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 ) );
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Specify one less than sizeof the target buffer to avoid this diagnostic.
Since the target buffer is pre-zeroed, the string will be NUL-terminated.

Signed-off-by: Bruce Rogers <brogers at suse.com>
---
 src/util/elf2efi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c
index 6718df77..58460ba2 100644
--- a/src/util/elf2efi.c
+++ b/src/util/elf2efi.c
@@ -494,7 +494,7 @@ 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 ) );
+	strncpy ( ( char * ) new->hdr.Name, name, sizeof ( new->hdr.Name ) -1 );
 	new->hdr.Misc.VirtualSize = section_memsz;
 	new->hdr.VirtualAddress = shdr->sh_addr;
 	new->hdr.SizeOfRawData = section_filesz;
-- 
2.16.3




More information about the ipxe-devel mailing list