convert snprintf to strlcpy
authorBruce Richardson <bruce.richardson@intel.com>
Mon, 12 Mar 2018 11:33:00 +0000 (11:33 +0000)
committerThomas Monjalon <thomas@monjalon.net>
Wed, 4 Apr 2018 15:33:08 +0000 (17:33 +0200)
Since we have support for the strlcpy function in DPDK, replace all
instances where a string is copied using snprintf.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
21 files changed:
app/pdump/main.c
app/test-pmd/parameters.c
drivers/net/bonding/rte_eth_bond_pmd.c
drivers/net/failsafe/failsafe_args.c
drivers/net/mlx4/mlx4_ethdev.c
drivers/net/mlx5/mlx5_ethdev.c
drivers/net/mvpp2/mrvl_qos.c
drivers/net/tap/rte_eth_tap.c
examples/ip_pipeline/pipeline/pipeline_flow_classification_be.c
examples/ip_pipeline/pipeline/pipeline_passthrough_be.c
examples/load_balancer/config.c
lib/librte_cmdline/cmdline_parse.c
lib/librte_cmdline/cmdline_parse_etheraddr.c
lib/librte_cmdline/cmdline_parse_ipaddr.c
lib/librte_cmdline/cmdline_parse_portlist.c
lib/librte_cmdline/cmdline_parse_string.c
lib/librte_eal/common/eal_common_bus.c
lib/librte_pdump/rte_pdump.c
test/test/test_cmdline_cirbuf.c
test/test/test_eal_flags.c
test/test/test_malloc.c

index f6865bd..d29de03 100644 (file)
@@ -24,6 +24,7 @@
 #include <rte_kvargs.h>
 #include <rte_mempool.h>
 #include <rte_ring.h>
+#include <rte_string_fns.h>
 #include <rte_pdump.h>
 
 #define CMD_LINE_OPT_PDUMP "pdump"
@@ -408,17 +409,15 @@ launch_args_parse(int argc, char **argv, char *prgname)
                        if (!strncmp(long_option[option_index].name,
                                        CMD_LINE_OPT_SER_SOCK_PATH,
                                        sizeof(CMD_LINE_OPT_SER_SOCK_PATH))) {
-                               snprintf(server_socket_path,
-                                       sizeof(server_socket_path), "%s",
-                                       optarg);
+                               strlcpy(server_socket_path, optarg,
+                                       sizeof(server_socket_path));
                        }
 
                        if (!strncmp(long_option[option_index].name,
                                        CMD_LINE_OPT_CLI_SOCK_PATH,
                                        sizeof(CMD_LINE_OPT_CLI_SOCK_PATH))) {
-                               snprintf(client_socket_path,
-                                       sizeof(client_socket_path), "%s",
-                                       optarg);
+                               strlcpy(client_socket_path, optarg,
+                                       sizeof(client_socket_path));
                        }
 
                        break;
index 97d22b8..2192bdc 100644 (file)
@@ -658,9 +658,8 @@ launch_args_parse(int argc, char** argv)
                        if (!strcmp(lgopts[opt_idx].name, "cmdline-file")) {
                                printf("CLI commands to be read from %s\n",
                                       optarg);
-                               snprintf(cmdline_filename,
-                                        sizeof(cmdline_filename), "%s",
-                                        optarg);
+                               strlcpy(cmdline_filename, optarg,
+                                       sizeof(cmdline_filename));
                        }
                        if (!strcmp(lgopts[opt_idx].name, "auto-start")) {
                                printf("Auto-start selected\n");
index b59ba9f..9b02850 100644 (file)
@@ -17,6 +17,7 @@
 #include <rte_bus_vdev.h>
 #include <rte_alarm.h>
 #include <rte_cycles.h>
+#include <rte_string_fns.h>
 
 #include "rte_eth_bond.h"
 #include "rte_eth_bond_private.h"
@@ -617,7 +618,7 @@ mode6_debug(const char __attribute__((unused)) *info, struct ether_hdr *eth_h,
        uint16_t offset = get_vlan_offset(eth_h, &ether_type);
 
 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
-       snprintf(buf, 16, "%s", info);
+       strlcpy(buf, info, 16);
 #endif
 
        if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
index 366dbea..ea934f9 100644 (file)
@@ -14,6 +14,7 @@
 #include <rte_devargs.h>
 #include <rte_malloc.h>
 #include <rte_kvargs.h>
+#include <rte_string_fns.h>
 
 #include "failsafe_private.h"
 
@@ -340,7 +341,7 @@ fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
                a = b + 1;
        }
 out:
-       snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
+       strlcpy(params, buffer, DEVARGS_MAXLEN);
        return 0;
 }
 
