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

MIPS: Loongson: Fix potentially wrong string handling

This error was reported by cppcheck:
arch/mips/loongson/common/machtype.c:56: error: Dangerous usage of 'str' (strncpy doesn't always 0-terminate it)

If strncpy copied MACHTYPE_LEN bytes, the destination string str
was not terminated.

The patch adds one more byte to str and makes sure that this byte is
always 0.

Signed-off-by: Stefan Weil <weil@mail.berlios.de>
Cc: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Arnaud Patard <apatard@mandriva.com>
Cc: linux-mips@linux-mips.org
Cc: linux-kernel@vger.kernel.org
Patchwork: https://patchwork.linux-mips.org/patch/2053/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>

authored by

Stefan Weil and committed by
Ralf Baechle
994fed2d d3ce0e98

+2 -1
+2 -1
arch/mips/loongson/common/machtype.c
··· 41 41 42 42 void __init prom_init_machtype(void) 43 43 { 44 - char *p, str[MACHTYPE_LEN]; 44 + char *p, str[MACHTYPE_LEN + 1]; 45 45 int machtype = MACH_LEMOTE_FL2E; 46 46 47 47 mips_machtype = LOONGSON_MACHTYPE; ··· 53 53 } 54 54 p += strlen("machtype="); 55 55 strncpy(str, p, MACHTYPE_LEN); 56 + str[MACHTYPE_LEN] = '\0'; 56 57 p = strstr(str, " "); 57 58 if (p) 58 59 *p = '\0';