Skip to contentSkip to conversation

pci: a comment and a minor fix

PATCH Applied Gaetan Rivet 3 participants 7 messages May 13, 2020 – May 19, 2020

lore
Share & tools
lore
#0 lore raw

cover pci: a comment and a minor fix

Add a clarifying comment and fix the PCI addr parser behavior for some edge-case.

Gaetan Rivet (2):
pci: fix allowing underflow when parsing PCI id
pci: explain how empty strings are rejected in DBDF

 lib/librte_pci/rte_pci.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 pci: fix allowing underflow when parsing PCI id

The function strtoul will not return ERANGE if the input is negative, as one might expect.

0000:-FFFFFFFFFFFFFFFB:00.0

is not a better way to write 0000:05:00.0.
To simplify checking for '-', forbid using spaces before the field value.

0000: 00: 2c.0

Should not be accepted.

Fixes af75078fece3 ("first public release")
---
 lib/librte_pci/rte_pci.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/librte_pci/rte_pci.c +12 -0
index d1ab6b414..e4ecdc32f 100644
@@ -35,6 +35,12 @@ get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
 	if (*in == '\0')
 		return NULL;
 
+	/* PCI field starting with spaces is forbidden.
+	 * Negative wrap-around is not reported as an error by strtoul.
+	 */
+	if (*in == ' ' || *in == '-')
+		return NULL;
+
 	errno = 0;
 	val = strtoul(in, &end, 16);
 	if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
@@ -70,6 +76,12 @@ pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
 	unsigned long val;
 	char *end;
 
+	/* PCI id starting with spaces is forbidden.
+	 * Negative wrap-around is not reported as an error by strtoul.
+	 */
+	if (*in == ' ' || *in == '-')
+		return EINVAL;
+
 	errno = 0;
 	val = strtoul(in, &end, 16);
 	if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
Signature · 1 line
2.26.2
Acked-by on v1 1/2
#2 lore raw

Re: [dpdk-dev] [PATCH v1 1/2] pci: fix allowing underflow when parsing PCI id

Quoted 16 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Gaetan Rivet
Sent: Wednesday, May 13, 2020 12:48 PM
To: [email protected]
Cc: [email protected]
Subject: [dpdk-dev] [PATCH v1 1/2] pci: fix allowing underflow when parsing PCI id

The function strtoul will not return ERANGE if the input is negative, as one might expect.

   0000:-FFFFFFFFFFFFFFFB:00.0

is not a better way to write 0000:05:00.0.
To simplify checking for '-', forbid using spaces before the field value.

Quoted 8 lines
   0000: 00:   2c.0

Should not be accepted.

Fixes af75078fece3 ("first public release")

---

Acked-by Darek Stojaczyk on v1 1/2 Applied
#3 lore raw

Re: [dpdk-dev] [PATCH v1 1/2] pci: fix allowing underflow when parsing PCI id

In reply to Gaetan Rivet
Quoted 9 lines · Gaetan Rivet

The function strtoul will not return ERANGE if the input is negative, as one might expect.

   0000:-FFFFFFFFFFFFFFFB:00.0

is not a better way to write 0000:05:00.0.
To simplify checking for '-', forbid using spaces before the field value.

Quoted 6 lines
   0000: 00:   2c.0

Should not be accepted.

Fixes af75078fece3 ("first public release")

Not sure about backporting this one, will let stable maintainers reconsider this.

Quoted 1 line
Quoted 22 lines
---
 lib/librte_pci/rte_pci.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
lib/librte_pci/rte_pci.c +6 -0
index d1ab6b414..e4ecdc32f 100644
@@ -35,6 +35,12 @@ get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
        if (*in == '\0')
                return NULL;
 
+       /* PCI field starting with spaces is forbidden.
+        * Negative wrap-around is not reported as an error by strtoul.
+        */
+       if (*in == ' ' || *in == '-')
+               return NULL;
+
        errno = 0;
        val = strtoul(in, &end, 16);
        if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {

lib/librte_pci/rte_pci.c pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr) · line 83

Quoted 8 lines
lib/librte_pci/rte_pci.c +5 -0
@@ -70,6 +76,12 @@ pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
        unsigned long val;
        char *end;
 
+       /* PCI id starting with spaces is forbidden.
+        * Negative wrap-around is not reported as an error by strtoul.
+        */
+       if (*in == ' ' || *in == '-')
+               return EINVAL;

Should be -EINVAL, fixed.

Quoted 7 lines
lib/librte_pci/rte_pci.c +1 -1
+
        errno = 0;
        val = strtoul(in, &end, 16);
        if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
--

2.26.2

Applied, thanks.

Signature · 1 line
David Marchand
#4 lore raw

PATCH 2/2 pci: explain how empty strings are rejected in DBDF

In reply to Gaetan Rivet

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.

---
 lib/librte_pci/rte_pci.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/librte_pci/rte_pci.c +4 -0
index e4ecdc32f..60e6fbae7 100644
@@ -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;
Signature · 1 line
2.26.2
Acked-by on v1 2/2
#5 lore raw

Re: [dpdk-dev] [PATCH v1 2/2] pci: explain how empty strings are rejected in DBDF

Quoted 15 lines

-----Original Message-----
From: dev <[email protected]> On Behalf Of Gaetan Rivet
Sent: Wednesday, May 13, 2020 12:48 PM
To: [email protected]
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.

Quoted 5 lines

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

---

Acked-by Darek Stojaczyk on v1 2/2 Applied
#6 lore raw

Re: [dpdk-dev] [PATCH v1 2/2] pci: explain how empty strings are rejected in DBDF

In reply to Gaetan Rivet
Quoted 5 lines · Gaetan Rivet

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.

Quoted 8 lines

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.

Applied, thanks.

Signature · 1 line
David Marchand