@@ -392,7 +393,7 @@ failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
        ret = 0;
        priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
        /* default parameters */
-       n = snprintf(mut_params, sizeof(mut_params), "%s", params);
+       n = strlcpy(mut_params, params, sizeof(mut_params));
        if (n >= sizeof(mut_params)) {
                ERROR("Parameter string too long (>=%zu)",
                                sizeof(mut_params));
index fbeef16..5f731e0 100644 (file)
@@ -39,6 +39,7 @@
 #include <rte_ether.h>
 #include <rte_flow.h>
 #include <rte_pci.h>
+#include <rte_string_fns.h>
 
 #include "mlx4.h"
 #include "mlx4_flow.h"
@@ -120,7 +121,7 @@ try_dev_id:
                        goto try_dev_id;
                dev_port_prev = dev_port;
                if (dev_port == (priv->port - 1u))
-                       snprintf(match, sizeof(match), "%s", name);
+                       strlcpy(match, name, sizeof(match));
        }
        closedir(dir);
        if (match[0] == '\0') {
index b6f5101..cc85f76 100644 (file)
@@ -33,6 +33,7 @@
 #include <rte_common.h>
 #include <rte_interrupts.h>
 #include <rte_malloc.h>
+#include <rte_string_fns.h>
 
 #include "mlx5.h"
 #include "mlx5_glue.h"
@@ -165,7 +166,7 @@ try_dev_id:
                        goto try_dev_id;
                dev_port_prev = dev_port;
                if (dev_port == (priv->port - 1u))
-                       snprintf(match, sizeof(match), "%s", name);
+                       strlcpy(match, name, sizeof(match));
        }
        closedir(dir);
        if (match[0] == '\0') {
index 741d3da..70d000c 100644 (file)
@@ -242,7 +242,7 @@ get_entry_values(const char *entry, uint8_t *tab,
                return -1;
 
        /* Copy the entry to safely use rte_strsplit(). */
-       snprintf(entry_cpy, RTE_DIM(entry_cpy), "%s", entry);
+       strlcpy(entry_cpy, entry, RTE_DIM(entry_cpy));
 
        /*
         * If there are more tokens than array size, rte_strsplit will
index 3e4f7a8..ed6d738 100644 (file)
@@ -15,6 +15,7 @@
 #include <rte_net.h>
 #include <rte_debug.h>
 #include <rte_ip.h>
+#include <rte_string_fns.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -1546,7 +1547,7 @@ set_interface_name(const char *key __rte_unused,
        char *name = (char *)extra_args;
 
        if (value)
-               snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s", value);
+               strlcpy(name, value, RTE_ETH_NAME_MAX_LEN - 1);
        else
                snprintf(name, RTE_ETH_NAME_MAX_LEN - 1, "%s%d",
                         DEFAULT_TAP_NAME, (tap_unit - 1));
@@ -1562,7 +1563,7 @@ set_remote_iface(const char *key __rte_unused,
        char *name = (char *)extra_args;
 
        if (value)
-               snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s", value);
+               strlcpy(name, value, RTE_ETH_NAME_MAX_LEN);
 
        return 0;
 }
@@ -1576,7 +1577,7 @@ static int parse_user_mac(struct ether_addr *user_mac,
        if (user_mac == NULL || value == NULL)
                return 0;
 
-       snprintf(mac_temp, sizeof(mac_temp), "%s", value);
+       strlcpy(mac_temp, value, sizeof(mac_temp));
        mac_byte = strtok(mac_temp, ":");
 
        while ((mac_byte != NULL) &&
index 097ec34..3e26ae8 100644 (file)
@@ -8,6 +8,7 @@
 #include <rte_malloc.h>
 #include <rte_table_hash.h>
 #include <rte_byteorder.h>
+#include <rte_string_fns.h>
 #include <pipeline.h>
 
 #include "pipeline_flow_classification_be.h"
@@ -280,8 +281,7 @@ pipeline_fc_parse_args(struct pipeline_flow_classification *p,
                                "\"%s\" is too long", params->name,
                                arg_name);
 
-                       snprintf(key_mask_str, mask_str_len + 1, "%s",
-                               arg_value);
+                       strlcpy(key_mask_str, arg_value, mask_str_len + 1);
 
                        continue;
                }
index b2bbaed..1c44e75 100644 (file)
@@ -8,6 +8,7 @@
 #include <rte_common.h>
 #include <rte_malloc.h>
 #include <rte_byteorder.h>
+#include <rte_string_fns.h>
 #include <rte_table_stub.h>
 #include <rte_table_hash.h>
 #include <rte_pipeline.h>
@@ -524,8 +525,7 @@ pipeline_passthrough_parse_args(struct pipeline_passthrough_params *p,
                                "\"%s\" too long", params->name,
                                arg_name);
 
-                       snprintf(dma_mask_str, mask_str_len + 1,
-                               "%s", arg_value);
+                       strlcpy(dma_mask_str, arg_value, mask_str_len + 1);
 
                        p->dma_enabled = 1;
 
index b5b6636..972c85c 100644 (file)
@@ -121,7 +121,7 @@ str_to_unsigned_array(
        int i, num_splits = 0;
 
        /* copy s so we don't modify original string */
-       snprintf(str, sizeof(str), "%s", s);
+       strlcpy(str, s, sizeof(str));
        num_splits = rte_strsplit(str, sizeof(str), splits, num_vals, separator);
 
        errno = 0;
index e88e4e1..961f9be 100644 (file)
@@ -251,7 +251,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)
        }
 
 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
