[PATCH 01/25] app: use strlcpy in tests
Stephen Hemminger
stephen at networkplumber.org
Thu Jun 1 17:00:42 CEST 2023
Prefer using strlcpy over snprintf().
Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
app/test-bbdev/main.c | 5 ++---
app/test-pmd/cmdline_flow.c | 6 +++---
app/test/process.h | 2 +-
app/test/test_graph_perf.c | 29 ++++++++++++++++-------------
4 files changed, 22 insertions(+), 20 deletions(-)
diff --git a/app/test-bbdev/main.c b/app/test-bbdev/main.c
index ec830eb32bc8..b1f2d79f4b07 100644
--- a/app/test-bbdev/main.c
+++ b/app/test-bbdev/main.c
@@ -251,9 +251,8 @@ parse_args(int argc, char **argv, struct test_params *tp)
TEST_ASSERT(strlen(optarg) > 0,
"Config file name is null");
- snprintf(tp->test_vector_filename,
- sizeof(tp->test_vector_filename),
- "%s", optarg);
+ strlcpy(tp->test_vector_filename, optarg,
+ sizeof(tp->test_vector_filename));
break;
case 's':
TEST_ASSERT(strlen(optarg) > 0,
diff --git a/app/test-pmd/cmdline_flow.c b/app/test-pmd/cmdline_flow.c
index 58939ec321d6..40f67d126b4b 100644
--- a/app/test-pmd/cmdline_flow.c
+++ b/app/test-pmd/cmdline_flow.c
@@ -10028,7 +10028,7 @@ parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
char tmp[3], *end = tmp;
uint32_t read_lim = left & 1 ? 1 : 2;
- snprintf(tmp, read_lim + 1, "%s", src);
+ strlcpy(tmp, src, read_lim + 1);
*dst = strtoul(tmp, &end, 16);
if (*end) {
*dst = 0;
@@ -11404,7 +11404,7 @@ cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
if (!size)
return -1;
/* Set token type and update global help with details. */
- strlcpy(dst, (token->type ? token->type : "TOKEN"), size);
+ strlcpy(dst, token->type ? : "TOKEN", size);
if (token->help)
cmd_flow.help_str = token->help;
else
@@ -12220,7 +12220,7 @@ cmd_set_raw_get_help(cmdline_parse_token_hdr_t *hdr, char *dst,
if (!size)
return -1;
/* Set token type and update global help with details. */
- snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
+ strlcpy(dst, token->type ? : "TOKEN", size);
if (token->help)
cmd_set_raw.help_str = token->help;
else
diff --git a/app/test/process.h b/app/test/process.h
index 1f073b9c5c2a..e2bdecb9c807 100644
--- a/app/test/process.h
+++ b/app/test/process.h
@@ -176,7 +176,7 @@ get_current_prefix(char *prefix, int size)
return NULL;
/* get the prefix */
- snprintf(prefix, size, "%s", basename(dirname(buf)));
+ strlcpy(prefix, basename(dirname(buf)), size);
return prefix;
}
diff --git a/app/test/test_graph_perf.c b/app/test/test_graph_perf.c
index c5b463f700cd..521d4077600d 100644
--- a/app/test/test_graph_perf.c
+++ b/app/test/test_graph_perf.c
@@ -294,8 +294,9 @@ graph_node_count_edges(uint32_t stage, uint16_t node, uint16_t nodes_per_stage,
for (i = 0; i < nodes_per_stage && edges < MAX_EDGES_PER_NODE; i++) {
if (edge_map[stage + 1][i][node]) {
ename[edges] = malloc(sizeof(char) * RTE_NODE_NAMESIZE);
- snprintf(ename[edges], RTE_NODE_NAMESIZE, "%s",
- rte_node_id_to_name(node_map[stage + 1][i]));
+ strlcpy(ename[edges],
+ rte_node_id_to_name(node_map[stage + 1][i]),
+ RTE_NODE_NAMESIZE);
node_data->next_nodes[edges] = node_map[stage + 1][i];
node_data->next_percentage[edges] =
edge_map[stage + 1][i][node];
@@ -405,9 +406,9 @@ graph_init(const char *gname, uint8_t nb_srcs, uint8_t nb_sinks,
graph_data->nb_nodes++;
goto pattern_name_free;
}
- snprintf(node_patterns[graph_data->nb_nodes],
- RTE_NODE_NAMESIZE, "%s",
- rte_node_id_to_name(node_map[i][j]));
+ strlcpy(node_patterns[graph_data->nb_nodes],
+ rte_node_id_to_name(node_map[i][j]),
+ RTE_NODE_NAMESIZE);
node_data =
&graph_data->node_data[graph_data->nb_nodes];
node_data->node_id = node_map[i][j];
@@ -467,8 +468,8 @@ graph_init(const char *gname, uint8_t nb_srcs, uint8_t nb_sinks,
graph_data->nb_nodes++;
goto pattern_name_free;
}
- snprintf(node_patterns[graph_data->nb_nodes], RTE_NODE_NAMESIZE,
- "%s", rte_node_id_to_name(src_nodes[i]));
+ strlcpy(node_patterns[graph_data->nb_nodes],
+ rte_node_id_to_name(src_nodes[i]), RTE_NODE_NAMESIZE);
node_data = &graph_data->node_data[graph_data->nb_nodes];
node_data->node_id = src_nodes[i];
node_data->is_sink = false;
@@ -479,8 +480,9 @@ graph_init(const char *gname, uint8_t nb_srcs, uint8_t nb_sinks,
if (!src_map[i][j])
continue;
ename[edges] = malloc(sizeof(char) * RTE_NODE_NAMESIZE);
- snprintf(ename[edges], RTE_NODE_NAMESIZE, "%s",
- rte_node_id_to_name(node_map[0][j]));
+ strlcpy(ename[edges],
+ rte_node_id_to_name(node_map[0][j]),
+ RTE_NODE_NAMESIZE);
node_data->next_nodes[edges] = node_map[0][j];
node_data->next_percentage[edges] = src_map[i][j];
edges++;
@@ -524,8 +526,8 @@ graph_init(const char *gname, uint8_t nb_srcs, uint8_t nb_sinks,
graph_data->nb_nodes++;
goto pattern_name_free;
}
- snprintf(node_patterns[graph_data->nb_nodes], RTE_NODE_NAMESIZE,
- "%s", rte_node_id_to_name(snk_nodes[i]));
+ strlcpy(node_patterns[graph_data->nb_nodes],
+ rte_node_id_to_name(snk_nodes[i]), RTE_NODE_NAMESIZE);
node_data = &graph_data->node_data[graph_data->nb_nodes];
node_data->node_id = snk_nodes[i];
node_data->is_sink = true;
@@ -543,8 +545,9 @@ graph_init(const char *gname, uint8_t nb_srcs, uint8_t nb_sinks,
if (!snk_map[i][j])
continue;
ename[edges] = malloc(sizeof(char) * RTE_NODE_NAMESIZE);
- snprintf(ename[edges], RTE_NODE_NAMESIZE, "%s",
- rte_node_id_to_name(snk_nodes[j]));
+ strlcpy(ename[edges],
+ rte_node_id_to_name(snk_nodes[j]),
+ RTE_NODE_NAMESIZE);
node_data->next_nodes[edges] = snk_nodes[j];
node_data->next_percentage[edges] = snk_map[i][j];
edges++;
--
2.39.2
More information about the dev
mailing list