Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/64: Fix strncpy() related build failures with GCC 8.1

GCC 8.1 warns about possible string truncation:

arch/powerpc/kernel/nvram_64.c:1042:2: error: 'strncpy' specified
bound 12 equals destination size [-Werror=stringop-truncation]
strncpy(new_part->header.name, name, 12);

arch/powerpc/platforms/ps3/repository.c:106:2: error: 'strncpy'
output truncated before terminating nul copying 8 bytes from a
string of the same length [-Werror=stringop-truncation]
strncpy((char *)&n, text, 8);

Fix it by using memcpy(). To make that safe we need to ensure the
destination is pre-zeroed. Use kzalloc() in the nvram code and
initialise the u64 to zero in the ps3 code.

Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
[mpe: Use kzalloc() in the nvram code, flesh out change log]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Christophe Leroy and committed by
Michael Ellerman
c9599881 2135a6ec

+4 -4
+2 -2
arch/powerpc/kernel/nvram_64.c
··· 1030 1030 return -ENOSPC; 1031 1031 1032 1032 /* Create our OS partition */ 1033 - new_part = kmalloc(sizeof(*new_part), GFP_KERNEL); 1033 + new_part = kzalloc(sizeof(*new_part), GFP_KERNEL); 1034 1034 if (!new_part) { 1035 1035 pr_err("%s: kmalloc failed\n", __func__); 1036 1036 return -ENOMEM; ··· 1039 1039 new_part->index = free_part->index; 1040 1040 new_part->header.signature = sig; 1041 1041 new_part->header.length = size; 1042 - strncpy(new_part->header.name, name, 12); 1042 + memcpy(new_part->header.name, name, strnlen(name, sizeof(new_part->header.name))); 1043 1043 new_part->header.checksum = nvram_checksum(&new_part->header); 1044 1044 1045 1045 rc = nvram_write_header(new_part);
+2 -2
arch/powerpc/platforms/ps3/repository.c
··· 101 101 102 102 static u64 make_field(const char *text, u64 index) 103 103 { 104 - u64 n; 104 + u64 n = 0; 105 105 106 - strncpy((char *)&n, text, 8); 106 + memcpy((char *)&n, text, strnlen(text, sizeof(n))); 107 107 return n + index; 108 108 } 109 109