All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] MIPS: Loongson: Fix potentially wrong string handling
@ 2011-01-30 20:41 ` Stefan Weil
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Weil @ 2011-01-30 20:41 UTC (permalink / raw)
  To: Ralf Baechle
  Cc: Stefan Weil, Ralf Baechle, Wu Zhangjin, Arnaud Patard, linux-mips,
	linux-kernel

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.

Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Arnaud Patard <apatard@mandriva.com>
Cc: linux-mips@linux-mips.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 arch/mips/loongson/common/machtype.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/arch/mips/loongson/common/machtype.c b/arch/mips/loongson/common/machtype.c
index 81fbe6b..2efd5d9 100644
--- a/arch/mips/loongson/common/machtype.c
+++ b/arch/mips/loongson/common/machtype.c
@@ -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;
@@ -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';
-- 
1.7.2.3

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] MIPS: Loongson: Fix potentially wrong string handling
@ 2011-01-30 20:41 ` Stefan Weil
  0 siblings, 0 replies; 3+ messages in thread
From: Stefan Weil @ 2011-01-30 20:41 UTC (permalink / raw)
  To: Ralf Baechle
  Cc: Stefan Weil, Wu Zhangjin, Arnaud Patard, linux-mips, linux-kernel

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.

Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Arnaud Patard <apatard@mandriva.com>
Cc: linux-mips@linux-mips.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 arch/mips/loongson/common/machtype.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/arch/mips/loongson/common/machtype.c b/arch/mips/loongson/common/machtype.c
index 81fbe6b..2efd5d9 100644
--- a/arch/mips/loongson/common/machtype.c
+++ b/arch/mips/loongson/common/machtype.c
@@ -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;
@@ -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';
-- 
1.7.2.3

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] MIPS: Loongson: Fix potentially wrong string handling
  2011-01-30 20:41 ` Stefan Weil
  (?)
@ 2011-01-31  2:40 ` Ralf Baechle
  -1 siblings, 0 replies; 3+ messages in thread
From: Ralf Baechle @ 2011-01-31  2:40 UTC (permalink / raw)
  To: Stefan Weil; +Cc: Wu Zhangjin, Arnaud Patard, linux-mips, linux-kernel

Applied.  Thanks,

  Ralf

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-01-31  2:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-30 20:41 [PATCH] MIPS: Loongson: Fix potentially wrong string handling Stefan Weil
2011-01-30 20:41 ` Stefan Weil
2011-01-31  2:40 ` Ralf Baechle

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.