Linux bcache driver list
 help / color / mirror / Atom feed
* [PATCH] bcache-tools: fix strncpy compiler warning in replace_line()
@ 2025-06-24 12:04 Shaoxiong Li
  0 siblings, 0 replies; 3+ messages in thread
From: Shaoxiong Li @ 2025-06-24 12:04 UTC (permalink / raw)
  To: colyli; +Cc: linux-bcache

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.

Signed-off-by: Shaoxiong Li <dahefanteng@gmail.com>
---
 bcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bcache.c b/bcache.c
index f99d2dc..47d45e9 100644
--- a/bcache.c
+++ b/bcache.c
@@ -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;
-- 
2.43.0


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

* [PATCH] bcache-tools: fix strncpy compiler warning in replace_line()
@ 2025-06-24 12:19 Shaoxiong Li
  2025-06-24 12:32 ` Coly Li
  0 siblings, 1 reply; 3+ messages in thread
From: Shaoxiong Li @ 2025-06-24 12:19 UTC (permalink / raw)
  To: colyli; +Cc: linux-bcache

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.

Signed-off-by: Shaoxiong Li <dahefanteng@gmail.com>
---
 bcache.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bcache.c b/bcache.c
index f99d2dc..47d45e9 100644
--- a/bcache.c
+++ b/bcache.c
@@ -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;
-- 
2.43.0


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

* Re: [PATCH] bcache-tools: fix strncpy compiler warning in replace_line()
  2025-06-24 12:19 [PATCH] bcache-tools: fix strncpy compiler warning in replace_line() Shaoxiong Li
@ 2025-06-24 12:32 ` Coly Li
  0 siblings, 0 replies; 3+ messages in thread
From: Coly Li @ 2025-06-24 12:32 UTC (permalink / raw)
  To: Shaoxiong Li; +Cc: linux-bcache

On Tue, Jun 24, 2025 at 08:19:40PM +0800, Shaoxiong Li wrote:
> 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.
> 
> Signed-off-by: Shaoxiong Li <dahefanteng@gmail.com>

Thanks for the fixup. Applied.

> ---
>  bcache.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/bcache.c b/bcache.c
> index f99d2dc..47d45e9 100644
> --- a/bcache.c
> +++ b/bcache.c
> @@ -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;
> -- 
> 2.43.0
> 

-- 
Coly Li

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

end of thread, other threads:[~2025-06-24 12:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-24 12:19 [PATCH] bcache-tools: fix strncpy compiler warning in replace_line() Shaoxiong Li
2025-06-24 12:32 ` Coly Li
  -- strict thread matches above, loose matches on Subject: below --
2025-06-24 12:04 Shaoxiong Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox