[v1,2/2] pci: explain how empty strings are rejected in DBDF

Message ID 20200513104751.46466-3-grive@u256.net (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series pci: a comment and a minor fix |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/travis-robot warning Travis build: failed
ci/Intel-compilation success Compilation OK

Commit Message

Gaëtan Rivet May 13, 2020, 10:47 a.m. UTC
  Empty strings are forbidden as input to rte_pci_addr_parse().
It is explicitly enforced in BDF parsing as parsing the bus
field will immediately fail. The related check is commented.

It is implicitly enforced in DBDF parsing, as the domain would be
parsed to 0 without error, but the check `end[0] != ':'` afterward
will return -EINVAL.

Enforcing consistency between parsers by reading the code is not helped
by this property being implicit. Add a comment to explain.

Signed-off-by: Gaetan Rivet <grive@u256.net>
---
 lib/librte_pci/rte_pci.c | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

Stojaczyk, Dariusz May 14, 2020, 8:53 a.m. UTC | #1
> -----Original Message-----
> From: dev <dev-bounces@dpdk.org> On Behalf Of Gaetan Rivet
> Sent: Wednesday, May 13, 2020 12:48 PM
> To: dev@dpdk.org
> Subject: [dpdk-dev] [PATCH v1 2/2] pci: explain how empty strings are rejected
> in DBDF
> 
> Empty strings are forbidden as input to rte_pci_addr_parse().
> It is explicitly enforced in BDF parsing as parsing the bus
> field will immediately fail. The related check is commented.
> 
> It is implicitly enforced in DBDF parsing, as the domain would be
> parsed to 0 without error, but the check `end[0] != ':'` afterward
> will return -EINVAL.
> 
> Enforcing consistency between parsers by reading the code is not helped
> by this property being implicit. Add a comment to explain.
> 
> Signed-off-by: Gaetan Rivet <grive@u256.net>
> ---

Acked-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
  
David Marchand May 19, 2020, 9:18 a.m. UTC | #2
On Wed, May 13, 2020 at 12:48 PM Gaetan Rivet <grive@u256.net> wrote:
>
> Empty strings are forbidden as input to rte_pci_addr_parse().
> It is explicitly enforced in BDF parsing as parsing the bus
> field will immediately fail. The related check is commented.
>
> It is implicitly enforced in DBDF parsing, as the domain would be
> parsed to 0 without error, but the check `end[0] != ':'` afterward
> will return -EINVAL.
>
> Enforcing consistency between parsers by reading the code is not helped
> by this property being implicit. Add a comment to explain.
>
> Signed-off-by: Gaetan Rivet <grive@u256.net>
Acked-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>


Applied, thanks.
  

Patch

diff --git a/lib/librte_pci/rte_pci.c b/lib/librte_pci/rte_pci.c
index e4ecdc32f..60e6fbae7 100644
--- a/lib/librte_pci/rte_pci.c
+++ b/lib/librte_pci/rte_pci.c
@@ -84,6 +84,10 @@  pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
 
 	errno = 0;
 	val = strtoul(in, &end, 16);
+	/* Empty string is not an error for strtoul, but the check
+	 *   end[0] != ':'
+	 * will detect the issue.
+	 */
 	if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
 		return -EINVAL;
 	dev_addr->domain = (uint16_t)val;