Skip to contentSkip to conversation

bcache-tools: fix strncpy compiler warning in replace_line()

PATCH In review Shaoxiong Li 2 participants 2 messages Jun 24, 2025 bcache (block layer cache)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcache-tools: fix strncpy compiler warning in replace_line()

The strncpy() call in replace_line() was using strlen(src) as the size parameter instead of the destination buffer size, causing a compiler warning about potential string truncation. use snprintf() instead.

---
 bcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
bcache.c +4 -4
index f99d2dc..47d45e9 100644
@@ -142,7 +142,7 @@ int setlabel_usage(void)
 	return EXIT_FAILURE;
 }
 
-int version_usagee(void)
+int version_usage(void)
 {
 	fprintf(stderr,
 		"Usage: version		display software version\n");
@@ -157,7 +157,7 @@ void replace_line(char **dest, const char *from, const char *to)
 
 	strcpy(sub, *dest);
 	while (1) {
-		char *tmp = strpbrk(sub, from);
+		char *tmp = strstr(sub, from);
 
 		if (tmp != NULL) {
 			strcpy(new, tmp);
@@ -166,7 +166,7 @@ void replace_line(char **dest, const char *from, const char *to)
 			break;
 	}
 	if (strlen(new) > 0) {
-		strncpy(new, to, strlen(to));
+		snprintf(new, sizeof(new), "%s", to);
 		sprintf(*dest + strlen(*dest) - strlen(new), new, strlen(new));
 	}
 }
@@ -453,7 +453,7 @@ int main(int argc, char **argv)
 		return set_label(devname, argv[2]);
 	} else if (strcmp(subcmd, "version") == 0) {
 		if (argc != 1)
-			return version_usagee();
+			return version_usage();
 		printf("bcache-tools %s\n", BCACHE_TOOLS_VERSION);
 
 		return 0;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Shaoxiong Li

The strncpy() call in replace_line() was using strlen(src) as the size parameter instead of the destination buffer size, causing a compiler warning about potential string truncation. use snprintf() instead.

Thanks for the fixup. Applied.

Quoted 47 lines
---
 bcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
bcache.c +4 -4
index f99d2dc..47d45e9 100644
@@ -142,7 +142,7 @@ int setlabel_usage(void)
 	return EXIT_FAILURE;
 }
 
-int version_usagee(void)
+int version_usage(void)
 {
 	fprintf(stderr,
 		"Usage: version		display software version\n");
@@ -157,7 +157,7 @@ void replace_line(char **dest, const char *from, const char *to)
 
 	strcpy(sub, *dest);
 	while (1) {
-		char *tmp = strpbrk(sub, from);
+		char *tmp = strstr(sub, from);
 
 		if (tmp != NULL) {
 			strcpy(new, tmp);
@@ -166,7 +166,7 @@ void replace_line(char **dest, const char *from, const char *to)
 			break;
 	}
 	if (strlen(new) > 0) {
-		strncpy(new, to, strlen(to));
+		snprintf(new, sizeof(new), "%s", to);
 		sprintf(*dest + strlen(*dest) - strlen(new), new, strlen(new));
 	}
 }
@@ -453,7 +453,7 @@ int main(int argc, char **argv)
 		return set_label(devname, argv[2]);
 	} else if (strcmp(subcmd, "version") == 0) {
 		if (argc != 1)
-			return version_usagee();
+			return version_usage();
 		printf("bcache-tools %s\n", BCACHE_TOOLS_VERSION);
 
 		return 0;
Signature · 1 line
2.43.0
Signature · 1 line
Coly Li