Skip to contentSkip to conversation

app/testpmd: fix devargs format in port attach

PATCH Superseded Gregory Etelson 3 participants 6 messages Oct 30, 2025 – Nov 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 app/testpmd: fix devargs format in port attach

The port attach procedure discarded PCI port devargs provided by application.
The patch restores PCI port devargs.

Fixes 12c2405989f6 ("app/testpmd: canonicalize short PCI name format")
---
 app/test-pmd/testpmd.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
app/test-pmd/testpmd.c +10 -5
index 2360da3a48..cc384f0b14 100644
@@ -3413,7 +3413,7 @@ reset_port(portid_t pid)
 }
 
 static char *
-convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_size)
+convert_pci_address_format(const char *identifier, char *pci_buffer)
 {
 	struct rte_devargs da;
 	struct rte_pci_addr pci_addr;
@@ -3430,7 +3430,8 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 	if (rte_pci_addr_parse(da.name, &pci_addr) != 0)
 		return NULL;
 
-	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
+	rte_pci_device_name(&pci_addr, pci_buffer, PCI_PRI_STR_SIZE);
+	sprintf(pci_buffer + strlen(pci_buffer), ",%s", da.args);
 	return pci_buffer;
 }
 
@@ -3439,8 +3440,7 @@ attach_port(char *identifier)
 {
 	portid_t pi;
 	struct rte_dev_iterator iterator;
-	char *long_identifier;
-	char long_format[PCI_PRI_STR_SIZE];
+	char *long_format, *long_identifier;
 
 	printf("Attaching a new port...\n");
 
@@ -3448,9 +3448,14 @@ attach_port(char *identifier)
 		fprintf(stderr, "Invalid parameters are specified\n");
 		return;
 	}
+	long_format = alloca(strlen(identifier) + PCI_PRI_STR_SIZE);
+	if (long_format == NULL) {
+		TESTPMD_LOG(ERR, "Failed to attach port %s - allocation failure\n", identifier);
+		return;
+	}
 
 	/* For PCI device convert to canonical format */
-	long_identifier = convert_pci_address_format(identifier, long_format, sizeof(long_format));
+	long_identifier = convert_pci_address_format(identifier, long_format);
 	if (long_identifier != NULL)
 		identifier = long_identifier;
 
Signature · 1 line
2.51.0
#1 lore raw
Quoted 7 lines · On Thu, 30 Oct 2025 11:20:15 +0200 Gregory Etelson <[email protected]> wrote:

static char *

app/test-pmd/testpmd.c +1 -1
-convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_size)
+convert_pci_address_format(const char *identifier, char *pci_buffer)
 {
 	struct rte_devargs da;
 	struct rte_pci_addr pci_addr;

app/test-pmd/testpmd.c convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_ · line 3436

Quoted 8 lines
app/test-pmd/testpmd.c +2 -1
@@ -3430,7 +3430,8 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 	if (rte_pci_addr_parse(da.name, &pci_addr) != 0)
 		return NULL;
 
-	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
+	rte_pci_device_name(&pci_addr, pci_buffer, PCI_PRI_STR_SIZE);
+	sprintf(pci_buffer + strlen(pci_buffer), ",%s", da.args);
 	return pci_buffer;
 }

It would be safer to keep the buf_size argument. It avoids any overflow issues. Many tools flag any direct sprintf usage as an error.

#2 lore raw

PATCH v2 1/1 app/testpmd: fix devargs format in port attach

In reply to Gregory Etelson

The port attach procedure discarded PCI port devargs provided by application.
The patch restores PCI port devargs.

Fixes 12c2405989f6 ("app/testpmd: canonicalize short PCI name format")

---
v2: add buf_size parameter to convert_pci_address_format()

---
 app/test-pmd/testpmd.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
app/test-pmd/testpmd.c +10 -3
index 2360da3a48..a2280d38d3 100644
@@ -3431,6 +3431,7 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 		return NULL;
 
 	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
+	sprintf(pci_buffer + strlen(pci_buffer), ",%s", da.args);
 	return pci_buffer;
 }
 
@@ -3439,8 +3440,8 @@ attach_port(char *identifier)
 {
 	portid_t pi;
 	struct rte_dev_iterator iterator;
-	char *long_identifier;
-	char long_format[PCI_PRI_STR_SIZE];
+	char *long_format, *long_identifier;
+	size_t long_format_size;
 
 	printf("Attaching a new port...\n");
 
@@ -3448,9 +3449,15 @@ attach_port(char *identifier)
 		fprintf(stderr, "Invalid parameters are specified\n");
 		return;
 	}
+	long_format_size = strlen(identifier) + PCI_PRI_STR_SIZE;
+	long_format = alloca(long_format_size);
+	if (long_format == NULL) {
+		TESTPMD_LOG(ERR, "Failed to attach port %s - allocation failure\n", identifier);
+		return;
+	}
 
 	/* For PCI device convert to canonical format */
-	long_identifier = convert_pci_address_format(identifier, long_format, sizeof(long_format));
+	long_identifier = convert_pci_address_format(identifier, long_format, long_format_size);
 	if (long_identifier != NULL)
 		identifier = long_identifier;
 
Signature · 1 line
2.51.0
#3 lore raw

app/test-pmd/testpmd.c convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_ · line 3437

