[02/25] examples: use strlcpy instead of snprintf

Message ID 20230601150106.18375-3-stephen@networkplumber.org (mailing list archive)
State Rejected, archived
Delegated to: Thomas Monjalon
Headers
Series replace snprintf with strlcpy |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger June 1, 2023, 3 p.m. UTC
  Replace snprintf() with strlcpy() where possible.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 examples/fips_validation/fips_validation.c | 8 ++++----
 examples/l3fwd-graph/main.c                | 2 +-
 examples/pipeline/cli.c                    | 2 +-
 examples/vhost_blk/vhost_blk.c             | 5 ++---
 4 files changed, 8 insertions(+), 9 deletions(-)
  

Patch

diff --git a/examples/fips_validation/fips_validation.c b/examples/fips_validation/fips_validation.c
index f840804009ed..6e913898074d 100644
--- a/examples/fips_validation/fips_validation.c
+++ b/examples/fips_validation/fips_validation.c
@@ -505,8 +505,8 @@  fips_test_parse_one_json_group(void)
 
 			switch (json_typeof(param)) {
 			case JSON_STRING:
-				snprintf(json_value, sizeof(json_value), "%s",
-						 json_string_value(param));
+				strlcpy(json_value, json_string_value(param),
+					sizeof(json_value));
 				break;
 
 			case JSON_INTEGER:
@@ -550,8 +550,8 @@  fips_test_parse_one_json_case(void)
 
 		switch (json_typeof(param)) {
 		case JSON_STRING:
-			snprintf(info.one_line_text, MAX_LINE_CHAR, "%s",
-					 json_string_value(param));
+			strlcpy(info.one_line_text, json_string_value(param),
+				MAX_LINE_CHAR);
 			break;
 
 		case JSON_INTEGER:
diff --git a/examples/l3fwd-graph/main.c b/examples/l3fwd-graph/main.c
index 5feeab4f0fb0..ee8f247108ad 100644
--- a/examples/l3fwd-graph/main.c
+++ b/examples/l3fwd-graph/main.c
@@ -546,7 +546,7 @@  parse_args(int argc, char **argv)
 			break;
 
 		case CMD_LINE_OPT_PCAP_FILENAME_CAP:
-			rte_strlcpy(pcap_filename, optarg,
+			strlcpy(pcap_filename, optarg,
 				    sizeof(pcap_filename));
 			printf("Pcap file name: %s\n", pcap_filename);
 			break;
diff --git a/examples/pipeline/cli.c b/examples/pipeline/cli.c
index 2ae6cc579ff2..514b6d779212 100644
--- a/examples/pipeline/cli.c
+++ b/examples/pipeline/cli.c
@@ -3164,7 +3164,7 @@  cmd_ipsec_sa_add(char **tokens,
 			goto free;
 		}
 
-		snprintf(out, out_size, "%s", line);
+		strlcpy(out, line, out_size);
 		out_size -= strlen(out);
 		out += strlen(out);
 
diff --git a/examples/vhost_blk/vhost_blk.c b/examples/vhost_blk/vhost_blk.c
index 3709d7ed06ba..3b05df390cce 100644
--- a/examples/vhost_blk/vhost_blk.c
+++ b/examples/vhost_blk/vhost_blk.c
@@ -771,9 +771,8 @@  vhost_blk_bdev_construct(const char *bdev_name,
 	if (!bdev)
 		return NULL;
 
-	snprintf(bdev->name, sizeof(bdev->name), "%s", bdev_name);
-	snprintf(bdev->product_name, sizeof(bdev->product_name), "%s",
-		 bdev_serial);
+	strlcpy(bdev->name, bdev_name, sizeof(bdev->name));
+	strlcpy(bdev->product_name, bdev_serial, sizeof(bdev->product_name));
 	bdev->blocklen = blk_size;
 	bdev->blockcnt = blk_cnt;
 	bdev->write_cache = wce_enable;