Skip to content

Commit

Permalink
MIPS: Loongson: Fix potentially wrong string handling
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Stefan Weil authored and Ralf Baechle committed Mar 14, 2011
1 parent d3ce0e9 commit 994fed2
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion arch/mips/loongson/common/machtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void __weak __init mach_prom_init_machtype(void)

void __init prom_init_machtype(void)
{
char *p, str[MACHTYPE_LEN];
char *p, str[MACHTYPE_LEN + 1];
int machtype = MACH_LEMOTE_FL2E;

mips_machtype = LOONGSON_MACHTYPE;
Expand All @@ -53,6 +53,7 @@ void __init prom_init_machtype(void)
}
p += strlen("machtype=");
strncpy(str, p, MACHTYPE_LEN);
str[MACHTYPE_LEN] = '\0';
p = strstr(str, " ");
if (p)
*p = '\0';
Expand Down

0 comments on commit 994fed2

Please sign in to comment.