Skip to contentSkip to conversation

ethdev: use strlcpy

PATCH In review Stephen Hemminger 4 participants 11 messages Feb 28, 2019 – Mar 13, 2019

lore
Share & tools
lore
#0 lore raw

cover ethdev: use strlcpy

A couple of places in ethdev are clearer if strlcpy is used.

Stephen Hemminger (2):
ethdev: replace snprintf with strlcpy
ethdev: use strlcpy instead of snprintf on initialization

 lib/librte_ethdev/rte_ethdev.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 ethdev: replace snprintf with strlcpy

The set_port_owner was copying a string between structures of the same type, therefore the name could never be truncated (unless source string was not null terminated). Use strlcpy which does it better.

---
 lib/librte_ethdev/rte_ethdev.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
lib/librte_ethdev/rte_ethdev.c +2 -6
index 85c1794968dd..95889ed206db 100644
@@ -585,7 +585,6 @@ _rte_eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
 {
 	struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
 	struct rte_eth_dev_owner *port_owner;
-	int sret;
 
 	if (port_id >= RTE_MAX_ETHPORTS || !is_allocated(ethdev)) {
 		RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
@@ -609,11 +608,8 @@ _rte_eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
 		return -EPERM;
 	}
 
-	sret = snprintf(port_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN, "%s",
-			new_owner->name);
-	if (sret < 0 || sret >= RTE_ETH_MAX_OWNER_NAME_LEN)
-		RTE_ETHDEV_LOG(ERR, "Port %u owner name was truncated\n",
-			port_id);
+	/* can not truncate (same structure) */
+	strlcpy(port_owner->name, new_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN);
 
 	port_owner->id = new_owner->id;
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH 2/2 ethdev: use strlcpy instead of snprintf on initialization

In reply to Stephen Hemminger

Don't need to use snprintf for simple name copy.

---
 lib/librte_ethdev/rte_ethdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_ethdev/rte_ethdev.c +1 -1
index 95889ed206db..8bd54dcf58c1 100644
@@ -459,7 +459,7 @@ rte_eth_dev_allocate(const char *name)
 	}
 
 	eth_dev = eth_dev_get(port_id);
-	snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
+	strlcpy(eth_dev->data->name, name, RTE_ETH_NAME_MAX_LEN);
 	eth_dev->data->port_id = port_id;
 	eth_dev->data->mtu = ETHER_MTU;
 
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH 2/2] ethdev: use strlcpy instead of snprintf on initialization

In reply to Stephen Hemminger
Quoted 12 lines · Stephen Hemminger

Don't need to use snprintf for simple name copy.

---
  lib/librte_ethdev/rte_ethdev.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_ethdev/rte_ethdev.c +0 -0
index 95889ed206db..8bd54dcf58c1 100644

lib/librte_ethdev/rte_ethdev.c rte_eth_dev_allocate(const char *name) · line 462

Quoted 5 lines
lib/librte_ethdev/rte_ethdev.c +1 -1
@@ -459,7 +459,7 @@ rte_eth_dev_allocate(const char *name)
  	}
 
  	eth_dev = eth_dev_get(port_id);
-	snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
+	strlcpy(eth_dev->data->name, name, RTE_ETH_NAME_MAX_LEN);

Why is sizeof() substituted with RTE_ETH_NAME_MAX_LEN?
I thought that sizeof() is the first choice in such cases since it is a bit more safer vs possible changes in the code.

BTW, wouldn't it be more friendly to check name length on entry and reject if it is too long? (and same for rte_eth_dev_create()) I agree that strlcpy() should be used anyway.

Quoted 3 lines
lib/librte_ethdev/rte_ethdev.c +0 -0
  	eth_dev->data->port_id = port_id;
  	eth_dev->data->mtu = ETHER_MTU;
 
#6 lore raw

Re: [PATCH 2/2] ethdev: use strlcpy instead of snprintf on initialization

Quoted 15 lines · On Fri, 1 Mar 2019 10:48:58 +0300 Andrew Rybchenko <[email protected]>…
Quoted 14 lines · Stephen Hemminger

Don't need to use snprintf for simple name copy.

---
  lib/librte_ethdev/rte_ethdev.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_ethdev/rte_ethdev.c +0 -0
index 95889ed206db..8bd54dcf58c1 100644
@@ -459,7 +459,7 @@ rte_eth_dev_allocate(const char *name)
  	}
 
Quoted 5 lines

lib/librte_ethdev/rte_ethdev.c rte_eth_dev_allocate(const char *name) · line 462

Quoted 3 lines
lib/librte_ethdev/rte_ethdev.c +1 -1
  	eth_dev = eth_dev_get(port_id);
-	snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
+	strlcpy(eth_dev->data->name, name, RTE_ETH_NAME_MAX_LEN);  

Why is sizeof() substituted with RTE_ETH_NAME_MAX_LEN?

Same thing, I just wanted to make the length obvious to the reader.

Quoted 6 lines

I thought that sizeof() is the first choice in such cases since it is a bit more
safer vs possible changes in the code.

BTW, wouldn't it be more friendly to check name length on entry and reject if it is too long? (and same for rte_eth_dev_create())

It is impossible for name to long since since both structures are the same.

Quoted 6 lines

I agree that strlcpy() should be used anyway.

Quoted 3 lines
lib/librte_ethdev/rte_ethdev.c +0 -0
  	eth_dev->data->port_id = port_id;
  	eth_dev->data->mtu = ETHER_MTU;
 
#7 lore raw

Re: [PATCH 2/2] ethdev: use strlcpy instead of snprintf on initialization