Quoted 12 lines · On Thu, 30 Oct 2025 19:17:34 +0200 Gregory Etelson <[email protected]> wrote:
app/test-pmd/testpmd.c +1 -0
index 2360da3a48..a2280d38d3 100644
@@ -3431,6 +3431,7 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 		return NULL;
 
 	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
+	sprintf(pci_buffer + strlen(pci_buffer), ",%s", da.args);
 	return pci_buffer;
 }
 

To avoid any possiblity of overflow, please us snprintf and adjust. Something like:

	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
	size_t pci_len = strlen(pci_buffer);
	snprintf(pci_buffer + pci_len, buf_size - pci_len, "%,%s", da.args);
#4 lore raw

PATCH v3 1/1 app/testpmd: fix devargs format in port attach

In reply to Gregory Etelson

The port attach procedure discarded PCI port devargs provided by application.
The patch restores PCI port devargs.

Fixes 12c2405989f6 ("app/testpmd: canonicalize short PCI name format")

---
v2: add buf_size parameter to convert_pci_address_format()
v3: use snprintf()

---
 app/test-pmd/testpmd.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
app/test-pmd/testpmd.c +12 -3
index 2360da3a48..b10f6baee2 100644
@@ -3417,6 +3417,7 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 {
 	struct rte_devargs da;
 	struct rte_pci_addr pci_addr;
+	size_t pci_len;
 
 	if (rte_devargs_parse(&da, identifier) != 0)
 		return NULL;
@@ -3431,6 +3432,8 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 		return NULL;
 
 	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
+	pci_len = strlen(pci_buffer);
+	snprintf(pci_buffer + pci_len, buf_size - pci_len, ",%s", da.args);
 	return pci_buffer;
 }
 
@@ -3439,8 +3442,8 @@ attach_port(char *identifier)
 {
 	portid_t pi;
 	struct rte_dev_iterator iterator;
-	char *long_identifier;
-	char long_format[PCI_PRI_STR_SIZE];
+	char *long_format, *long_identifier;
+	size_t long_format_size;
 
 	printf("Attaching a new port...\n");
 
@@ -3448,9 +3451,15 @@ attach_port(char *identifier)
 		fprintf(stderr, "Invalid parameters are specified\n");
 		return;
 	}
+	long_format_size = strlen(identifier) + PCI_PRI_STR_SIZE;
+	long_format = alloca(long_format_size);
+	if (long_format == NULL) {
+		TESTPMD_LOG(ERR, "Failed to attach port %s - allocation failure\n", identifier);
+		return;
+	}
 
 	/* For PCI device convert to canonical format */
-	long_identifier = convert_pci_address_format(identifier, long_format, sizeof(long_format));
+	long_identifier = convert_pci_address_format(identifier, long_format, long_format_size);
 	if (long_identifier != NULL)
 		identifier = long_identifier;
 
Signature · 1 line
2.51.0
#5 lore raw
Quoted 4 lines · Gregory Etelson

The port attach procedure discarded PCI port devargs provided by application.
The patch restores PCI port devargs.

There is a bugzilla for this issue, so it can be added to the commit

Bugzilla ID: 1815

Quoted 60 lines
Fixes 12c2405989f6 ("app/testpmd: canonicalize short PCI name format")

---
v2: add buf_size parameter to convert_pci_address_format()
v3: use snprintf()

---
 app/test-pmd/testpmd.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
app/test-pmd/testpmd.c +12 -3
index 2360da3a48..b10f6baee2 100644
@@ -3417,6 +3417,7 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 {
 	struct rte_devargs da;
 	struct rte_pci_addr pci_addr;
+	size_t pci_len;
 
 	if (rte_devargs_parse(&da, identifier) != 0)
 		return NULL;
@@ -3431,6 +3432,8 @@ convert_pci_address_format(const char *identifier, char *pci_buffer, size_t buf_
 		return NULL;
 
 	rte_pci_device_name(&pci_addr, pci_buffer, buf_size);
+	pci_len = strlen(pci_buffer);
+	snprintf(pci_buffer + pci_len, buf_size - pci_len, ",%s", da.args);
 	return pci_buffer;
 }
 
@@ -3439,8 +3442,8 @@ attach_port(char *identifier)
 {
 	portid_t pi;
 	struct rte_dev_iterator iterator;
-	char *long_identifier;
-	char long_format[PCI_PRI_STR_SIZE];
+	char *long_format, *long_identifier;
+	size_t long_format_size;
 
 	printf("Attaching a new port...\n");
 
@@ -3448,9 +3451,15 @@ attach_port(char *identifier)
 		fprintf(stderr, "Invalid parameters are specified\n");
 		return;
 	}
+	long_format_size = strlen(identifier) + PCI_PRI_STR_SIZE;
+	long_format = alloca(long_format_size);
+	if (long_format == NULL) {
+		TESTPMD_LOG(ERR, "Failed to attach port %s - allocation failure\n", identifier);
+		return;
+	}
 
 	/* For PCI device convert to canonical format */
-	long_identifier = convert_pci_address_format(identifier, long_format, sizeof(long_format));
+	long_identifier = convert_pci_address_format(identifier, long_format, long_format_size);
 	if (long_identifier != NULL)
 		identifier = long_identifier;