Skip to contentSkip to conversation

PPC: in celleb_show_cpuinfo() convert strncpy(x, y, sizeof(x)) to strlcpy

PATCH In review Roel Kluin 3 participants 4 messages Mar 6, 2008 – Mar 7, 2008 PowerPC

lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

PATCH 1/1 PPC: in celleb_show_cpuinfo() convert strncpy(x, y, sizeof(x)) to strlcpy

This patch was not yet tested. Please confirm it's right.
---
strncpy does not append '\0' if the length of the source string equals the size parameter, strlcpy does.

---

arch/powerpc/platforms/celleb/setup.c +1 -1
index f27ae1e..d70fc53 100644
@@ -81,7 +81,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
 	return 0;
 }
author
#1 lore raw
Quoted 1 line · Roel Kluin

This patch was not yet tested. Please confirm it's right.

was too quick with the send button. the batch below is probably better
---
strncpy does not append '\0' if the length of the source string equals the size parameter, strlcpy does.

---

arch/powerpc/platforms/celleb/setup.c +1 -2
index f27ae1e..cbe09d9 100644
@@ -81,8 +81,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
-	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	return 0;
 }
 
#3 lore raw
In reply to Roel Kluin
Quoted 4 lines

This patch was not yet tested. Please confirm it's right.
---
strncpy does not append '\0' if the length of the source string equals the size parameter, strlcpy does.

strlcpy() doesn't fill the rest of the array with zeroes, strncpy() does. This could matter (information leak, for example), although I doubt it does here.

arch/powerpc/platforms/celleb/setup.c static void celleb_show_cpuinfo(struct seq_file *m) · line 85

Quoted 3 lines
arch/powerpc/platforms/celleb/setup.c +1 -1
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;

That last statement makes the strncpy() case always zero-terminated, so this patch isn't necessary. Oh, and neither the old nor the new code handles the case where the string doesn't fit (other than just cutting it short); is that the wanted behaviour?

Segher

Acked-by
#2 lore raw
In reply to Roel Kluin
Quoted 9 lines · Roel Kluin
Quoted 1 line · Roel Kluin

This patch was not yet tested. Please confirm it's right.

was too quick with the send button. the batch below is probably better
---
strncpy does not append '\0' if the length of the source string equals the size parameter, strlcpy does.

I tested Roel's 2nd patch and it works good on Celleb.

Quoted 19 lines

---

arch/powerpc/platforms/celleb/setup.c +1 -2
index f27ae1e..cbe09d9 100644
@@ -81,8 +81,7 @@ static void celleb_show_cpuinfo(struct seq_file *m)
 
 static int __init celleb_machine_type_hack(char *ptr)
 {
-	strncpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
-	celleb_machine_type[sizeof(celleb_machine_type)-1] = 0;
+	strlcpy(celleb_machine_type, ptr, sizeof(celleb_machine_type));
 	return 0;
 }
 

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://ozlabs.org/mailman/listinfo/linuxppc-dev