Quoted 24 lines · Stephen Hemminger
Quoted 15 lines · On Fri, 1 Mar 2019 10:48:58 +0300 Andrew Rybchenko <[email protected]>…
Quoted 14 lines · Stephen Hemminger

Don't need to use snprintf for simple name copy.

---
   lib/librte_ethdev/rte_ethdev.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_ethdev/rte_ethdev.c +0 -0
index 95889ed206db..8bd54dcf58c1 100644
@@ -459,7 +459,7 @@ rte_eth_dev_allocate(const char *name)
   	}
 
Quoted 4 lines

lib/librte_ethdev/rte_ethdev.c rte_eth_dev_allocate(const char *name) · line 462

Quoted 3 lines
lib/librte_ethdev/rte_ethdev.c +1 -1
   	eth_dev = eth_dev_get(port_id);
-	snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
+	strlcpy(eth_dev->data->name, name, RTE_ETH_NAME_MAX_LEN);

Why is sizeof() substituted with RTE_ETH_NAME_MAX_LEN?

Same thing, I just wanted to make the length obvious to the reader.

Quoted 7 lines
Quoted 6 lines

I thought that sizeof() is the first choice in such cases since it is a bit more
safer vs possible changes in the code.

BTW, wouldn't it be more friendly to check name length on entry and reject if it is too long? (and same for rte_eth_dev_create())

It is impossible for name to long since since both structures are the same.

Which structures? name is an input parameter of the function.

#8 lore raw

Re: [PATCH 2/2] ethdev: use strlcpy instead of snprintf on initialization

Quoted 29 lines · On Mon, 4 Mar 2019 12:11:20 +0300 Andrew Rybchenko <[email protected]>…
Quoted 28 lines · Stephen Hemminger
Quoted 15 lines · On Fri, 1 Mar 2019 10:48:58 +0300 Andrew Rybchenko <[email protected]>…
Quoted 14 lines · Stephen Hemminger

Don't need to use snprintf for simple name copy.

---
   lib/librte_ethdev/rte_ethdev.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_ethdev/rte_ethdev.c +0 -0
index 95889ed206db..8bd54dcf58c1 100644
@@ -459,7 +459,7 @@ rte_eth_dev_allocate(const char *name)
   	}
 
Quoted 4 lines

lib/librte_ethdev/rte_ethdev.c rte_eth_dev_allocate(const char *name) · line 462

Quoted 3 lines
lib/librte_ethdev/rte_ethdev.c +1 -1
   	eth_dev = eth_dev_get(port_id);
-	snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
+	strlcpy(eth_dev->data->name, name, RTE_ETH_NAME_MAX_LEN);  

Why is sizeof() substituted with RTE_ETH_NAME_MAX_LEN?

Same thing, I just wanted to make the length obvious to the reader.

Quoted 4 lines

I thought that sizeof() is the first choice in such cases since it is a bit more
safer vs possible changes in the code.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

BTW, wouldn't it be more friendly to check name length on entry and reject if it is too long? (and same for rte_eth_dev_create())

It is impossible for name to long since since both structures are the same.

Which structures? name is an input parameter of the function.

Sorry my confusion, that was in patch 1.

#9 lore raw

Re: [PATCH 2/2] ethdev: use strlcpy instead of snprintf on initialization

Quoted 32 lines · Stephen Hemminger
Quoted 29 lines · On Mon, 4 Mar 2019 12:11:20 +0300 Andrew Rybchenko <[email protected]>…
Quoted 28 lines · Stephen Hemminger
Quoted 15 lines · On Fri, 1 Mar 2019 10:48:58 +0300 Andrew Rybchenko <[email protected]>…
Quoted 14 lines · Stephen Hemminger

Don't need to use snprintf for simple name copy.

---
   lib/librte_ethdev/rte_ethdev.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
lib/librte_ethdev/rte_ethdev.c +0 -0
index 95889ed206db..8bd54dcf58c1 100644
@@ -459,7 +459,7 @@ rte_eth_dev_allocate(const char *name)
   	}
 
Quoted 4 lines

lib/librte_ethdev/rte_ethdev.c rte_eth_dev_allocate(const char *name) · line 462

Quoted 3 lines
lib/librte_ethdev/rte_ethdev.c +1 -1
   	eth_dev = eth_dev_get(port_id);
-	snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
+	strlcpy(eth_dev->data->name, name, RTE_ETH_NAME_MAX_LEN);  

Why is sizeof() substituted with RTE_ETH_NAME_MAX_LEN?

Same thing, I just wanted to make the length obvious to the reader.

Quoted 4 lines

I thought that sizeof() is the first choice in such cases since it is a bit more
safer vs possible changes in the code.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

BTW, wouldn't it be more friendly to check name length on entry and reject if it is too long? (and same for rte_eth_dev_create())

It is impossible for name to long since since both structures are the same.

Which structures? name is an input parameter of the function.

Sorry my confusion, that was in patch 1.

Only concern is keep using "sizeof(eth_dev->data->name)" or RTE_ETH_NAME_MAX_LEN, both are correct but I agree with Andrew about using sizeof.

Stephen,

Would you be OK if I update it to sizeof() while merging, so I can merge them?

Thanks,
ferruh

#10 lore raw
In reply to Stephen Hemminger
Quoted 5 lines · Stephen Hemminger

A couple of places in ethdev are clearer if strlcpy is used.

Stephen Hemminger (2):
ethdev: replace snprintf with strlcpy
ethdev: use strlcpy instead of snprintf on initialization

Series applied to dpdk-next-net/master, thanks.

(2/2 updated as suggested while merging.)