-       snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
+       strlcpy(debug_buf, buf, (linelen > 64 ? 64 : linelen));
        debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
 #endif
 
@@ -436,7 +436,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                                if ((unsigned)(comp_len + 1) > size)
                                        return 0;
 
-                               snprintf(dst, size, "%s", comp_buf);
+                               strlcpy(dst, comp_buf, size);
                                dst[comp_len] = 0;
                                return 2;
                        }
@@ -513,7 +513,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                                        continue;
                                }
                                (*state)++;
-                               l=snprintf(dst, size, "%s", tmpbuf);
+                               l=strlcpy(dst, tmpbuf, size);
                                if (l>=0 && token_hdr.ops->get_help) {
                                        token_hdr.ops->get_help(token_p, tmpbuf,
                                                                sizeof(tmpbuf));
index 8d28119..24e0475 100644 (file)
@@ -102,7 +102,7 @@ cmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
                        (token_len != ETHER_ADDRSTRLENSHORT - 1))
                return -1;
 
-       snprintf(ether_str, token_len+1, "%s", buf);
+       strlcpy(ether_str, buf, token_len + 1);
 
        tmp = my_ether_aton(ether_str);
        if (tmp == NULL)
index ae6ea10..d34436a 100644 (file)
@@ -277,7 +277,7 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
        if (token_len >= INET6_ADDRSTRLEN+4)
                return -1;
 
-       snprintf(ip_str, token_len+1, "%s", buf);
+       strlcpy(ip_str, buf, token_len + 1);
 
        /* convert the network prefix */
        if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
index 5952f34..ad43b52 100644 (file)
@@ -94,7 +94,7 @@ cmdline_parse_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
        if (token_len >= PORTLIST_TOKEN_SIZE)
                return -1;
 
-       snprintf(portlist_str, token_len+1, "%s", buf);
+       strlcpy(portlist_str, buf, token_len + 1);
 
        if (pl) {
                pl->map = 0;
index abde041..9cf41d0 100644 (file)
@@ -125,10 +125,10 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
        if (res) {
                if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0))
                        /* we are sure that token_len is < STR_MULTI_TOKEN_SIZE-1 */
-                       snprintf(res, STR_MULTI_TOKEN_SIZE, "%s", buf);
+                       strlcpy(res, buf, STR_MULTI_TOKEN_SIZE);
                else
                        /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
-                       snprintf(res, STR_TOKEN_SIZE, "%s", buf);
+                       strlcpy(res, buf, STR_TOKEN_SIZE);
 
                *((char *)res + token_len) = 0;
        }
index 3e022d5..0943851 100644 (file)
@@ -36,6 +36,7 @@
 
 #include <rte_bus.h>
 #include <rte_debug.h>
+#include <rte_string_fns.h>
 
 #include "eal_private.h"
 
