clean up snprintf use for string copying
cover clean up snprintf use for string copying
There are quite a few instances remaining in DPDK where snprintf is being used for string copying. These were not being picked up by our existing coccinelle script, but that can be fixed by editing the script and running it against our code. In the process a bug was found and fixed in the bonding pmd, where we were incorrectly specifiying the buffer length parameter to snprintf.
The actual replacement was done in two phases - first replacing all instances where only the snprintf line in question needed changing, then fixing the other instances where we also needed to add in the header include. [Using two stages allowed the header addition to be automated too, since we had a list of files where every one needed the header inclusion]
Bruce Richardson (5):
net/bonding: fix buffer length when printing strings devtools/cocci: make strlcpy replacement smarter devtools/cocci: create safer version of strlcpy script replace snprintf with strlcpy without adding extra include replace snprintf with strlcpy
app/pdump/main.c | 4 +-- app/proc-info/main.c | 2 +- app/test-acl/main.c | 3 +- app/test-bbdev/main.c | 5 ++- app/test-eventdev/evt_options.c | 3 +- app/test-pmd/cmdline_flow.c | 13 +++---- app/test/test_cmdline_etheraddr.c | 3 +- app/test/test_cmdline_num.c | 3 +- app/test/test_compressdev.c | 2 +- app/test/test_eal_flags.c | 2 +- app/test/test_table_acl.c | 11 +++--- devtools/cocci/strlcpy-with-header.cocci | 12 +++++++ devtools/cocci/strlcpy.cocci | 3 +- drivers/bus/dpaa/base/fman/of.c | 7 ++-- drivers/bus/pci/bsd/pci.c | 2 +- drivers/bus/pci/linux/pci_uio.c | 3 +- drivers/bus/vdev/vdev.c | 2 +- .../crypto/aesni_mb/rte_aesni_mb_pmd_ops.c | 3 +- .../aesni_mb/rte_aesni_mb_pmd_ops_compat.c | 3 +- drivers/crypto/ccp/rte_ccp_pmd.c | 6 ++-- .../scheduler/rte_cryptodev_scheduler.c | 8 ++--- drivers/event/opdl/opdl_ring.c | 3 +- drivers/net/af_packet/rte_eth_af_packet.c | 5 +-- drivers/net/ark/ark_pktchkr.c | 3 +- drivers/net/ark/ark_pktgen.c | 3 +- drivers/net/atlantic/atl_ethdev.c | 5 +-- drivers/net/bnx2x/bnx2x_ethdev.c | 8 ++--- drivers/net/bnxt/bnxt_stats.c | 36 +++++++++---------- drivers/net/bonding/rte_eth_bond_pmd.c | 23 ++++++------ drivers/net/dpaa/dpaa_ethdev.c | 8 ++--- drivers/net/dpaa2/dpaa2_ethdev.c | 7 ++-- drivers/net/e1000/igb_ethdev.c | 17 ++++----- drivers/net/ena/ena_ethdev.c | 11 +++--- drivers/net/failsafe/failsafe_args.c | 2 +- drivers/net/failsafe/failsafe_eal.c | 6 ++-- drivers/net/i40e/i40e_ethdev.c | 12 +++---- drivers/net/i40e/rte_pmd_i40e.c | 5 +-- drivers/net/ice/ice_ethdev.c | 11 +++--- drivers/net/ixgbe/ixgbe_ethdev.c | 35 +++++++++--------- drivers/net/kni/rte_eth_kni.c | 3 +- drivers/net/liquidio/lio_ethdev.c | 5 +-- drivers/net/mvneta/mvneta_ethdev.c | 3 +- drivers/net/mvpp2/mrvl_ethdev.c | 5 +-- drivers/net/pcap/rte_eth_pcap.c | 8 ++--- drivers/net/qede/qede_ethdev.c | 22 ++++++------ drivers/net/ring/rte_eth_ring.c | 3 +- drivers/net/softnic/rte_eth_softnic_tap.c | 2 +- drivers/net/tap/rte_eth_tap.c | 9 +++-- drivers/net/virtio/virtio_user/vhost_user.c | 3 +- .../net/virtio/virtio_user/virtio_user_dev.c | 3 +- examples/cmdline/commands.c | 2 +- examples/cmdline/parse_obj_list.c | 2 +- examples/ethtool/lib/rte_ethtool.c | 8 ++--- examples/exception_path/main.c | 4 +-- examples/ip_pipeline/kni.c | 2 +- examples/ip_pipeline/tap.c | 2 +- examples/l2fwd-crypto/main.c | 4 +-- examples/l3fwd-power/main.c | 2 +- .../multi_process/simple_mp/mp_commands.c | 2 +- examples/netmap_compat/bridge/bridge.c | 4 +-- examples/netmap_compat/lib/compat_netmap.c | 2 +- examples/tep_termination/main.c | 2 +- examples/vhost/main.c | 2 +- examples/vhost_scsi/scsi.c | 9 +++-- examples/vm_power_manager/channel_manager.c | 7 ++-- examples/vm_power_manager/channel_monitor.c | 14 ++++---- lib/librte_acl/rte_acl.c | 3 +- lib/librte_bbdev/rte_bbdev.c | 3 +- lib/librte_cfgfile/rte_cfgfile.c | 22 ++++++------ lib/librte_cmdline/cmdline.c | 2 +- lib/librte_cmdline/cmdline_parse.c | 5 +-- lib/librte_cmdline/cmdline_parse_num.c | 2 +- lib/librte_compressdev/rte_compressdev.c | 5 +-- lib/librte_compressdev/rte_compressdev_pmd.c | 3 +- lib/librte_cryptodev/rte_cryptodev.c | 4 +-- lib/librte_cryptodev/rte_cryptodev_pmd.c | 3 +- lib/librte_distributor/rte_distributor.c | 2 +- lib/librte_distributor/rte_distributor_v20.c | 2 +- lib/librte_eal/common/eal_common_memzone.c | 2 +- lib/librte_eal/common/eal_common_options.c | 3 +- lib/librte_eal/common/eal_common_tailqs.c | 2 +- lib/librte_efd/rte_efd.c | 3 +- lib/librte_ethdev/rte_ethdev.c | 6 ++-- lib/librte_eventdev/rte_eventdev.c | 4 +-- lib/librte_flow_classify/rte_flow_classify.c | 4 +-- lib/librte_hash/rte_cuckoo_hash.c | 2 +- lib/librte_hash/rte_fbk_hash.c | 2 +- lib/librte_jobstats/rte_jobstats.c | 3 +- lib/librte_kni/rte_kni.c | 6 ++-- lib/librte_latencystats/rte_latencystats.c | 5 +-- lib/librte_lpm/rte_lpm.c | 4 +-- lib/librte_lpm/rte_lpm6.c | 2 +- lib/librte_member/rte_member.c | 3 +- lib/librte_mempool/rte_mempool.c | 2 +- lib/librte_mempool/rte_mempool_ops.c | 3 +- lib/librte_pdump/rte_pdump.c | 8 ++--- lib/librte_pipeline/rte_pipeline.c | 2 +- lib/librte_power/power_acpi_cpufreq.c | 3 +- lib/librte_power/power_pstate_cpufreq.c | 3 +- lib/librte_rawdev/rte_rawdev.c | 3 +- lib/librte_reorder/rte_reorder.c | 5 +-- lib/librte_ring/rte_ring.c | 2 +- 102 files changed, 302 insertions(+), 270 deletions(-) create mode 100644 devtools/cocci/strlcpy-with-header.cocci
Signature · 1 line
2.20.1
PATCH 1/5 net/bonding: fix buffer length when printing strings
Using the size of the source string is incorrect when printing using snprintf. Instead pass in the buffer size to be used appropriately.
--- drivers/net/bonding/rte_eth_bond_pmd.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/net/bonding/rte_eth_bond_pmd.c +10 -13
@@ -489,35 +489,31 @@ uint32_t burstnumberTX;#ifdef RTE_LIBRTE_BOND_DEBUG_ALB static void -arp_op_name (uint16_t arp_op,char *buf) +arp_op_name (uint16_t arp_op,char *buf,size_t buf_len) {switch (arp_op) {case ARP_OP_REQUEST : -snprintf (buf,sizeof ("ARP Request" ),"%s" ,"ARP Request" ); +snprintf (buf, buf_len,"%s" ,"ARP Request" );return ;case ARP_OP_REPLY : -snprintf (buf,sizeof ("ARP Reply" ),"%s" ,"ARP Reply" ); +snprintf (buf, buf_len,"%s" ,"ARP Reply" );return ;case ARP_OP_REVREQUEST : -snprintf (buf,sizeof ("Reverse ARP Request" ),"%s" , -"Reverse ARP Request" ); +snprintf (buf, buf_len,"%s" ,"Reverse ARP Request" );return ;case ARP_OP_REVREPLY : -snprintf (buf,sizeof ("Reverse ARP Reply" ),"%s" , -"Reverse ARP Reply" ); +snprintf (buf, buf_len,"%s" ,"Reverse ARP Reply" );return ;case ARP_OP_INVREQUEST : -snprintf (buf,sizeof ("Peer Identify Request" ),"%s" , -"Peer Identify Request" ); +snprintf (buf, buf_len,"%s" ,"Peer Identify Request" );return ;case ARP_OP_INVREPLY : -snprintf (buf,sizeof ("Peer Identify Reply" ),"%s" , -"Peer Identify Reply" ); +snprintf (buf, buf_len,"%s" ,"Peer Identify Reply" );return ;default :break ; } -snprintf (buf,sizeof ("Unknown" ),"%s" ,"Unknown" ); +snprintf (buf, buf_len,"%s" ,"Unknown" );return ; }#endif
@@ -621,7 +617,8 @@ mode6_debug(const char __attribute__((unused)) *info, struct ether_hdr *eth_h, arp_h = (struct arp_hdr *)((char *)(eth_h +1 ) + offset);ipv4_addr_to_dot (arp_h->arp_data.arp_sip, src_ip, MaxIPv4String);ipv4_addr_to_dot (arp_h->arp_data.arp_tip, dst_ip, MaxIPv4String); -arp_op_name (rte_be_to_cpu_16 (arp_h->arp_op), ArpOp); +arp_op_name (rte_be_to_cpu_16 (arp_h->arp_op), + ArpOp,sizeof (ArpOp)); MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, ArpOp, port, *burstnumber); }#endif
Signature · 1 line
2.20.1
Re: [PATCH 1/5] net/bonding: fix buffer length when printing strings
drivers/net/bonding/rte_eth_bond_pmd.c uint32_t burstnumberTX; · line 497
Quoted 10 lines · On Wed, 3 Apr 2019 15:45:01 +0100 Bruce Richardson…
static void
drivers/net/bonding/rte_eth_bond_pmd.c +2
-2-arp_op_name(uint16_t arp_op, char *buf) +arp_op_name(uint16_t arp_op, char *buf, size_t buf_len) { switch (arp_op) { case ARP_OP_REQUEST: -snprintf (buf,sizeof ("ARP Request" ),"%s" ,"ARP Request" ); + snprintf(buf, buf_len, "%s", "ARP Request");return ;
This should be strlcpy not snprintf
Re: [PATCH 1/5] net/bonding: fix buffer length when printing strings
Quoted 4 lines · Stephen Hemminger
Quoted 1 line · On Wed, 3 Apr 2019 15:45:01 +0100 Bruce Richardson…
Quoted 10 lines
drivers/net/bonding/rte_eth_bond_pmd.c uint32_t burstnumberTX; · line 497
Quoted 9 lines
static void
drivers/net/bonding/rte_eth_bond_pmd.c +2
-2-arp_op_name(uint16_t arp_op, char *buf) +arp_op_name(uint16_t arp_op, char *buf, size_t buf_len) { switch (arp_op) { case ARP_OP_REQUEST: -snprintf (buf,sizeof ("ARP Request" ),"%s" ,"ARP Request" ); + snprintf(buf, buf_len, "%s", "ARP Request");return ;This should be strlcpy not snprintf
Yes, it should, but I just let that get fixed by cocci script in the later patch. For this one, I just fixed the most egregious error.
Re: [PATCH 1/5] net/bonding: fix buffer length when printing strings
Quoted 16 lines · On Wed, 3 Apr 2019 16:51:17 +0100 Bruce Richardson…
Quoted 4 lines · Stephen Hemminger
Quoted 1 line · On Wed, 3 Apr 2019 15:45:01 +0100 Bruce Richardson…
Quoted 10 lines
drivers/net/bonding/rte_eth_bond_pmd.c uint32_t burstnumberTX; · line 497
Quoted 9 lines
static void
drivers/net/bonding/rte_eth_bond_pmd.c +2
-2-arp_op_name(uint16_t arp_op, char *buf) +arp_op_name(uint16_t arp_op, char *buf, size_t buf_len) { switch (arp_op) { case ARP_OP_REQUEST: -snprintf (buf,sizeof ("ARP Request" ),"%s" ,"ARP Request" ); + snprintf(buf, buf_len, "%s", "ARP Request");return ;This should be strlcpy not snprintf
Quoted 2 lines
Yes, it should, but I just let that get fixed by cocci script in the later patch. For this one, I just fixed the most egregious error.
Ok, I see
PATCH 2/5 devtools/cocci: make strlcpy replacement smarter
In reply to Bruce RichardsonThe original coccinelle script worked by replacing instances of snprintf(.."%s",...) with strlcpy(), but only where the source and dest parameters were plain identifiers. Allowing expressions for those params opens up a wide range of other possible changes.
--- devtools/cocci/strlcpy.cocci | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
devtools/cocci/strlcpy.cocci +1 -2
@@ -1,6 +1,5 @@ @use_strlcpy@ -identifier src, dst; -expression size; +expression src, dst, size; @@ ( - snprintf(dst, size, "%s", src)
Signature · 1 line
2.20.1
PATCH 3/5 devtools/cocci: create safer version of strlcpy script
In reply to Bruce RichardsonThe existing cocci script for coccinelle replaces all matching instances of snprintf() with strlcpy() without regards to header inclusion. To allow changes without build errors, we create a safer version of this script that only makes changes when the rte_string_fns.h header is already included.
--- devtools/cocci/strlcpy-with-header.cocci | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 devtools/cocci/strlcpy-with-header.cocci
devtools/cocci/strlcpy-with-header.cocci new file +12 -0
@@ -0,0 +1,12 @@ +@include@ +@@ + +#include <rte_string_fns.h> + +@use_strlcpy depends on include@ +expression src, dst, size; +@@ +( +- snprintf(dst, size, "%s", src) ++ strlcpy(dst, src, size) +)
Signature · 1 line
2.20.1
PATCH 4/5 replace snprintf with strlcpy without adding extra include
In reply to Bruce RichardsonFor files that already have rte_string_fns.h included in them, we can do a straight replacement of snprintf(..."%s",...) with strlcpy. The changes in this patch were auto-generated via command:
spatch --sp-file devtools/cocci/strlcpy-with-header.cocci --dir . --in-place
--- app/pdump/main.c | 4 ++-- app/proc-info/main.c | 2 +- app/test-bbdev/main.c | 5 ++--- app/test/test_cmdline_etheraddr.c | 3 +-- app/test/test_cmdline_num.c | 3 +-- app/test/test_compressdev.c | 2 +- app/test/test_eal_flags.c | 2 +- drivers/bus/pci/bsd/pci.c | 2 +- drivers/bus/vdev/vdev.c | 2 +- drivers/net/bonding/rte_eth_bond_pmd.c | 14 +++++++------- drivers/net/dpaa2/dpaa2_ethdev.c | 7 +++---- drivers/net/failsafe/failsafe_args.c | 2 +- drivers/net/i40e/i40e_ethdev.c | 12 ++++++------ drivers/net/pcap/rte_eth_pcap.c | 8 ++++---- drivers/net/ring/rte_eth_ring.c | 3 ++- drivers/net/softnic/rte_eth_softnic_tap.c | 2 +- drivers/net/tap/rte_eth_tap.c | 9 ++++----- examples/cmdline/commands.c | 2 +- examples/cmdline/parse_obj_list.c | 2 +- examples/exception_path/main.c | 4 ++-- examples/ip_pipeline/kni.c | 2 +- examples/ip_pipeline/tap.c | 2 +- examples/l3fwd-power/main.c | 2 +- examples/multi_process/simple_mp/mp_commands.c | 2 +- examples/netmap_compat/bridge/bridge.c | 4 ++-- examples/netmap_compat/lib/compat_netmap.c | 2 +- examples/tep_termination/main.c | 2 +- examples/vhost/main.c | 2 +- examples/vhost_scsi/scsi.c | 9 ++++----- lib/librte_cmdline/cmdline.c | 2 +- lib/librte_cmdline/cmdline_parse.c | 5 +++-- lib/librte_cmdline/cmdline_parse_num.c | 2 +- lib/librte_cryptodev/rte_cryptodev.c | 4 ++-- lib/librte_distributor/rte_distributor.c | 2 +- lib/librte_distributor/rte_distributor_v20.c | 2 +- lib/librte_eal/common/eal_common_memzone.c | 2 +- lib/librte_eal/common/eal_common_tailqs.c | 2 +- lib/librte_ethdev/rte_ethdev.c | 6 +++--- lib/librte_hash/rte_cuckoo_hash.c | 2 +- lib/librte_hash/rte_fbk_hash.c | 2 +- lib/librte_kni/rte_kni.c | 6 +++--- lib/librte_lpm/rte_lpm.c | 4 ++-- lib/librte_lpm/rte_lpm6.c | 2 +- lib/librte_mempool/rte_mempool.c | 2 +- lib/librte_pdump/rte_pdump.c | 8 ++++---- lib/librte_pipeline/rte_pipeline.c | 2 +- lib/librte_ring/rte_ring.c | 2 +- 47 files changed, 85 insertions(+), 89 deletions(-)
app/pdump/main.c +2 -2
@@ -189,12 +189,12 @@ parse_rxtxdev(const char *key, const char *value, void *extra_args)struct pdump_tuples *pt = extra_args;if (!strcmp (key,PDUMP_RX_DEV_ARG )) { -snprintf (pt->rx_dev,sizeof (pt->rx_dev),"%s" , value); +strlcpy (pt->rx_dev, value,sizeof (pt->rx_dev));/* identify the tx stream type for pcap vdev */ if (if_nametoindex (pt->rx_dev)) pt->rx_vdev_stream_type =IFACE ; }else if (!strcmp (key,PDUMP_TX_DEV_ARG )) { -snprintf (pt->tx_dev,sizeof (pt->tx_dev),"%s" , value); +strlcpy (pt->tx_dev, value,sizeof (pt->tx_dev));/* identify the tx stream type for pcap vdev */ if (if_nametoindex (pt->tx_dev)) pt->tx_vdev_stream_type =IFACE ;
app/proc-info/main.c +1 -1
@@ -193,7 +193,7 @@ proc_info_preparse_args(int argc, char **argv)proc_info_usage (prgname);return -1 ; } -snprintf (host_id,sizeof (host_id),"%s" , argv[i+1 ]); +strlcpy (host_id, argv[i +1 ],sizeof (host_id)); } }
app/test-bbdev/main.c +2 -3
@@ -226,9 +226,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 'l' : TEST_ASSERT(strlen(optarg) > 0,
app/test/test_cmdline_etheraddr.c +1 -2
@@ -118,8 +118,7 @@ test_parse_etheraddr_invalid_param(void)/* try null result */ /* copy string to buffer */ -snprintf (buf,sizeof (buf),"%s" , - ether_addr_valid_strs[0 ].str); +strlcpy (buf, ether_addr_valid_strs[0 ].str,sizeof (buf)); ret =cmdline_parse_etheraddr (NULL , buf,NULL ,0 );if (ret ==-1 ) {
app/test/test_cmdline_num.c +1 -2
@@ -318,8 +318,7 @@ test_parse_num_invalid_param(void) token.num_data.type =UINT32 ;/* copy string to buffer */ -snprintf (buf,sizeof (buf),"%s" , - num_valid_positive_strs[0 ].str); +strlcpy (buf, num_valid_positive_strs[0 ].str,sizeof (buf));/* try all null */ ret =cmdline_parse_num (NULL ,NULL ,NULL ,0 );
app/test/test_compressdev.c +1 -1
@@ -767,7 +767,7 @@ test_deflate_comp_decomp(const struct interim_data_params *int_data,for (i =0 ; i < num_bufs; i++) { data_size =strlen (test_bufs[i]) +1 ; buf_ptr =rte_pktmbuf_append (uncomp_bufs[i], data_size); -snprintf (buf_ptr, data_size,"%s" , test_bufs[i]); +strlcpy (buf_ptr, test_bufs[i], data_size); } }
app/test/test_eal_flags.c +1 -1
@@ -58,7 +58,7 @@ get_hugepage_path(char * src, int src_len, char * dst, int dst_len)return 0 ;if (strncmp (tokens[2 ],"hugetlbfs" ,sizeof ("hugetlbfs" )) ==0 ) { -snprintf (dst, dst_len,"%s" , tokens[1 ]); +strlcpy (dst, tokens[1 ], dst_len);return 1 ; }return 0 ;
drivers/bus/pci/bsd/pci.c +1 -1
@@ -145,7 +145,7 @@ pci_uio_alloc_resource(struct rte_pci_device *dev, goto error; } -snprintf ((*uio_res)->path,sizeof ((*uio_res)->path),"%s" , devname); +strlcpy ((*uio_res)->path, devname,sizeof ((*uio_res)->path));memcpy (&(*uio_res)->pci_addr, &dev->addr,sizeof ((*uio_res)->pci_addr));return 0 ;
drivers/bus/vdev/vdev.c +1 -1
@@ -192,7 +192,7 @@ alloc_devargs(const char *name, const char *args)else devargs->args =strdup ("" ); - ret =snprintf (devargs->name,sizeof (devargs->name),"%s" , name); + ret =strlcpy (devargs->name, name,sizeof (devargs->name));if (ret <0 || ret >= (int )sizeof (devargs->name)) {free (devargs->args);free (devargs);
drivers/net/bonding/rte_eth_bond_pmd.c +7 -7
@@ -493,27 +493,27 @@ arp_op_name(uint16_t arp_op, char *buf, size_t buf_len) {switch (arp_op) {case ARP_OP_REQUEST : -snprintf (buf, buf_len,"%s" ,"ARP Request" ); +strlcpy (buf,"ARP Request" , buf_len);return ;case ARP_OP_REPLY : -snprintf (buf, buf_len,"%s" ,"ARP Reply" ); +strlcpy (buf,"ARP Reply" , buf_len);return ;case ARP_OP_REVREQUEST : -snprintf (buf, buf_len,"%s" ,"Reverse ARP Request" ); +strlcpy (buf,"Reverse ARP Request" , buf_len);return ;case ARP_OP_REVREPLY : -snprintf (buf, buf_len,"%s" ,"Reverse ARP Reply" ); +strlcpy (buf,"Reverse ARP Reply" , buf_len);return ;case ARP_OP_INVREQUEST : -snprintf (buf, buf_len,"%s" ,"Peer Identify Request" ); +strlcpy (buf,"Peer Identify Request" , buf_len);return ;case ARP_OP_INVREPLY : -snprintf (buf, buf_len,"%s" ,"Peer Identify Reply" ); +strlcpy (buf,"Peer Identify Reply" , buf_len);return ;default :break ; } -snprintf (buf, buf_len,"%s" ,"Unknown" ); +strlcpy (buf,"Unknown" , buf_len);return ; }#endif
drivers/net/dpaa2/dpaa2_ethdev.c +3 -4
@@ -1334,10 +1334,9 @@ dpaa2_xstats_get_names(__rte_unused struct rte_eth_dev *dev,if (xstats_names !=NULL )for (i =0 ; i < stat_cnt; i++) -snprintf (xstats_names[i].name, -sizeof (xstats_names[i].name), -"%s" , - dpaa2_xstats_strings[i].name); +strlcpy (xstats_names[i].name, + dpaa2_xstats_strings[i].name, +sizeof (xstats_names[i].name));return stat_cnt; }
drivers/net/failsafe/failsafe_args.c +1 -1
@@ -102,7 +102,7 @@ fs_execute_cmd(struct sub_device *sdev, char *cmdline)ERROR ("Command line allocation failed" );return -ENOMEM ; } -snprintf (sdev->cmdline, len,"%s" , cmdline); +strlcpy (sdev->cmdline, cmdline, len);/* Replace all commas in the command line by spaces */ for (i =0 ; i < len; i++)if (sdev->cmdline[i] ==',' )
drivers/net/i40e/i40e_ethdev.c +6 -6
@@ -3324,17 +3324,17 @@ static int i40e_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,/* Get stats from i40e_eth_stats struct */ for (i =0 ; i <I40E_NB_ETH_XSTATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , rte_i40e_stats_strings[i].name); +strlcpy (xstats_names[count].name, + rte_i40e_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }/* Get individiual stats from i40e_hw_port struct */ for (i =0 ; i <I40E_NB_HW_PORT_XSTATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , rte_i40e_hw_port_strings[i].name); +strlcpy (xstats_names[count].name, + rte_i40e_hw_port_strings[i].name, +sizeof (xstats_names[count].name)); count++; }
drivers/net/pcap/rte_eth_pcap.c +4 -4
@@ -1084,8 +1084,8 @@ eth_from_pcaps_common(struct rte_vdev_device *vdev,struct devargs_queue *queue = &rx_queues->queue[i]; pp->rx_pcap[i] = queue->pcap; -snprintf (rx->name,sizeof (rx->name),"%s" , queue->name); -snprintf (rx->type,sizeof (rx->type),"%s" , queue->type); +strlcpy (rx->name, queue->name,sizeof (rx->name)); +strlcpy (rx->type, queue->type,sizeof (rx->type)); }for (i =0 ; i < nb_tx_queues; i++) {
@@ -1094,8 +1094,8 @@ eth_from_pcaps_common(struct rte_vdev_device *vdev, pp->tx_dumper[i] = queue->dumper; pp->tx_pcap[i] = queue->pcap; -snprintf (tx->name,sizeof (tx->name),"%s" , queue->name); -snprintf (tx->type,sizeof (tx->type),"%s" , queue->type); +strlcpy (tx->name, queue->name,sizeof (tx->name)); +strlcpy (tx->type, queue->type,sizeof (tx->type)); }return 0 ;
drivers/net/ring/rte_eth_ring.c +2 -1
@@ -497,7 +497,8 @@ static int parse_kvlist (const char *key __rte_unused, const char *value, void * goto out; } -snprintf (info->list[info->count].name,sizeof (info->list[info->count].name),"%s" , name); +strlcpy (info->list[info->count].name, name, +sizeof (info->list[info->count].name)); info->count++;
drivers/net/softnic/rte_eth_softnic_tap.c +1 -1
@@ -91,7 +91,7 @@ softnic_tap_create(struct pmd_internals *p,memset (&ifr,0 ,sizeof (ifr)); ifr.ifr_flags =IFF_TAP |IFF_NO_PI ;/* No packet information */ -snprintf (ifr.ifr_name,IFNAMSIZ ,"%s" , name); +strlcpy (ifr.ifr_name, name,IFNAMSIZ ); status =ioctl (fd,TUNSETIFF , (void *)&ifr);if (status <0 ) {
drivers/net/tap/rte_eth_tap.c +4 -5
@@ -760,9 +760,9 @@ tap_ioctl(struct pmd_internals *pmd, unsigned long request,*/ apply:if (remote) -snprintf (ifr->ifr_name,IFNAMSIZ ,"%s" , pmd->remote_iface); +strlcpy (ifr->ifr_name, pmd->remote_iface,IFNAMSIZ );else if (mode ==LOCAL_ONLY || mode ==LOCAL_AND_REMOTE ) -snprintf (ifr->ifr_name,IFNAMSIZ ,"%s" , pmd->name); +strlcpy (ifr->ifr_name, pmd->name,IFNAMSIZ );switch (request) {case SIOCSIFFLAGS :/* fetch current flags to leave other flags untouched */
@@ -1714,7 +1714,7 @@ eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name, pmd = dev->data->dev_private; dev->process_private = process_private; pmd->dev = dev; -snprintf (pmd->name,sizeof (pmd->name),"%s" , tap_name); +strlcpy (pmd->name, tap_name,sizeof (pmd->name)); pmd->type = type; pmd->ioctl_sock =socket (AF_INET ,SOCK_DGRAM ,0 );
@@ -1823,8 +1823,7 @@ eth_dev_tap_create(struct rte_vdev_device *vdev, const char *tap_name, pmd->name, remote_iface); goto error_remote; } -snprintf (pmd->remote_iface,RTE_ETH_NAME_MAX_LEN , -"%s" , remote_iface); +strlcpy (pmd->remote_iface, remote_iface,RTE_ETH_NAME_MAX_LEN );/* Save state of remote device */ tap_ioctl (pmd,SIOCGIFFLAGS , &pmd->remote_initial_flags,0 ,REMOTE_ONLY );
examples/cmdline/commands.c +1 -1
@@ -145,7 +145,7 @@ static void cmd_obj_add_parsed(void *parsed_result,cmdline_printf (cl,"mem error\n" );return ; } -snprintf (o->name,sizeof (o->name),"%s" , res->name); +strlcpy (o->name, res->name,sizeof (o->name)); o->ip = res->ip;SLIST_INSERT_HEAD (&global_obj_list, o, next);
examples/cmdline/parse_obj_list.c +1 -1
@@ -98,7 +98,7 @@ int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk,return -1 ;if (dstbuf) -snprintf (dstbuf, size,"%s" , o->name); +strlcpy (dstbuf, o->name, size);return 0 ; }
examples/exception_path/main.c +2 -2
@@ -176,7 +176,7 @@ static int tap_create(char *name) ifr.ifr_flags =IFF_TAP |IFF_NO_PI ;if (name && *name) -snprintf (ifr.ifr_name,IFNAMSIZ ,"%s" , name); +strlcpy (ifr.ifr_name, name,IFNAMSIZ ); ret =ioctl (fd,TUNSETIFF , (void *) &ifr);if (ret <0 ) {
@@ -185,7 +185,7 @@ static int tap_create(char *name) }if (name) -snprintf (name,IFNAMSIZ ,"%s" , ifr.ifr_name); +strlcpy (name, ifr.ifr_name,IFNAMSIZ );return fd; }
examples/ip_pipeline/kni.c +1 -1
@@ -126,7 +126,7 @@ kni_create(const char *name, struct kni_params *params)rte_eth_dev_info_get (link->port_id, &dev_info);memset (&kni_conf,0 ,sizeof (kni_conf)); -snprintf (kni_conf.name,RTE_KNI_NAMESIZE ,"%s" , name); +strlcpy (kni_conf.name, name,RTE_KNI_NAMESIZE ); kni_conf.force_bind = params->force_bind; kni_conf.core_id = params->thread_id; kni_conf.group_id = link->port_id;
examples/ip_pipeline/tap.c +1 -1
@@ -75,7 +75,7 @@ tap_create(const char *name)memset (&ifr,0 ,sizeof (ifr)); ifr.ifr_flags =IFF_TAP |IFF_NO_PI ;/* No packet information */ -snprintf (ifr.ifr_name,IFNAMSIZ ,"%s" , name); +strlcpy (ifr.ifr_name, name,IFNAMSIZ ); status =ioctl (fd,TUNSETIFF , (void *) &ifr);if (status <0 ) {
examples/l3fwd-power/main.c +1 -1
@@ -1384,7 +1384,7 @@ parse_ep_config(const char *q_arg) ep_med_edpi =EMPTY_POLL_MED_THRESHOLD ; ep_hgh_edpi =EMPTY_POLL_MED_THRESHOLD ; -snprintf (s,sizeof (s),"%s" , p); +strlcpy (s, p,sizeof (s)); num_arg =rte_strsplit (s,sizeof (s), str_fld,3 ,',' );
examples/multi_process/simple_mp/mp_commands.c +1 -1
@@ -48,7 +48,7 @@ static void cmd_send_parsed(void *parsed_result,if (rte_mempool_get (message_pool, &msg) <0 )rte_panic ("Failed to get message buffer\n" ); -snprintf ((char *)msg,STR_TOKEN_SIZE ,"%s" , res->message); +strlcpy ((char *)msg, res->message,STR_TOKEN_SIZE );if (rte_ring_enqueue (send_ring, msg) <0 ) {printf ("Failed to send message - message discarded\n" );rte_mempool_put (message_pool, msg);
examples/netmap_compat/bridge/bridge.c +2 -2
@@ -174,7 +174,7 @@ netmap_port_open(uint32_t idx) port->fd =rte_netmap_open ("/dev/netmap" ,O_RDWR ); -snprintf (req.nr_name,sizeof (req.nr_name),"%s" , port->str); +strlcpy (req.nr_name, port->str,sizeof (req.nr_name)); req.nr_version =NETMAP_API ; req.nr_ringid =0 ;
@@ -184,7 +184,7 @@ netmap_port_open(uint32_t idx)return err; } -snprintf (req.nr_name,sizeof (req.nr_name),"%s" , port->str); +strlcpy (req.nr_name, port->str,sizeof (req.nr_name)); req.nr_version =NETMAP_API ; req.nr_ringid =0 ;
examples/netmap_compat/lib/compat_netmap.c +1 -1
@@ -302,7 +302,7 @@ netmap_regif(struct nmreq *req, uint32_t idx, uint16_t port)if (req->nr_ringid !=0 )return -EINVAL ; -snprintf (nmif->ni_name,sizeof (nmif->ni_name),"%s" , req->nr_name); +strlcpy (nmif->ni_name, req->nr_name,sizeof (nmif->ni_name)); nmif->ni_version = req->nr_version;/* Netmap uses ni_(r|t)x_rings + 1 */
examples/tep_termination/main.c +1 -1
@@ -191,7 +191,7 @@ us_vhost_parse_basename(const char *q_arg)if (strlen (q_arg) >=MAX_BASENAME_SZ )return -1 ;else -snprintf ((char *)&dev_basename,MAX_BASENAME_SZ ,"%s" , q_arg); +strlcpy ((char *)&dev_basename, q_arg,MAX_BASENAME_SZ );return 0 ; }
examples/vhost/main.c +1 -1
@@ -366,7 +366,7 @@ us_vhost_parse_socket_path(const char *q_arg)return -1 ; } -snprintf (socket_files + nb_sockets *PATH_MAX ,PATH_MAX ,"%s" , q_arg); +strlcpy (socket_files + nb_sockets *PATH_MAX , q_arg,PATH_MAX ); nb_sockets++;return 0 ;
examples/vhost_scsi/scsi.c +4 -5
@@ -234,8 +234,8 @@ vhost_bdev_scsi_inquiry_command(struct vhost_block_dev *bdev, desig->reserved0 =0 ; desig->piv =1 ; desig->reserved1 =0 ; - desig->len =snprintf ((char *)desig->desig, -255 ,"%s" , bdev->name); + desig->len =strlcpy ((char *)desig->desig, bdev->name, +255 ); len +=sizeof (struct scsi_desig_desc ) + desig->len; buf +=sizeof (struct scsi_desig_desc ) + desig->len;
@@ -281,9 +281,8 @@ vhost_bdev_scsi_inquiry_command(struct vhost_block_dev *bdev, sizeof(inqdata->t10_vendor_id));/* PRODUCT IDENTIFICATION */ -snprintf ((char *)inqdata->product_id, -RTE_DIM (inqdata->product_id),"%s" , - bdev->product_name); +strlcpy ((char *)inqdata->product_id, bdev->product_name, +RTE_DIM (inqdata->product_id));/* PRODUCT REVISION LEVEL */ strlcpy((char *)inqdata->product_rev, "0001",
lib/librte_cmdline/cmdline.c +1 -1
@@ -69,7 +69,7 @@ cmdline_set_prompt(struct cmdline *cl, const char *prompt) {if (!cl || !prompt)return ; -snprintf (cl->prompt,sizeof (cl->prompt),"%s" , prompt); +strlcpy (cl->prompt, prompt,sizeof (cl->prompt)); } struct cmdline *
lib/librte_cmdline/cmdline_parse.c +3 -2
@@ -394,8 +394,9 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,if (!strncmp (partial_tok, tmpbuf, partial_tok_len)) {if (comp_len ==-1 ) { -snprintf (comp_buf,sizeof (comp_buf), -"%s" , tmpbuf + partial_tok_len); +strlcpy (comp_buf, + tmpbuf + partial_tok_len, +sizeof (comp_buf)); comp_len =strnlen (tmpbuf + partial_tok_len,sizeof (tmpbuf) - partial_tok_len);
lib/librte_cmdline/cmdline_parse_num.c +1 -1
@@ -340,7 +340,7 @@ cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int s/* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */ /* return -1; */ - ret =snprintf (dstbuf, size,"%s" , num_help[nd.type]); + ret =strlcpy (dstbuf, num_help[nd.type], size);if (ret <0 )return -1 ; dstbuf[size-1 ] ='\0' ;
lib/librte_cryptodev/rte_cryptodev.c +2 -2
@@ -694,8 +694,8 @@ rte_cryptodev_pmd_allocate(const char *name, int socket_id) cryptodev->data = cryptodev_data; -snprintf (cryptodev->data->name,RTE_CRYPTODEV_NAME_MAX_LEN , -"%s" , name); +strlcpy (cryptodev->data->name, name, +RTE_CRYPTODEV_NAME_MAX_LEN ); cryptodev->data->dev_id = dev_id; cryptodev->data->socket_id = socket_id;
lib/librte_distributor/rte_distributor.c +1 -1
@@ -625,7 +625,7 @@ rte_distributor_create_v1705(const char *name, } d = mz->addr; -snprintf (d->name,sizeof (d->name),"%s" , name); +strlcpy (d->name, name,sizeof (d->name)); d->num_workers = num_workers; d->alg_type = alg_type;
lib/librte_distributor/rte_distributor_v20.c +1 -1
@@ -386,7 +386,7 @@ rte_distributor_create_v20(const char *name, } d = mz->addr; -snprintf (d->name,sizeof (d->name),"%s" , name); +strlcpy (d->name, name,sizeof (d->name)); d->num_workers = num_workers; distributor_list = RTE_TAILQ_CAST(rte_distributor_tailq.head,
lib/librte_eal/common/eal_common_memzone.c +1 -1
@@ -171,7 +171,7 @@ memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,return NULL ; } -snprintf (mz->name,sizeof (mz->name),"%s" , name); +strlcpy (mz->name, name,sizeof (mz->name)); mz->iova =rte_malloc_virt2iova (mz_addr); mz->addr = mz_addr; mz->len = requested_len == 0 ?
lib/librte_eal/common/eal_common_tailqs.c +1 -1
@@ -80,7 +80,7 @@ rte_eal_tailq_create(const char *name) mcfg =rte_eal_get_configuration ()->mem_config; head = &mcfg->tailq_head[rte_tailqs_count]; -snprintf (head->name,sizeof (head->name) -1 ,"%s" , name); +strlcpy (head->name, name,sizeof (head->name) -1 );TAILQ_INIT (&head->tailq_head); rte_tailqs_count++; }
lib/librte_ethdev/rte_ethdev.c +3 -3
@@ -2068,9 +2068,9 @@ rte_eth_basic_stats_get_names(struct rte_eth_dev *dev,uint16_t num_q;for (idx =0 ; idx <RTE_NB_STATS ; idx++) { -snprintf (xstats_names[cnt_used_entries].name, -sizeof (xstats_names[0 ].name), -"%s" , rte_stats_strings[idx].name); +strlcpy (xstats_names[cnt_used_entries].name, + rte_stats_strings[idx].name, +sizeof (xstats_names[0 ].name)); cnt_used_entries++; } num_q =RTE_MIN (dev->data->nb_rx_queues,RTE_ETHDEV_QUEUE_STAT_CNTRS );
lib/librte_hash/rte_cuckoo_hash.c +1 -1
@@ -379,7 +379,7 @@ rte_hash_create(const struct rte_hash_parameters *params) default_hash_func = (rte_hash_function )rte_hash_crc;#endif /* Setup hash context */ -snprintf (h->name,sizeof (h->name),"%s" , params->name); +strlcpy (h->name, params->name,sizeof (h->name)); h->entries = params->entries; h->key_len = params->key_len; h->key_entry_size = key_entry_size;
lib/librte_hash/rte_fbk_hash.c +1 -1
@@ -141,7 +141,7 @@ rte_fbk_hash_create(const struct rte_fbk_hash_params *params)#endif /* Set up hash table context. */ -snprintf (ht->name,sizeof (ht->name),"%s" , params->name); +strlcpy (ht->name, params->name,sizeof (ht->name)); ht->entries = params->entries; ht->entries_per_bucket = params->entries_per_bucket; ht->used_entries =0 ;
lib/librte_kni/rte_kni.c +3 -3
@@ -234,7 +234,7 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool, goto kni_fail; } -snprintf (kni->name,RTE_KNI_NAMESIZE ,"%s" , conf->name); +strlcpy (kni->name, conf->name,RTE_KNI_NAMESIZE );if (ops)memcpy (&kni->ops, ops,sizeof (struct rte_kni_ops ));
@@ -255,7 +255,7 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool,memcpy (dev_info.mac_addr, conf->mac_addr,ETHER_ADDR_LEN ); -snprintf (dev_info.name,RTE_KNI_NAMESIZE ,"%s" , conf->name); +strlcpy (dev_info.name, conf->name,RTE_KNI_NAMESIZE ); RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n", dev_info.bus, dev_info.devid, dev_info.function,
@@ -400,7 +400,7 @@ rte_kni_release(struct rte_kni *kni)if (te ==NULL ) goto unlock; -snprintf (dev_info.name,sizeof (dev_info.name),"%s" , kni->name); +strlcpy (dev_info.name, kni->name,sizeof (dev_info.name));if (ioctl (kni_fd,RTE_KNI_IOCTL_RELEASE , &dev_info) <0 ) {RTE_LOG (ERR ,KNI ,"Fail to release kni device\n" ); goto unlock;
lib/librte_lpm/rte_lpm.c +2 -2
@@ -205,7 +205,7 @@ rte_lpm_create_v20(const char *name, int socket_id, int max_rules,/* Save user arguments. */ lpm->max_rules = max_rules; -snprintf (lpm->name,sizeof (lpm->name),"%s" , name); +strlcpy (lpm->name, name,sizeof (lpm->name)); te->data = lpm;
@@ -308,7 +308,7 @@ rte_lpm_create_v1604(const char *name, int socket_id,/* Save user arguments. */ lpm->max_rules = config->max_rules; lpm->number_tbl8s = config->number_tbl8s; -snprintf (lpm->name,sizeof (lpm->name),"%s" , name); +strlcpy (lpm->name, name,sizeof (lpm->name)); te->data = lpm;
lib/librte_lpm/rte_lpm6.c +1 -1
@@ -352,7 +352,7 @@ rte_lpm6_create(const char *name, int socket_id,/* Save user arguments. */ lpm->max_rules = config->max_rules; lpm->number_tbl8s = config->number_tbl8s; -snprintf (lpm->name,sizeof (lpm->name),"%s" , name); +strlcpy (lpm->name, name,sizeof (lpm->name)); lpm->rules_tbl = rules_tbl; lpm->tbl8_pool = tbl8_pool; lpm->tbl8_hdrs = tbl8_hdrs;
lib/librte_mempool/rte_mempool.c +1 -1
@@ -864,7 +864,7 @@ rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,/* init the mempool structure */ mp = mz->addr;memset (mp,0 ,MEMPOOL_HEADER_SIZE (mp, cache_size)); - ret =snprintf (mp->name,sizeof (mp->name),"%s" , name); + ret =strlcpy (mp->name, name,sizeof (mp->name));if (ret <0 || ret >= (int )sizeof (mp->name)) { rte_errno =ENAMETOOLONG ; goto exit_unlock;
lib/librte_pdump/rte_pdump.c +4 -4
@@ -501,15 +501,15 @@ pdump_prepare_client_request(char *device, uint16_t queue, req->flags = flags; req->op = operation;if ((operation &ENABLE ) !=0 ) { -snprintf (req->data.en_v1.device, -sizeof (req->data.en_v1.device),"%s" , device); +strlcpy (req->data.en_v1.device, device, +sizeof (req->data.en_v1.device)); req->data.en_v1.queue = queue; req->data.en_v1.ring = ring; req->data.en_v1.mp = mp; req->data.en_v1.filter = filter; }else { -snprintf (req->data.dis_v1.device, -sizeof (req->data.dis_v1.device),"%s" , device); +strlcpy (req->data.dis_v1.device, device, +sizeof (req->data.dis_v1.device)); req->data.dis_v1.queue = queue; req->data.dis_v1.ring =NULL ; req->data.dis_v1.mp =NULL ;
lib/librte_pipeline/rte_pipeline.c +1 -1
@@ -214,7 +214,7 @@ rte_pipeline_create(struct rte_pipeline_params *params) }/* Save input parameters */ -snprintf (p->name,RTE_PIPELINE_MAX_NAME_SZ ,"%s" , params->name); +strlcpy (p->name, params->name,RTE_PIPELINE_MAX_NAME_SZ ); p->socket_id = params->socket_id; p->offset_port_id = params->offset_port_id;
lib/librte_ring/rte_ring.c +1 -1
@@ -78,7 +78,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count,/* init the ring structure */ memset (r,0 ,sizeof (*r)); - ret =snprintf (r->name,sizeof (r->name),"%s" , name); + ret =strlcpy (r->name, name,sizeof (r->name));if (ret <0 || ret >= (int )sizeof (r->name))return -ENAMETOOLONG ; r->flags = flags;
Signature · 1 line
2.20.1
Re: [PATCH 4/5] replace snprintf with strlcpy without adding extra include
lib/librte_ring/rte_ring.c rte_ring_init(struct rte_ring *r, const char *name, unsigned count, · line 82
Quoted 11 lines · On Wed, 3 Apr 2019 15:45:04 +0100 Bruce Richardson…
lib/librte_ring/rte_ring.c +1
-1@@ -78,7 +78,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count,/* init the ring structure */ memset (r,0 ,sizeof (*r)); - ret =snprintf (r->name,sizeof (r->name),"%s" , name); + ret =strlcpy (r->name, name,sizeof (r->name));if (ret <0 || ret >= (int )sizeof (r->name))
I would rather use the name length that is part of the header file.
if (strnlen(name, RTE_RING_NAMESIZE) == RTE_RING_NAMESIZE) return -ENAMETOOLONG; strlcpy(r->name, name, sizeof(r->name))
Re: [PATCH 4/5] replace snprintf with strlcpy without adding extra include
Quoted 17 lines · Stephen Hemminger
lib/librte_ring/rte_ring.c rte_ring_init(struct rte_ring *r, const char *name, unsigned count, · line 82
Quoted 11 lines · On Wed, 3 Apr 2019 15:45:04 +0100 Bruce Richardson…
lib/librte_ring/rte_ring.c +1
-1@@ -78,7 +78,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count,/* init the ring structure */ memset (r,0 ,sizeof (*r)); - ret =snprintf (r->name,sizeof (r->name),"%s" , name); + ret =strlcpy (r->name, name,sizeof (r->name));if (ret <0 || ret >= (int )sizeof (r->name))I would rather use the name length that is part of the header file.
Quoted 5 lines
if (strnlen(name, RTE_RING_NAMESIZE) == RTE_RING_NAMESIZE) return -ENAMETOOLONG; strlcpy(r->name, name, sizeof(r->name))
Yes, though honestly it's a matter of preference. Since this was a scripted replacement each change wasn't checked in too much detail other than a cursory check for correctness.
Re: [PATCH 4/5] replace snprintf with strlcpy without adding extra include
03/04/2019 17:56, Bruce Richardson:
Quoted 18 lines · Stephen Hemminger
Quoted 17 lines
lib/librte_ring/rte_ring.c rte_ring_init(struct rte_ring *r, const char *name, unsigned count, · line 82
Quoted 11 lines · On Wed, 3 Apr 2019 15:45:04 +0100 Bruce Richardson…
lib/librte_ring/rte_ring.c +1
-1@@ -78,7 +78,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count,/* init the ring structure */ memset (r,0 ,sizeof (*r)); - ret =snprintf (r->name,sizeof (r->name),"%s" , name); + ret =strlcpy (r->name, name,sizeof (r->name));if (ret <0 || ret >= (int )sizeof (r->name))I would rather use the name length that is part of the header file.
Quoted 8 lines
Quoted 5 lines
if (strnlen(name, RTE_RING_NAMESIZE) == RTE_RING_NAMESIZE) return -ENAMETOOLONG; strlcpy(r->name, name, sizeof(r->name))Yes, though honestly it's a matter of preference. Since this was a scripted replacement each change wasn't checked in too much detail other than a cursory check for correctness.
I prefer using sizeof/strlen rather than the constant used for allocation. Reasons are:
- it's easier to understand the intent
- it's easier to change how the allocation size is chosen
PATCH 5/5 replace snprintf with strlcpy
In reply to Bruce RichardsonDo a global replace of snprintf(..."%s",...) with strlcpy, adding in the rte_string_fns.h header if needed. The function changes in this patch were auto-generated via command:
spatch --sp-file devtools/cocci/strlcpy.cocci --dir . --in-place
and then the files edited using awk to add in the missing header:
gawk -i inplace '/include <rte_/ && ! seen { \
print "#include <rte_string_fns.h>"; seen=1} {print}'--- app/test-acl/main.c | 3 +- app/test-eventdev/evt_options.c | 3 +- app/test-pmd/cmdline_flow.c | 13 +++---- app/test/test_table_acl.c | 11 +++--- drivers/bus/dpaa/base/fman/of.c | 7 ++-- drivers/bus/pci/linux/pci_uio.c | 3 +- .../crypto/aesni_mb/rte_aesni_mb_pmd_ops.c | 3 +- .../aesni_mb/rte_aesni_mb_pmd_ops_compat.c | 3 +- drivers/crypto/ccp/rte_ccp_pmd.c | 6 ++-- .../scheduler/rte_cryptodev_scheduler.c | 8 ++--- drivers/event/opdl/opdl_ring.c | 3 +- drivers/net/af_packet/rte_eth_af_packet.c | 5 +-- drivers/net/ark/ark_pktchkr.c | 3 +- drivers/net/ark/ark_pktgen.c | 3 +- drivers/net/atlantic/atl_ethdev.c | 5 +-- drivers/net/bnx2x/bnx2x_ethdev.c | 8 ++--- drivers/net/bnxt/bnxt_stats.c | 36 +++++++++---------- drivers/net/dpaa/dpaa_ethdev.c | 8 ++--- drivers/net/e1000/igb_ethdev.c | 17 ++++----- drivers/net/ena/ena_ethdev.c | 11 +++--- drivers/net/failsafe/failsafe_eal.c | 6 ++-- drivers/net/i40e/rte_pmd_i40e.c | 5 +-- drivers/net/ice/ice_ethdev.c | 11 +++--- drivers/net/ixgbe/ixgbe_ethdev.c | 35 +++++++++--------- drivers/net/kni/rte_eth_kni.c | 3 +- drivers/net/liquidio/lio_ethdev.c | 5 +-- drivers/net/mvneta/mvneta_ethdev.c | 3 +- drivers/net/mvpp2/mrvl_ethdev.c | 5 +-- drivers/net/qede/qede_ethdev.c | 22 ++++++------ drivers/net/virtio/virtio_user/vhost_user.c | 3 +- .../net/virtio/virtio_user/virtio_user_dev.c | 3 +- examples/ethtool/lib/rte_ethtool.c | 8 ++--- examples/l2fwd-crypto/main.c | 4 +-- examples/vm_power_manager/channel_manager.c | 7 ++-- examples/vm_power_manager/channel_monitor.c | 14 ++++---- lib/librte_acl/rte_acl.c | 3 +- lib/librte_bbdev/rte_bbdev.c | 3 +- lib/librte_cfgfile/rte_cfgfile.c | 22 ++++++------ lib/librte_compressdev/rte_compressdev.c | 5 +-- lib/librte_compressdev/rte_compressdev_pmd.c | 3 +- lib/librte_cryptodev/rte_cryptodev_pmd.c | 3 +- lib/librte_eal/common/eal_common_options.c | 3 +- lib/librte_efd/rte_efd.c | 3 +- lib/librte_eventdev/rte_eventdev.c | 4 +-- lib/librte_flow_classify/rte_flow_classify.c | 4 +-- lib/librte_jobstats/rte_jobstats.c | 3 +- lib/librte_latencystats/rte_latencystats.c | 5 +-- lib/librte_member/rte_member.c | 3 +- lib/librte_mempool/rte_mempool_ops.c | 3 +- lib/librte_power/power_acpi_cpufreq.c | 3 +- lib/librte_power/power_pstate_cpufreq.c | 3 +- lib/librte_rawdev/rte_rawdev.c | 3 +- lib/librte_reorder/rte_reorder.c | 5 +-- 53 files changed, 201 insertions(+), 173 deletions(-)
app/test-acl/main.c +2 -1
@@ -2,6 +2,7 @@* Copyright(c) 2010-2014 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_acl.h> #include <getopt.h> #include <string.h>
@@ -928,7 +929,7 @@ print_usage(const char *prgname) n += rc; } -snprintf (buf + n,sizeof (buf) - n,"%s" , acl_alg[i].name); +strlcpy (buf + n, acl_alg[i].name,sizeof (buf) - n);fprintf (stdout, PRINT_USAGE_START
app/test-eventdev/evt_options.c +2 -1
@@ -7,6 +7,7 @@#include <inttypes.h> #include <getopt.h> +#include <rte_string_fns.h> #include <rte_common.h> #include <rte_eventdev.h> #include <rte_lcore.h>
@@ -110,7 +111,7 @@ evt_parse_timer_prod_type_burst(struct evt_options *opt,static int evt_parse_test_name (struct evt_options *opt,const char *arg) { -snprintf (opt->test_name,EVT_TEST_NAME_MAX_LEN ,"%s" , arg); +strlcpy (opt->test_name, arg,EVT_TEST_NAME_MAX_LEN );return 0 ; }
app/test-pmd/cmdline_flow.c +7 -6
@@ -13,6 +13,7 @@#include <arpa/inet.h> #include <sys/socket.h> +#include <rte_string_fns.h> #include <rte_common.h> #include <rte_eth_ctrl.h> #include <rte_ethdev.h>
@@ -4694,7 +4695,7 @@ comp_boolean(struct context *ctx, const struct token *token, (void )token;for (i =0 ; boolean_name[i]; ++i)if (buf && i == ent) -return snprintf (buf, size,"%s" , boolean_name[i]); +return strlcpy (buf, boolean_name[i], size);if (buf)return -1 ;return i;
@@ -4711,8 +4712,8 @@ comp_action(struct context *ctx, const struct token *token, (void )token;for (i =0 ; next_action[i]; ++i)if (buf && i == ent) -return snprintf (buf, size,"%s" , - token_list[next_action[i]].name); +return strlcpy (buf, token_list[next_action[i]].name, + size);if (buf)return -1 ;return i;
@@ -4776,7 +4777,7 @@ comp_vc_action_rss_type(struct context *ctx, const struct token *token,if (!buf)return i +1 ;if (ent < i) -return snprintf (buf, size,"%s" , rss_type_table[ent].str); +return strlcpy (buf, rss_type_table[ent].str, size);if (ent == i)return snprintf (buf, size,"end" );return -1 ;
@@ -4961,7 +4962,7 @@ cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,if (index >= i)return -1 ; token = &token_list[list[index]]; -snprintf (dst, size,"%s" , token->name); +strlcpy (dst, token->name, size);/* Save index for cmd_flow_get_help(). */ ctx->prev = list[index];return 0 ;
@@ -4978,7 +4979,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. */ -snprintf (dst, size,"%s" , (token->type ? token->type :"TOKEN" )); +strlcpy (dst, (token->type ? token->type :"TOKEN" ), size);if (token->help) cmd_flow.help_str = token->help;else
app/test/test_table_acl.c +6 -5
@@ -2,6 +2,7 @@* Copyright(c) 2010-2014 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_hexdump.h> #include "test_table.h" #include "test_table_acl.h"
@@ -467,7 +468,7 @@ setup_acl_pipeline(void)memset (&keys[n],0 ,sizeof (struct rte_table_acl_rule_add_params )); key_array[n] = &keys[n]; -snprintf (line,sizeof (line),"%s" , lines[n]); +strlcpy (line, lines[n],sizeof (line));printf ("PARSING [%s]\n" , line); ret =parser (line, &keys[n]);
@@ -509,7 +510,7 @@ setup_acl_pipeline(void)memset (&keys[n],0 ,sizeof (struct rte_table_acl_rule_delete_params )); key_array[n] = &keys[n]; -snprintf (line,sizeof (line),"%s" , lines[n]); +strlcpy (line, lines[n],sizeof (line));printf ("PARSING [%s]\n" , line); ret =parse_cb_ipv4_rule_del (line, &keys[n]);
@@ -545,7 +546,7 @@ setup_acl_pipeline(void) parser = parse_cb_ipv4_rule;for (n =1 ; n <=5 ; n++) { -snprintf (line,sizeof (line),"%s" , lines[n-1 ]); +strlcpy (line, lines[n -1 ],sizeof (line));printf ("PARSING [%s]\n" , line); ret =parser (line, &rule_params);
@@ -571,7 +572,7 @@ setup_acl_pipeline(void)/* delete a few rules */ for (n =2 ; n <=3 ; n++) { -snprintf (line,sizeof (line),"%s" , lines[n-1 ]); +strlcpy (line, lines[n -1 ],sizeof (line));printf ("PARSING [%s]\n" , line); ret =parser (line, &rule_params);
@@ -598,7 +599,7 @@ setup_acl_pipeline(void)/* Try to add duplicates */ for (n =1 ; n <=5 ; n++) { -snprintf (line,sizeof (line),"%s" , lines[n-1 ]); +strlcpy (line, lines[n -1 ],sizeof (line));printf ("PARSING [%s]\n" , line); ret =parser (line, &rule_params);
drivers/bus/dpaa/base/fman/of.c +4 -3
@@ -6,6 +6,7 @@*/ #include <of.h> +#include <rte_string_fns.h> #include <rte_dpaa_logs.h> static int alive;
@@ -60,7 +61,7 @@ process_file(struct dirent *dent, struct dt_dir *parent)return ; } f->node.is_file =1 ; -snprintf (f->node.node.name,NAME_MAX ,"%s" , dent->d_name); +strlcpy (f->node.node.name, dent->d_name,NAME_MAX );snprintf (f->node.node.full_name,PATH_MAX ,"%s/%s" , parent->node.node.full_name, dent->d_name); f->parent = parent;
@@ -117,8 +118,8 @@ iterate_dir(struct dirent **d, int num, struct dt_dir *dt)perror ("malloc" );return -ENOMEM ; } -snprintf (subdir->node.node.name,NAME_MAX ,"%s" , - d[loop]->d_name); +strlcpy (subdir->node.node.name, d[loop]->d_name, +NAME_MAX );snprintf (subdir->node.node.full_name,PATH_MAX ,"%s/%s" , dt->node.node.full_name, d[loop]->d_name);
drivers/bus/pci/linux/pci_uio.c +2 -1
@@ -16,6 +16,7 @@#include <sys/io.h> #endif +#include <rte_string_fns.h> #include <rte_log.h> #include <rte_pci.h> #include <rte_bus_pci.h>
@@ -268,7 +269,7 @@ pci_uio_alloc_resource(struct rte_pci_device *dev, goto error; } -snprintf ((*uio_res)->path,sizeof ((*uio_res)->path),"%s" , devname); +strlcpy ((*uio_res)->path, devname,sizeof ((*uio_res)->path));memcpy (&(*uio_res)->pci_addr, &dev->addr,sizeof ((*uio_res)->pci_addr));return 0 ;
drivers/crypto/aesni_mb/rte_aesni_mb_pmd_ops.c +2 -1
@@ -4,6 +4,7 @@#include <string.h> +#include <rte_string_fns.h> #include <rte_common.h> #include <rte_malloc.h> #include <rte_cryptodev_pmd.h>
@@ -614,7 +615,7 @@ aesni_mb_pmd_qp_create_processed_ops_ring(struct aesni_mb_qp *qp,struct rte_ring *r;char ring_name[RTE_CRYPTODEV_NAME_MAX_LEN ]; -unsigned int n =snprintf (ring_name,sizeof (ring_name),"%s" , qp->name); +unsigned int n =strlcpy (ring_name, qp->name,sizeof (ring_name));if (n >=sizeof (ring_name))return NULL ;
drivers/crypto/aesni_mb/rte_aesni_mb_pmd_ops_compat.c +2 -1
@@ -4,6 +4,7 @@#include <string.h> +#include <rte_string_fns.h> #include <rte_common.h> #include <rte_malloc.h> #include <rte_cryptodev_pmd.h>
@@ -565,7 +566,7 @@ aesni_mb_pmd_qp_create_processed_ops_ring(struct aesni_mb_qp *qp,struct rte_ring *r;char ring_name[RTE_CRYPTODEV_NAME_MAX_LEN ]; -unsigned int n =snprintf (ring_name,sizeof (ring_name),"%s" , qp->name); +unsigned int n =strlcpy (ring_name, qp->name,sizeof (ring_name));if (n >=sizeof (ring_name))return NULL ;
drivers/crypto/ccp/rte_ccp_pmd.c +3 -3
@@ -2,6 +2,7 @@* Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved. */ +#include <rte_string_fns.h> #include <rte_bus_pci.h> #include <rte_bus_vdev.h> #include <rte_common.h>
@@ -298,9 +299,8 @@ cryptodev_ccp_create(const char *name,uint8_t cryptodev_cnt =0 ;if (init_params->def_p.name[0 ] =='\0' ) -snprintf (init_params->def_p.name, -sizeof (init_params->def_p.name), -"%s" , name); +strlcpy (init_params->def_p.name, name, +sizeof (init_params->def_p.name)); dev = rte_cryptodev_pmd_create(init_params->def_p.name, &vdev->device,
drivers/crypto/scheduler/rte_cryptodev_scheduler.c +4 -4
@@ -1,6 +1,7 @@/* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2017 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_reorder.h> #include <rte_cryptodev.h> #include <rte_cryptodev_pmd.h>
@@ -443,8 +444,7 @@ rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id, RTE_CRYPTODEV_NAME_MAX_LEN);return -EINVAL ; } -snprintf (sched_ctx->name,sizeof (sched_ctx->name),"%s" , - scheduler->name); +strlcpy (sched_ctx->name, scheduler->name,sizeof (sched_ctx->name));if (strlen (scheduler->description) >RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN -1 ) {
@@ -453,8 +453,8 @@ rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id, RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN - 1);return -EINVAL ; } -snprintf (sched_ctx->description,sizeof (sched_ctx->description),"%s" , - scheduler->description); +strlcpy (sched_ctx->description, scheduler->description, +sizeof (sched_ctx->description));/* load scheduler instance operations functions */ sched_ctx->ops.config_queue_pair = scheduler->ops->config_queue_pair;
drivers/event/opdl/opdl_ring.c +2 -1
@@ -7,6 +7,7 @@#include <stdint.h> #include <stdio.h> +#include <rte_string_fns.h> #include <rte_branch_prediction.h> #include <rte_debug.h> #include <rte_lcore.h>
@@ -944,7 +945,7 @@ opdl_ring_create(const char *name, uint32_t num_slots, uint32_t slot_size,/* Initialise opdl_ring queue */ memset (t,0 ,sizeof (*t)); -snprintf (t->name,sizeof (t->name),"%s" , name); +strlcpy (t->name, name,sizeof (t->name)); t->socket = socket; t->num_slots = num_slots; t->mask = num_slots -1 ;
drivers/net/af_packet/rte_eth_af_packet.c +3 -2
@@ -6,6 +6,7 @@* All rights reserved. */ +#include <rte_string_fns.h> #include <rte_mbuf.h> #include <rte_ethdev_driver.h> #include <rte_ethdev_vdev.h>
@@ -442,7 +443,7 @@ eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)if (s <0 )return -EINVAL ; -snprintf (ifr.ifr_name,IFNAMSIZ ,"%s" , internals->if_name); +strlcpy (ifr.ifr_name, internals->if_name,IFNAMSIZ ); ret =ioctl (s,SIOCSIFMTU , &ifr);close (s);
@@ -462,7 +463,7 @@ eth_dev_change_flags(char *if_name, uint32_t flags, uint32_t mask)if (s <0 )return ; -snprintf (ifr.ifr_name,IFNAMSIZ ,"%s" , if_name); +strlcpy (ifr.ifr_name, if_name,IFNAMSIZ );if (ioctl (s,SIOCGIFFLAGS , &ifr) <0 ) goto out; ifr.ifr_flags &= mask;
drivers/net/ark/ark_pktchkr.c +2 -1
@@ -7,6 +7,7 @@#include <locale.h> #include <unistd.h> +#include <rte_string_fns.h> #include <rte_ethdev_driver.h> #include <rte_malloc.h>
@@ -347,7 +348,7 @@ set_arg(char *arg, char *val) o->v.INT =atoll (val);break ;case OTSTRING : -snprintf (o->v.STR,ARK_MAX_STR_LEN ,"%s" , val); +strlcpy (o->v.STR, val,ARK_MAX_STR_LEN );break ; }return 1 ;
drivers/net/ark/ark_pktgen.c +2 -1
@@ -7,6 +7,7 @@#include <locale.h> #include <unistd.h> +#include <rte_string_fns.h> #include <rte_eal.h> #include <rte_ethdev_driver.h>
@@ -329,7 +330,7 @@ pmd_set_arg(char *arg, char *val) o->v.INT =atoll (val);break ;case OTSTRING : -snprintf (o->v.STR,ARK_MAX_STR_LEN ,"%s" , val); +strlcpy (o->v.STR, val,ARK_MAX_STR_LEN );break ; }return 1 ;
drivers/net/atlantic/atl_ethdev.c +3 -2
@@ -2,6 +2,7 @@* Copyright(c) 2018 Aquantia Corporation */ +#include <rte_string_fns.h> #include <rte_ethdev_pci.h> #include "atl_ethdev.h"
@@ -755,8 +756,8 @@ atl_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,return RTE_DIM (atl_xstats_tbl);for (i =0 ; i < size && i <RTE_DIM (atl_xstats_tbl); i++) -snprintf (xstats_names[i].name,RTE_ETH_XSTATS_NAME_SIZE ,"%s" , - atl_xstats_tbl[i].name); +strlcpy (xstats_names[i].name, atl_xstats_tbl[i].name, +RTE_ETH_XSTATS_NAME_SIZE );return i; }
drivers/net/bnx2x/bnx2x_ethdev.c +4 -4
@@ -8,6 +8,7 @@#include "bnx2x.h" #include "bnx2x_rxtx.h" +#include <rte_string_fns.h> #include <rte_dev.h> #include <rte_ethdev_pci.h> #include <rte_alarm.h>
@@ -445,10 +446,9 @@ bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev,if (xstats_names !=NULL )for (i =0 ; i < stat_cnt; i++) -snprintf (xstats_names[i].name, -sizeof (xstats_names[i].name), -"%s" , - bnx2x_xstats_strings[i].name); +strlcpy (xstats_names[i].name, + bnx2x_xstats_strings[i].name, +sizeof (xstats_names[i].name));return stat_cnt; }
drivers/net/bnxt/bnxt_stats.c +16 -20
@@ -5,6 +5,7 @@#include <inttypes.h> +#include <rte_string_fns.h> #include <rte_byteorder.h> #include "bnxt.h"
@@ -491,41 +492,36 @@ int bnxt_dev_xstats_get_names_op(__rte_unused struct rte_eth_dev *eth_dev, count =0 ;for (i =0 ; i <RTE_DIM (bnxt_rx_stats_strings); i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - bnxt_rx_stats_strings[i].name); +strlcpy (xstats_names[count].name, + bnxt_rx_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }for (i =0 ; i <RTE_DIM (bnxt_tx_stats_strings); i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - bnxt_tx_stats_strings[i].name); +strlcpy (xstats_names[count].name, + bnxt_tx_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; } -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - bnxt_func_stats_strings[4 ].name); +strlcpy (xstats_names[count].name, + bnxt_func_stats_strings[4 ].name, +sizeof (xstats_names[count].name)); count++;for (i =0 ; i <RTE_DIM (bnxt_rx_ext_stats_strings); i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - bnxt_rx_ext_stats_strings[i].name); +strlcpy (xstats_names[count].name, + bnxt_rx_ext_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }for (i =0 ; i <RTE_DIM (bnxt_tx_ext_stats_strings); i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - bnxt_tx_ext_stats_strings[i].name); +strlcpy (xstats_names[count].name, + bnxt_tx_ext_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }
drivers/net/dpaa/dpaa_ethdev.c +4 -4
@@ -15,6 +15,7 @@#include <sys/types.h> #include <sys/syscall.h> +#include <rte_string_fns.h> #include <rte_byteorder.h> #include <rte_common.h> #include <rte_interrupts.h>
@@ -439,10 +440,9 @@ dpaa_xstats_get_names(__rte_unused struct rte_eth_dev *dev,if (xstats_names !=NULL )for (i =0 ; i < stat_cnt; i++) -snprintf (xstats_names[i].name, -sizeof (xstats_names[i].name), -"%s" , - dpaa_xstats_strings[i].name); +strlcpy (xstats_names[i].name, + dpaa_xstats_strings[i].name, +sizeof (xstats_names[i].name));return stat_cnt; }
drivers/net/e1000/igb_ethdev.c +9 -8
@@ -8,6 +8,7 @@#include <stdint.h> #include <stdarg.h> +#include <rte_string_fns.h> #include <rte_common.h> #include <rte_interrupts.h> #include <rte_byteorder.h>
@@ -1904,8 +1905,8 @@ static int eth_igb_xstats_get_names(__rte_unused struct rte_eth_dev *dev,/* Note: limit checked in rte_eth_xstats_names() */ for (i =0 ; i <IGB_NB_XSTATS ; i++) { -snprintf (xstats_names[i].name,sizeof (xstats_names[i].name), -"%s" , rte_igb_stats_strings[i].name); +strlcpy (xstats_names[i].name, rte_igb_stats_strings[i].name, +sizeof (xstats_names[i].name)); }return IGB_NB_XSTATS ;
@@ -1922,9 +1923,9 @@ static int eth_igb_xstats_get_names_by_id(struct rte_eth_dev *dev,return IGB_NB_XSTATS ;for (i =0 ; i <IGB_NB_XSTATS ; i++) -snprintf (xstats_names[i].name, -sizeof (xstats_names[i].name), -"%s" , rte_igb_stats_strings[i].name); +strlcpy (xstats_names[i].name, + rte_igb_stats_strings[i].name, +sizeof (xstats_names[i].name));return IGB_NB_XSTATS ;
@@ -2071,9 +2072,9 @@ static int eth_igbvf_xstats_get_names(__rte_unused struct rte_eth_dev *dev,if (xstats_names !=NULL )for (i =0 ; i <IGBVF_NB_XSTATS ; i++) { -snprintf (xstats_names[i].name, -sizeof (xstats_names[i].name),"%s" , - rte_igbvf_stats_strings[i].name); +strlcpy (xstats_names[i].name, + rte_igbvf_stats_strings[i].name, +sizeof (xstats_names[i].name)); }return IGBVF_NB_XSTATS ; }
drivers/net/ena/ena_ethdev.c +5 -6
@@ -31,6 +31,7 @@* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <rte_string_fns.h> #include <rte_ether.h> #include <rte_ethdev_driver.h> #include <rte_ethdev_pci.h>
@@ -421,13 +422,11 @@ static void ena_config_host_info(struct ena_com_dev *ena_dev) host_info->os_type =ENA_ADMIN_OS_DPDK ; host_info->kernel_ver =RTE_VERSION ; -snprintf ((char *)host_info->kernel_ver_str, -sizeof (host_info->kernel_ver_str), -"%s" ,rte_version ()); +strlcpy ((char *)host_info->kernel_ver_str,rte_version (), +sizeof (host_info->kernel_ver_str)); host_info->os_dist =RTE_VERSION ; -snprintf ((char *)host_info->os_dist_str, -sizeof (host_info->os_dist_str), -"%s" ,rte_version ()); +strlcpy ((char *)host_info->os_dist_str,rte_version (), +sizeof (host_info->os_dist_str)); host_info->driver_version = (DRV_MODULE_VER_MAJOR ) | (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
drivers/net/failsafe/failsafe_eal.c +4 -2
@@ -3,6 +3,7 @@* Copyright 2017 Mellanox Technologies, Ltd */ +#include <rte_string_fns.h> #include <rte_malloc.h> #include "failsafe_private.h"
@@ -84,8 +85,9 @@ fs_bus_init(struct rte_eth_dev *dev)snprintf (devstr,sizeof (devstr),"%s,%s" , probed_da->name, probed_da->args); else - snprintf(devstr, sizeof(devstr), "%s", - rte_eth_devices[pid].device->name); +strlcpy (devstr , + rte_eth_devices[pid].device->name, + sizeof(devstr)); ret =rte_devargs_parse (da, devstr);if (ret) { ERROR("Probed devargs parsing failed with code"
drivers/net/i40e/rte_pmd_i40e.c +3 -2
@@ -2,6 +2,7 @@* Copyright(c) 2010-2017 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_malloc.h> #include <rte_tailq.h>
@@ -1983,8 +1984,8 @@ int rte_pmd_i40e_get_ddp_info(uint8_t *pkg_buff, uint32_t pkg_size, tlv = (struct i40e_profile_tlv_section_record *)&proto[1 ];for (i = j =0 ; i < nb_rec; j++) { pinfo[j].proto_id = tlv->data[0 ]; -snprintf (pinfo[j].name,I40E_DDP_NAME_SIZE ,"%s" , - (const char *)&tlv->data[1 ]); +strlcpy (pinfo[j].name, (const char *)&tlv->data[1 ], +I40E_DDP_NAME_SIZE ); i += tlv->len; tlv = &tlv[tlv->len]; }
drivers/net/ice/ice_ethdev.c +5 -6
@@ -2,6 +2,7 @@* Copyright(c) 2018 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_ethdev_pci.h> #include <stdio.h>
@@ -3437,17 +3438,15 @@ static int ice_xstats_get_names(__rte_unused struct rte_eth_dev *dev,/* Get stats from ice_eth_stats struct */ for (i =0 ; i <ICE_NB_ETH_XSTATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , ice_stats_strings[i].name); +strlcpy (xstats_names[count].name, ice_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }/* Get individiual stats from ice_hw_port struct */ for (i =0 ; i <ICE_NB_HW_PORT_XSTATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , ice_hw_port_strings[i].name); +strlcpy (xstats_names[count].name, ice_hw_port_strings[i].name, +sizeof (xstats_names[count].name)); count++; }
drivers/net/ixgbe/ixgbe_ethdev.c +16 -19
@@ -11,6 +11,7 @@#include <stdarg.h> #include <inttypes.h> #include <netinet/in.h> +#include <rte_string_fns.h> #include <rte_byteorder.h> #include <rte_common.h> #include <rte_cycles.h>
@@ -3293,19 +3294,17 @@ static int ixgbe_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,/* Extended stats from ixgbe_hw_stats */ for (i =0 ; i <IXGBE_NB_HW_STATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - rte_ixgbe_stats_strings[i].name); +strlcpy (xstats_names[count].name, + rte_ixgbe_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }/* MACsec Stats */ for (i =0 ; i <IXGBE_NB_MACSEC_STATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - rte_ixgbe_macsec_strings[i].name); +strlcpy (xstats_names[count].name, + rte_ixgbe_macsec_strings[i].name, +sizeof (xstats_names[count].name)); count++; }
@@ -3353,19 +3352,17 @@ static int ixgbe_dev_xstats_get_names_by_id(/* Extended stats from ixgbe_hw_stats */ for (i =0 ; i <IXGBE_NB_HW_STATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - rte_ixgbe_stats_strings[i].name); +strlcpy (xstats_names[count].name, + rte_ixgbe_stats_strings[i].name, +sizeof (xstats_names[count].name)); count++; }/* MACsec Stats */ for (i =0 ; i <IXGBE_NB_MACSEC_STATS ; i++) { -snprintf (xstats_names[count].name, -sizeof (xstats_names[count].name), -"%s" , - rte_ixgbe_macsec_strings[i].name); +strlcpy (xstats_names[count].name, + rte_ixgbe_macsec_strings[i].name, +sizeof (xstats_names[count].name)); count++; }
@@ -3422,9 +3419,9 @@ static int ixgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,if (xstats_names !=NULL )for (i =0 ; i <IXGBEVF_NB_XSTATS ; i++) -snprintf (xstats_names[i].name, -sizeof (xstats_names[i].name), -"%s" , rte_ixgbevf_stats_strings[i].name); +strlcpy (xstats_names[i].name, + rte_ixgbevf_stats_strings[i].name, +sizeof (xstats_names[i].name));return IXGBEVF_NB_XSTATS ; }
drivers/net/kni/rte_eth_kni.c +2 -1
@@ -6,6 +6,7 @@#include <pthread.h> #include <unistd.h> +#include <rte_string_fns.h> #include <rte_ethdev_driver.h> #include <rte_ethdev_vdev.h> #include <rte_kni.h>
@@ -126,7 +127,7 @@ eth_kni_start(struct rte_eth_dev *dev)const char *name = dev->device->name +4 ;/* remove net_ */ mb_pool = internals->rx_queues[0 ].mb_pool; -snprintf (conf.name,RTE_KNI_NAMESIZE ,"%s" , name); +strlcpy (conf.name, name,RTE_KNI_NAMESIZE ); conf.force_bind =0 ; conf.group_id = port_id; conf.mbuf_size =
drivers/net/liquidio/lio_ethdev.c +3 -2
@@ -2,6 +2,7 @@* Copyright(c) 2017 Cavium, Inc */ +#include <rte_string_fns.h> #include <rte_ethdev_driver.h> #include <rte_ethdev_pci.h> #include <rte_cycles.h>
@@ -1781,8 +1782,8 @@ lio_dev_configure(struct rte_eth_dev *eth_dev) goto nic_config_fail; } -snprintf (lio_dev->firmware_version,LIO_FW_VERSION_LENGTH ,"%s" , - resp->cfg_info.lio_firmware_version); +strlcpy (lio_dev->firmware_version, + resp->cfg_info.lio_firmware_version,LIO_FW_VERSION_LENGTH );lio_swap_8B_data ((uint64_t *)(&resp->cfg_info),sizeof (struct octeon_if_cfg_info ) >>3 );
drivers/net/mvneta/mvneta_ethdev.c +2 -1
@@ -4,6 +4,7 @@* All rights reserved. */ +#include <rte_string_fns.h> #include <rte_ethdev_driver.h> #include <rte_kvargs.h> #include <rte_bus_vdev.h>
@@ -347,7 +348,7 @@ mvneta_dev_start(struct rte_eth_dev *dev)if (priv->ppio)return mvneta_dev_set_link_up (dev); -snprintf (match,sizeof (match),"%s" , dev->data->name); +strlcpy (match, dev->data->name,sizeof (match)); priv->ppio_params.match = match; priv->ppio_params.inqs_params.mtu = dev->data->mtu;
drivers/net/mvpp2/mrvl_ethdev.c +3 -2
@@ -4,6 +4,7 @@* All rights reserved. */ +#include <rte_string_fns.h> #include <rte_ethdev_driver.h> #include <rte_kvargs.h> #include <rte_log.h>
@@ -1388,8 +1389,8 @@ mrvl_xstats_get_names(struct rte_eth_dev *dev __rte_unused,return RTE_DIM (mrvl_xstats_tbl);for (i =0 ; i < size && i <RTE_DIM (mrvl_xstats_tbl); i++) -snprintf (xstats_names[i].name,RTE_ETH_XSTATS_NAME_SIZE ,"%s" , - mrvl_xstats_tbl[i].name); +strlcpy (xstats_names[i].name, mrvl_xstats_tbl[i].name, +RTE_ETH_XSTATS_NAME_SIZE );return size; }
drivers/net/qede/qede_ethdev.c +10 -12
@@ -5,6 +5,7 @@*/ #include "qede_ethdev.h" +#include <rte_string_fns.h> #include <rte_alarm.h> #include <rte_version.h> #include <rte_kvargs.h>
@@ -1591,27 +1592,24 @@ qede_get_xstats_names(struct rte_eth_dev *dev,if (xstats_names !=NULL ) {for (i =0 ; i <RTE_DIM (qede_xstats_strings); i++) { -snprintf (xstats_names[stat_idx].name, -sizeof (xstats_names[stat_idx].name), -"%s" , - qede_xstats_strings[i].name); +strlcpy (xstats_names[stat_idx].name, + qede_xstats_strings[i].name, +sizeof (xstats_names[stat_idx].name)); stat_idx++; }if (ECORE_IS_BB (edev)) {for (i =0 ; i <RTE_DIM (qede_bb_xstats_strings); i++) { -snprintf (xstats_names[stat_idx].name, -sizeof (xstats_names[stat_idx].name), -"%s" , - qede_bb_xstats_strings[i].name); +strlcpy (xstats_names[stat_idx].name, + qede_bb_xstats_strings[i].name, +sizeof (xstats_names[stat_idx].name)); stat_idx++; } }else {for (i =0 ; i <RTE_DIM (qede_ah_xstats_strings); i++) { -snprintf (xstats_names[stat_idx].name, -sizeof (xstats_names[stat_idx].name), -"%s" , - qede_ah_xstats_strings[i].name); +strlcpy (xstats_names[stat_idx].name, + qede_ah_xstats_strings[i].name, +sizeof (xstats_names[stat_idx].name)); stat_idx++; } }
drivers/net/virtio/virtio_user/vhost_user.c +2 -1
@@ -11,6 +11,7 @@#include <string.h> #include <errno.h> +#include <rte_string_fns.h> #include <rte_fbarray.h> #include <rte_eal_memconfig.h>
@@ -424,7 +425,7 @@ vhost_user_setup(struct virtio_user_dev *dev)memset (&un,0 ,sizeof (un)); un.sun_family =AF_UNIX ; -snprintf (un.sun_path,sizeof (un.sun_path),"%s" , dev->path); +strlcpy (un.sun_path, dev->path,sizeof (un.sun_path));if (dev->is_server) { dev->listenfd = fd;
drivers/net/virtio/virtio_user/virtio_user_dev.c +2 -1
@@ -13,6 +13,7 @@#include <sys/types.h> #include <sys/stat.h> +#include <rte_string_fns.h> #include <rte_eal_memconfig.h> #include "vhost.h"
@@ -429,7 +430,7 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, int server, int mrg_rxbuf, int in_order, int packed_vq) { pthread_mutex_init(&dev->mutex, NULL); -snprintf (dev->path,PATH_MAX ,"%s" , path); +strlcpy (dev->path, path,PATH_MAX ); dev->started =0 ; dev->max_queue_pairs = queues; dev->queue_pairs =1 ;/* mq disabled by default */
examples/ethtool/lib/rte_ethtool.c +4 -4
@@ -4,6 +4,7 @@#include <stdio.h> #include <string.h> #include <stdint.h> +#include <rte_string_fns.h> #include <rte_version.h> #include <rte_ethdev.h> #include <rte_ether.h>
@@ -43,10 +44,9 @@ rte_ethtool_get_drvinfo(uint16_t port_id, struct ethtool_drvinfo *drvinfo)memset (&dev_info,0 ,sizeof (dev_info));rte_eth_dev_info_get (port_id, &dev_info); -snprintf (drvinfo->driver,sizeof (drvinfo->driver),"%s" , - dev_info.driver_name); -snprintf (drvinfo->version,sizeof (drvinfo->version),"%s" , -rte_version ()); +strlcpy (drvinfo->driver, dev_info.driver_name, +sizeof (drvinfo->driver)); +strlcpy (drvinfo->version,rte_version (),sizeof (drvinfo->version));/* TODO: replace bus_info by rte_devargs.name */ if (dev_info.device) bus =rte_bus_find_by_device (dev_info.device);
examples/l2fwd-crypto/main.c +2 -2
@@ -19,6 +19,7 @@#include <fcntl.h> #include <unistd.h> +#include <rte_string_fns.h> #include <rte_atomic.h> #include <rte_branch_prediction.h> #include <rte_common.h>
@@ -1199,8 +1200,7 @@ l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,if (strcmp (lgopts[option_index].name,"cdev_type" ) ==0 ) { retval =parse_cryptodev_type (&options->type, optarg);if (retval ==0 ) -snprintf (options->string_type,MAX_STR_LEN , -"%s" , optarg); +strlcpy (options->string_type, optarg,MAX_STR_LEN );return retval; }
examples/vm_power_manager/channel_manager.c +4 -3
@@ -17,6 +17,7 @@#include <sys/socket.h> #include <sys/select.h> +#include <rte_string_fns.h> #include <rte_malloc.h> #include <rte_memory.h> #include <rte_mempool.h>
@@ -415,7 +416,7 @@ add_all_channels(const char *vm_name) !strncmp(dir->d_name, "..", 2))continue ; -snprintf (socket_name,sizeof (socket_name),"%s" , dir->d_name); +strlcpy (socket_name, dir->d_name,sizeof (socket_name)); remaining = socket_name;/* Extract vm_name from "<vm_name>.<channel_num>" */ token =strsep (&remaining,"." );
@@ -562,8 +563,8 @@ add_host_channel(void) "channel '%s'\n", socket_path);return 0 ; } -snprintf (chan_info->channel_path, -sizeof (chan_info->channel_path),"%s" , socket_path); +strlcpy (chan_info->channel_path, socket_path, +sizeof (chan_info->channel_path));if (setup_host_channel_info (&chan_info,0 ) <0 ) {rte_free (chan_info);return 0 ;
examples/vm_power_manager/channel_monitor.c +8 -6
@@ -21,6 +21,7 @@#else #pragma message "Jansson dev libs unavailable, not including JSON parsing"#endif +#include <rte_string_fns.h> #include <rte_log.h> #include <rte_memory.h> #include <rte_malloc.h>
@@ -161,7 +162,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt)strcpy (pkt->vm_name,json_string_value (value)); }else if (!strcmp (key,"command" )) {char command[32 ]; -snprintf (command,32 ,"%s" ,json_string_value (value)); +strlcpy (command,json_string_value (value),32 );if (!strcmp (command,"power" )) { pkt->command =CPU_POWER ; }else if (!strcmp (command,"create" )) {
@@ -175,7 +176,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt) } }else if (!strcmp (key,"policy_type" )) {char command[32 ]; -snprintf (command,32 ,"%s" ,json_string_value (value)); +strlcpy (command,json_string_value (value),32 );if (!strcmp (command,"TIME" )) { pkt->policy_to_use =TIME ; }else if (!strcmp (command,"TRAFFIC" )) {
@@ -191,7 +192,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt) } }else if (!strcmp (key,"workload" )) {char command[32 ]; -snprintf (command,32 ,"%s" ,json_string_value (value)); +strlcpy (command,json_string_value (value),32 );if (!strcmp (command,"HIGH" )) { pkt->workload =HIGH ; }else if (!strcmp (command,"MEDIUM" )) {
@@ -237,8 +238,9 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt)for (i =0 ; i < size; i++) {char mac[32 ]; -snprintf (mac,32 ,"%s" ,json_string_value ( -json_array_get (value, i))); +strlcpy (mac, +json_string_value (json_array_get (value, i)), +32 );set_policy_mac (pkt, i, mac); } pkt->nb_mac_to_monitor = size;
@@ -250,7 +252,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt) (uint32_t )json_integer_value (value); }else if (!strcmp (key,"unit" )) {char unit[32 ]; -snprintf (unit,32 ,"%s" ,json_string_value (value)); +strlcpy (unit,json_string_value (value),32 );if (!strcmp (unit,"SCALE_UP" )) { pkt->unit =CPU_POWER_SCALE_UP ; }else if (!strcmp (unit,"SCALE_DOWN" )) {
lib/librte_acl/rte_acl.c +2 -1
@@ -2,6 +2,7 @@* Copyright(c) 2010-2014 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_acl.h> #include "acl.h"
@@ -249,7 +250,7 @@ rte_acl_create(const struct rte_acl_param *param) ctx->rule_sz = param->rule_size; ctx->socket_id = param->socket_id; ctx->alg = rte_acl_default_classify; -snprintf (ctx->name,sizeof (ctx->name),"%s" , param->name); +strlcpy (ctx->name, param->name,sizeof (ctx->name)); te->data = (void *) ctx;
lib/librte_bbdev/rte_bbdev.c +2 -1
@@ -6,6 +6,7 @@#include <string.h> #include <stdbool.h> +#include <rte_string_fns.h> #include <rte_compat.h> #include <rte_common.h> #include <rte_errno.h>
@@ -214,7 +215,7 @@ rte_bbdev_allocate(const char *name) bbdev->data->dev_id = dev_id; bbdev->state =RTE_BBDEV_INITIALIZED ; - ret =snprintf (bbdev->data->name,RTE_BBDEV_NAME_MAX_LEN ,"%s" , name); + ret =strlcpy (bbdev->data->name, name,RTE_BBDEV_NAME_MAX_LEN );if ((ret <0 ) || (ret >=RTE_BBDEV_NAME_MAX_LEN )) {rte_bbdev_log (ERR ,"Copying device name \"%s\" failed" , name);return NULL ;
lib/librte_cfgfile/rte_cfgfile.c +10 -12
@@ -7,6 +7,7 @@#include <string.h> #include <ctype.h> #include <errno.h> +#include <rte_string_fns.h> #include <rte_common.h> #include "rte_cfgfile.h"
@@ -113,9 +114,8 @@ _add_entry(struct rte_cfgfile_section *section, const char *entryname,struct rte_cfgfile_entry *curr_entry = §ion->entries[section->num_entries]; -snprintf (curr_entry->name,sizeof (curr_entry->name),"%s" , entryname); -snprintf (curr_entry->value, -sizeof (curr_entry->value),"%s" , entryvalue); +strlcpy (curr_entry->name, entryname,sizeof (curr_entry->name)); +strlcpy (curr_entry->value, entryvalue,sizeof (curr_entry->value)); section->num_entries++;return 0 ;
@@ -341,8 +341,8 @@ rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname) cfg->allocated_sections +=CFG_ALLOC_SECTION_BATCH ; } -snprintf (cfg->sections[cfg->num_sections].name, -sizeof (cfg->sections[0 ].name),"%s" , sectionname); +strlcpy (cfg->sections[cfg->num_sections].name, sectionname, +sizeof (cfg->sections[0 ].name)); cfg->sections[cfg->num_sections].num_entries =0 ; cfg->num_sections++;
@@ -392,9 +392,8 @@ int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,for (i =0 ; i < curr_section->num_entries; i++)if (!strcmp (curr_section->entries[i].name, entryname)) { -snprintf (curr_section->entries[i].value, -sizeof (curr_section->entries[i].value), -"%s" , entryvalue); +strlcpy (curr_section->entries[i].value, entryvalue, +sizeof (curr_section->entries[i].value));return 0 ; }printf ("Error - entry name doesn't exist\n" );
@@ -468,8 +467,7 @@ rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],int i;for (i =0 ; i < cfg->num_sections && i < max_sections; i++) -snprintf (sections[i],CFG_NAME_LEN ,"%s" , - cfg->sections[i].name); +strlcpy (sections[i], cfg->sections[i].name,CFG_NAME_LEN );return i; }
@@ -499,7 +497,7 @@ rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,const struct rte_cfgfile_section *sect = &(cfg->sections[index]); -snprintf (sectionname,CFG_NAME_LEN ,"%s" , sect->name); +strlcpy (sectionname, sect->name,CFG_NAME_LEN );return sect->num_entries; }int
@@ -526,7 +524,7 @@ rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,if (index <0 || index >= cfg->num_sections)return -1 ; sect = &cfg->sections[index]; -snprintf (sectionname,CFG_NAME_LEN ,"%s" , sect->name); +strlcpy (sectionname, sect->name,CFG_NAME_LEN );for (i =0 ; i < max_entries && i < sect->num_entries; i++) entries[i] = sect->entries[i];return i;
lib/librte_compressdev/rte_compressdev.c +3 -2
@@ -7,6 +7,7 @@#include <stdio.h> #include <inttypes.h> +#include <rte_string_fns.h> #include <rte_malloc.h> #include <rte_eal.h> #include <rte_memzone.h>
@@ -252,8 +253,8 @@ rte_compressdev_pmd_allocate(const char *name, int socket_id) compressdev->data = compressdev_data; -snprintf (compressdev->data->name,RTE_COMPRESSDEV_NAME_MAX_LEN , -"%s" , name); +strlcpy (compressdev->data->name, name, +RTE_COMPRESSDEV_NAME_MAX_LEN ); compressdev->data->dev_id = dev_id; compressdev->data->socket_id = socket_id;
lib/librte_compressdev/rte_compressdev_pmd.c +2 -1
@@ -2,6 +2,7 @@* Copyright(c) 2017-2018 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_malloc.h> #include <rte_kvargs.h> #include <rte_eal.h>
@@ -21,7 +22,7 @@ rte_compressdev_pmd_parse_name_arg(const char *key __rte_unused,struct rte_compressdev_pmd_init_params *params = extra_args;int n; - n =snprintf (params->name,RTE_COMPRESSDEV_NAME_MAX_LEN ,"%s" , value); + n =strlcpy (params->name, value,RTE_COMPRESSDEV_NAME_MAX_LEN );if (n >=RTE_COMPRESSDEV_NAME_MAX_LEN )return -EINVAL ;
lib/librte_cryptodev/rte_cryptodev_pmd.c +2 -1
@@ -2,6 +2,7 @@* Copyright(c) 2017 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_malloc.h> #include "rte_cryptodev_pmd.h"
@@ -16,7 +17,7 @@ rte_cryptodev_pmd_parse_name_arg(const char *key __rte_unused,struct rte_cryptodev_pmd_init_params *params = extra_args;int n; - n =snprintf (params->name,RTE_CRYPTODEV_NAME_MAX_LEN ,"%s" , value); + n =strlcpy (params->name, value,RTE_CRYPTODEV_NAME_MAX_LEN );if (n >=RTE_CRYPTODEV_NAME_MAX_LEN )return -EINVAL ;
lib/librte_eal/common/eal_common_options.c +2 -1
@@ -16,6 +16,7 @@#include <sys/stat.h> #include <dirent.h> +#include <rte_string_fns.h> #include <rte_eal.h> #include <rte_log.h> #include <rte_lcore.h>
@@ -139,7 +140,7 @@ eal_option_device_add(enum rte_devtype type, const char *optarg) } devopt->type = type; - ret =snprintf (devopt->arg, optlen,"%s" , optarg); + ret =strlcpy (devopt->arg, optarg, optlen);if (ret <0 ) {RTE_LOG (ERR ,EAL ,"Unable to copy device option\n" );free (devopt);
lib/librte_efd/rte_efd.c +2 -1
@@ -9,6 +9,7 @@#include <stdarg.h> #include <sys/queue.h> +#include <rte_string_fns.h> #include <rte_log.h> #include <rte_eal_memconfig.h> #include <rte_errno.h>
@@ -591,7 +592,7 @@ rte_efd_create(const char *name, uint32_t max_num_rules, uint32_t key_len, goto error_unlock_exit; } table->keys = key_array; -snprintf (table->name,sizeof (table->name),"%s" , name); +strlcpy (table->name, name,sizeof (table->name)); RTE_LOG(DEBUG, EFD, "Creating an EFD table with %u chunks," " which potentially supports %u entries\n",
lib/librte_eventdev/rte_eventdev.c +2 -2
@@ -13,6 +13,7 @@#include <sys/types.h> #include <sys/queue.h> +#include <rte_string_fns.h> #include <rte_byteorder.h> #include <rte_log.h> #include <rte_debug.h>
@@ -1362,8 +1363,7 @@ rte_event_pmd_allocate(const char *name, int socket_id) eventdev->data = eventdev_data; -snprintf (eventdev->data->name,RTE_EVENTDEV_NAME_MAX_LEN , -"%s" , name); +strlcpy (eventdev->data->name, name,RTE_EVENTDEV_NAME_MAX_LEN ); eventdev->data->dev_id = dev_id; eventdev->data->socket_id = socket_id;
lib/librte_flow_classify/rte_flow_classify.c +2 -2
@@ -2,6 +2,7 @@* Copyright(c) 2017 Intel Corporation */ +#include <rte_string_fns.h> #include <rte_compat.h> #include <rte_flow_classify.h> #include "rte_flow_classify_parse.h"
@@ -285,8 +286,7 @@ rte_flow_classifier_create(struct rte_flow_classifier_params *params) }/* Save input parameters */ -snprintf (cls->name,RTE_FLOW_CLASSIFIER_MAX_NAME_SZ ,"%s" , - params->name); +strlcpy (cls->name, params->name,RTE_FLOW_CLASSIFIER_MAX_NAME_SZ ); cls->socket_id = params->socket_id;
lib/librte_jobstats/rte_jobstats.c +2 -1
@@ -6,6 +6,7 @@#include <stdlib.h> #include <errno.h> +#include <rte_string_fns.h> #include <rte_errno.h> #include <rte_common.h> #include <rte_eal.h>
@@ -240,7 +241,7 @@ rte_jobstats_init(struct rte_jobstats *job, const char *name, job->target = target; job->update_period_cb = &default_update_function;rte_jobstats_reset (job); -snprintf (job->name,RTE_DIM (job->name),"%s" , name ==NULL ?"" : name); +strlcpy (job->name, name ==NULL ?"" : name,RTE_DIM (job->name)); job->context =NULL ;return 0 ;
lib/librte_latencystats/rte_latencystats.c +3 -2
@@ -7,6 +7,7 @@#include <stdbool.h> #include <math.h> +#include <rte_string_fns.h> #include <rte_mbuf.h> #include <rte_log.h> #include <rte_cycles.h>
@@ -309,8 +310,8 @@ rte_latencystats_get_names(struct rte_metric_name *names, uint16_t size)return NUM_LATENCY_STATS ;for (i =0 ; i <NUM_LATENCY_STATS ; i++) -snprintf (names[i].name,sizeof (names[i].name), -"%s" , lat_stats_strings[i].name); +strlcpy (names[i].name, lat_stats_strings[i].name, +sizeof (names[i].name));return NUM_LATENCY_STATS ; }
lib/librte_member/rte_member.c +2 -1
@@ -4,6 +4,7 @@#include <string.h> +#include <rte_string_fns.h> #include <rte_eal.h> #include <rte_eal_memconfig.h> #include <rte_memory.h>
@@ -132,7 +133,7 @@ rte_member_create(const struct rte_member_parameters *params)RTE_MEMBER_LOG (ERR ,"Create setsummary failed\n" ); goto error_unlock_exit; } -snprintf (setsum->name,sizeof (setsum->name),"%s" , params->name); +strlcpy (setsum->name, params->name,sizeof (setsum->name)); setsum->type = params->type; setsum->socket_id = params->socket_id; setsum->key_len = params->key_len;
lib/librte_mempool/rte_mempool_ops.c +2 -1
@@ -6,6 +6,7 @@#include <stdio.h> #include <string.h> +#include <rte_string_fns.h> #include <rte_mempool.h> #include <rte_errno.h> #include <rte_dev.h>
@@ -51,7 +52,7 @@ rte_mempool_register_ops(const struct rte_mempool_ops *h) ops_index = rte_mempool_ops_table.num_ops++; ops = &rte_mempool_ops_table.ops[ops_index]; -snprintf (ops->name,sizeof (ops->name),"%s" , h->name); +strlcpy (ops->name, h->name,sizeof (ops->name)); ops->alloc = h->alloc; ops->free = h->free; ops->enqueue = h->enqueue;
lib/librte_power/power_acpi_cpufreq.c +2 -1
@@ -12,6 +12,7 @@#include <signal.h> #include <limits.h> +#include <rte_string_fns.h> #include <rte_memcpy.h> #include <rte_atomic.h>
@@ -159,7 +160,7 @@ power_set_governor_userspace(struct rte_power_info *pi) goto out; }/* Save the original governor */ -snprintf (pi->governor_ori,sizeof (pi->governor_ori),"%s" , buf); +strlcpy (pi->governor_ori, buf,sizeof (pi->governor_ori));/* Write 'userspace' to the governor */ val =fseek (f,0 ,SEEK_SET );
lib/librte_power/power_pstate_cpufreq.c +2 -1
@@ -14,6 +14,7 @@#include <errno.h> #include <inttypes.h> +#include <rte_string_fns.h> #include <rte_memcpy.h> #include <rte_atomic.h>
@@ -349,7 +350,7 @@ power_set_governor_performance(struct pstate_power_info *pi) goto out; }/* Save the original governor */ -snprintf (pi->governor_ori,sizeof (pi->governor_ori),"%s" , buf); +strlcpy (pi->governor_ori, buf,sizeof (pi->governor_ori));/* Write 'performance' to the governor */ val =fseek (f,0 ,SEEK_SET );
lib/librte_rawdev/rte_rawdev.c +2 -1
@@ -13,6 +13,7 @@#include <sys/types.h> #include <sys/queue.h> +#include <rte_string_fns.h> #include <rte_byteorder.h> #include <rte_log.h> #include <rte_debug.h>
@@ -508,7 +509,7 @@ rte_rawdev_pmd_allocate(const char *name, size_t dev_priv_size, int socket_id) rawdev->dev_id = dev_id; rawdev->socket_id = socket_id; rawdev->started =0 ; -snprintf (rawdev->name,RTE_RAWDEV_NAME_MAX_LEN ,"%s" , name); +strlcpy (rawdev->name, name,RTE_RAWDEV_NAME_MAX_LEN ); rawdev->attached =RTE_RAWDEV_ATTACHED ; rawdev_globals.nb_devs++;
lib/librte_reorder/rte_reorder.c +3 -2
@@ -5,6 +5,7 @@#include <inttypes.h> #include <string.h> +#include <rte_string_fns.h> #include <rte_log.h> #include <rte_mbuf.h> #include <rte_eal_memconfig.h>
@@ -82,7 +83,7 @@ rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize, }memset (b,0 , bufsize); -snprintf (b->name,sizeof (b->name),"%s" , name); +strlcpy (b->name, name,sizeof (b->name)); b->memsize = bufsize; b->order_buf.size = b->ready_buf.size = size; b->order_buf.mask = b->ready_buf.mask = size -1 ;
@@ -161,7 +162,7 @@ rte_reorder_reset(struct rte_reorder_buffer *b)char name[RTE_REORDER_NAMESIZE ];rte_reorder_free_mbufs (b); -snprintf (name,sizeof (name),"%s" , b->name); +strlcpy (name, b->name,sizeof (name));/* No error checking as current values should be valid */ rte_reorder_init (b, b->memsize, name, b->order_buf.size); }
Signature · 1 line
2.20.1
Quoted 18 lines · Bruce Richardson
There are quite a few instances remaining in DPDK where snprintf is being used for string copying. These were not being picked up by our existing coccinelle script, but that can be fixed by editing the script and running it against our code. In the process a bug was found and fixed in the bonding pmd, where we were incorrectly specifiying the buffer length parameter to snprintf.
The actual replacement was done in two phases - first replacing all instances where only the snprintf line in question needed changing, then fixing the other instances where we also needed to add in the header include. [Using two stages allowed the header addition to be automated too, since we had a list of files where every one needed the header inclusion]
Quoted 7 lines
Bruce Richardson (5):
net/bonding: fix buffer length when printing strings devtools/cocci: make strlcpy replacement smarter devtools/cocci: create safer version of strlcpy script replace snprintf with strlcpy without adding extra include replace snprintf with strlcpy
Should we not be testing the return values from strlcpy and snprintf, which means we need to create a macro or inline function. We could use a macro and only enable with DEBUG support if we think performance or code size if a problem.
I am surprised none of the tools are catching these types of problems.
Not to make Bruce do that change for this patch, but we need to look at it for a later patch IMO.
Regards,
Keith
Quoted 26 lines · Keith
Quoted 18 lines · Bruce Richardson
There are quite a few instances remaining in DPDK where snprintf is being used for string copying. These were not being picked up by our existing coccinelle script, but that can be fixed by editing the script and running it against our code. In the process a bug was found and fixed in the bonding pmd, where we were incorrectly specifiying the buffer length parameter to snprintf.
The actual replacement was done in two phases - first replacing all instances where only the snprintf line in question needed changing, then fixing the other instances where we also needed to add in the header include. [Using two stages allowed the header addition to be automated too, since we had a list of files where every one needed the header inclusion]
Quoted 7 lines
Bruce Richardson (5):
net/bonding: fix buffer length when printing strings devtools/cocci: make strlcpy replacement smarter devtools/cocci: create safer version of strlcpy script replace snprintf with strlcpy without adding extra include replace snprintf with strlcpy
Quoted 2 lines
Should we not be testing the return values from strlcpy and snprintf, which means we need to create a macro or inline function. We could use a macro and only enable with DEBUG support if we think performance or code size if a problem.
Yes, I think in some/many cases we should be checking the return value, but unfortunately I don't think it's the case that we always should or should not do so. In some cases, truncation is fine. Therefore, I don't think an automated solution can work here, and I don't see much point in wrapping any of these functions in macros, since the action to take on truncation probably varies from place to place.
Quoted 2 lines
I am surprised none of the tools are catching these types of problems.
Yes. It would be useful to have a tool to flag this, so we can see under what circumstances it may be a problem.
Quoted 2 lines
Not to make Bruce do that change for this patch, but we need to look at it for a later patch IMO.
Phew! :-)
Quoted 24 lines · Keith
Quoted 16 lines · Bruce Richardson
There are quite a few instances remaining in DPDK where snprintf is being used for string copying. These were not being picked up by our existing coccinelle script, but that can be fixed by editing the script and running it against our code. In the process a bug was found and fixed in the bonding pmd, where we were incorrectly specifiying the buffer length parameter to snprintf.
The actual replacement was done in two phases - first replacing all instances where only the snprintf line in question needed changing, then fixing the other instances where we also needed to add in the header include. [Using two stages allowed the header addition to be automated too, since we had a list of files where every one needed the header inclusion]
Quoted 7 lines
Bruce Richardson (5):
net/bonding: fix buffer length when printing strings devtools/cocci: make strlcpy replacement smarter devtools/cocci: create safer version of strlcpy script replace snprintf with strlcpy without adding extra include replace snprintf with strlcpy
Quoted 9 lines
Should we not be testing the return values from strlcpy and snprintf, which means we need to create a macro or inline function. We could use a macro and only enable with DEBUG support if we think performance or code size if a problem.
I am surprised none of the tools are catching these types of problems.
Not to make Bruce do that change for this patch, but we need to look at it for a later patch IMO.
Regards,
Keith
In a lot of cases the error checking should be earlier in the input path. Detecting overflow late in making a string is unhelpful. But rejecting input where the name is too long to start with is safer and better.
It is one of those cases where static checkers tell you there is a problem but the right solution is farther back in the code, not just right where the error is reported.
03/04/2019 16:45, Bruce Richardson:
Quoted 14 lines
There are quite a few instances remaining in DPDK where snprintf is being used for string copying. These were not being picked up by our existing coccinelle script, but that can be fixed by editing the script and running it against our code. In the process a bug was found and fixed in the bonding pmd, where we were incorrectly specifiying the buffer length parameter to snprintf.
The actual replacement was done in two phases - first replacing all instances where only the snprintf line in question needed changing, then fixing the other instances where we also needed to add in the header include. [Using two stages allowed the header addition to be automated too, since we had a list of files where every one needed the header inclusion]
Quoted 6 lines
Bruce Richardson (5):
net/bonding: fix buffer length when printing strings devtools/cocci: make strlcpy replacement smarter devtools/cocci: create safer version of strlcpy script replace snprintf with strlcpy without adding extra include replace snprintf with strlcpy
Applied (with 2 rebase changes), thanks