Skip to contentSkip to conversation

partitions: Prefer strlcpy() over snprintf()

PATCH In review Jean Delvare 2 participants 3 messages Jul 10, 2008 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 partitions: Prefer strlcpy() over snprintf()

Subject: partitions: Prefer strlcpy() over snprintf()

strlcpy is faster than snprintf when you don't use the returned value.

Cc Andries Brouwer <[email protected]>

---
This patch was already sent on:
* 2008-05-09

 fs/partitions/check.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/partitions/check.c +1 -1
@@ -123,7 +123,7 @@ static int (*check_part[])(struct parsed
 char *disk_name(struct gendisk *hd, int part, char *buf)
 {
 	if (!part)
-		snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
+		strlcpy(buf, hd->disk_name, BDEVNAME_SIZE);
 	else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
 		snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
 	else
Signature · 1 line
Jean Delvare
#1 lore raw

fs/partitions/check.c static int (*check_part[])(struct parsed · line 129

Quoted 10 lines · Jean Delvare

Subject: partitions: Prefer strlcpy() over snprintf()

strlcpy is faster than snprintf when you don't use the returned value.

fs/partitions/check.c +1 -1
 	if (!part)
-		snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
+		strlcpy(buf, hd->disk_name, BDEVNAME_SIZE);
 	else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
 		snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
 	else

Yecch. There is a parallelism between the two cases which makes the source nice and readable. You want to destroy the parallelism for no gain. Different functions, different parameter order. Ach.

#2 lore raw

Hi Andries,

Quoted 5 lines · Andries E. Brouwer
Quoted 3 lines · Jean Delvare

Subject: partitions: Prefer strlcpy() over snprintf()

strlcpy is faster than snprintf when you don't use the returned value.

Quoted 11 lines

fs/partitions/check.c static int (*check_part[])(struct parsed · line 129

Quoted 6 lines
fs/partitions/check.c +1 -1
 	if (!part)
-		snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
+		strlcpy(buf, hd->disk_name, BDEVNAME_SIZE);
 	else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
 		snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
 	else

Yecch. There is a parallelism between the two cases which makes the source nice and readable. You want to destroy the parallelism for no gain. Different functions, different parameter order. Ach.

At the moment I hit "Send" I thought about that and suspected that someone would object for that reason. And that makes sense, indeed. Just scratch this patch, sorry for the noise.

Signature · 1 line
Jean Delvare