@@ -212,7 +213,7 @@ rte_bus_find_by_device_name(const char *str)
        char name[RTE_DEV_NAME_MAX_LEN];
        char *c;
 
-       snprintf(name, sizeof(name), "%s", str);
+       strlcpy(name, str, sizeof(name));
        c = strchr(name, ',');
        if (c != NULL)
                c[0] = '\0';
index 4fb0b42..ad6efc6 100644 (file)
@@ -17,6 +17,7 @@
 #include <rte_lcore.h>
 #include <rte_log.h>
 #include <rte_errno.h>
+#include <rte_string_fns.h>
 
 #include "rte_pdump.h"
 
@@ -401,9 +402,9 @@ pdump_get_socket_path(char *buffer, int bufsz, enum rte_pdump_socktype type)
        int ret = 0;
 
        if (type == RTE_PDUMP_SOCKET_SERVER && server_socket_dir[0] != 0)
-               snprintf(dir, sizeof(dir), "%s", server_socket_dir);
+               strlcpy(dir, server_socket_dir, sizeof(dir));
        else if (type == RTE_PDUMP_SOCKET_CLIENT && client_socket_dir[0] != 0)
-               snprintf(dir, sizeof(dir), "%s", client_socket_dir);
+               strlcpy(dir, client_socket_dir, sizeof(dir));
        else {
                if (getuid() != 0) {
                        dir_home = getenv(SOCKET_PATH_HOME);
@@ -891,10 +892,10 @@ rte_pdump_set_socket_dir(const char *path, enum rte_pdump_socktype type)
        if (path != NULL) {
                if (type == RTE_PDUMP_SOCKET_SERVER) {
                        count = sizeof(server_socket_dir);
-                       ret = snprintf(server_socket_dir, count, "%s", path);
+                       ret = strlcpy(server_socket_dir, path, count);
                } else {
                        count = sizeof(client_socket_dir);
-                       ret = snprintf(client_socket_dir, count, "%s", path);
+                       ret = strlcpy(client_socket_dir, path, count);
                }
 
                if (ret < 0  || ret >= count) {
index e9193f6..8ac326c 100644 (file)
@@ -483,7 +483,7 @@ test_cirbuf_string_get_del_partial(void)
        memset(tmp, 0, sizeof(tmp));
        memset(tmp2, 0, sizeof(tmp));
 
-       snprintf(tmp2, sizeof(tmp2), "%s", CIRBUF_STR_HEAD);
+       strlcpy(tmp2, CIRBUF_STR_HEAD, sizeof(tmp2));
 
        /*
         * initialize circular buffer
index 37c42ef..93eb7a4 100644 (file)
@@ -1151,11 +1151,12 @@ test_memory_flags(void)
        /* add one extra socket */
        for (i = 0; i < num_sockets + 1; i++) {
                snprintf(buf, sizeof(buf), "%s%s", invalid_socket_mem, DEFAULT_MEM_SIZE);
-               snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
+               strlcpy(invalid_socket_mem, buf, sizeof(invalid_socket_mem));
 
                if (num_sockets + 1 - i > 1) {
                        snprintf(buf, sizeof(buf), "%s,", invalid_socket_mem);
-                       snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
+                       strlcpy(invalid_socket_mem, buf,
+                               sizeof(invalid_socket_mem));
                }
        }
 
@@ -1167,11 +1168,12 @@ test_memory_flags(void)
        /* add one extra socket */
        for (i = 0; i < num_sockets; i++) {
                snprintf(buf, sizeof(buf), "%s%s", valid_socket_mem, DEFAULT_MEM_SIZE);
-               snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
+               strlcpy(valid_socket_mem, buf, sizeof(valid_socket_mem));
 
                if (num_sockets - i > 1) {
                        snprintf(buf, sizeof(buf), "%s,", valid_socket_mem);
-                       snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
+                       strlcpy(valid_socket_mem, buf,
+                               sizeof(valid_socket_mem));
                }
        }
 
index d23192c..ccc5fea 100644 (file)
@@ -378,7 +378,7 @@ test_realloc(void)
                printf("NULL pointer returned from rte_zmalloc\n");
                return -1;
        }
-       snprintf(ptr1, size1, "%s" ,hello_str);
+       strlcpy(ptr1, hello_str, size1);
        char *ptr2 = rte_realloc(ptr1, size2, RTE_CACHE_LINE_SIZE);
        if (!ptr2){
                rte_free(ptr1);