Skip to contentSkip to conversation

Utils: Change sprintf to snprintf

PATCH Applied Mateusz Kusiak 2 participants 2 messages Aug 12, 2021 – Aug 13, 2021 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Utils: Change sprintf to snprintf

Using sprintf can cause segmentation fault by exceeding the size of buffer array.

---
 util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util.c +2 -2
index cdf1da24..ea07277c 100644
@@ -947,12 +947,12 @@ dev_t devnm2devid(char *devnm)
 	/* First look in /sys/block/$DEVNM/dev for %d:%d
 	 * If that fails, try parsing out a number
 	 */
-	char path[100];
+	char path[PATH_MAX];
 	char *ep;
 	int fd;
 	int mjr,mnr;
 
-	sprintf(path, "/sys/block/%s/dev", devnm);
+	snprintf(path, sizeof(path), "/sys/block/%s/dev", devnm);
 	fd = open(path, O_RDONLY);
 	if (fd >= 0) {
 		char buf[20];
Signature · 1 line
2.26.2
Applied
#1 lore raw
Quoted 6 lines · Mateusz Kusiak

Using sprintf can cause segmentation fault by exceeding the size of buffer array.

---
 util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied, thanks!

Jes