[dpdk-dev] eal: fix possible segfault in hotplug add

Message ID 31be1706d1613adecd43328272f1b6f556c6110f.1501763608.git.gaetan.rivet@6wind.com (mailing list archive)
State Accepted, archived
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Gaëtan Rivet Aug. 3, 2017, 12:34 p.m. UTC
  If devargs is NULL, building the full_dev_name will segfault
when using strlen on it.

Coverity issue: 158630
Fixes: 7e8b26650146 ("eal: fix hotplug add / remove")

Signed-off-by: Gaetan Rivet <gaetan.rivet@6wind.com>
---
 lib/librte_eal/common/eal_common_dev.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
  

Comments

Thomas Monjalon Aug. 3, 2017, 7:38 p.m. UTC | #1
03/08/2017 14:34, Gaetan Rivet:
> If devargs is NULL, building the full_dev_name will segfault
> when using strlen on it.
> 
> Coverity issue: 158630
> Fixes: 7e8b26650146 ("eal: fix hotplug add / remove")
> 
> Signed-off-by: Gaetan Rivet <gaetan.rivet@6wind.com>

Applied, thanks
  

Patch

diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
index f98302d..d19232d 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -133,16 +133,13 @@  full_dev_name(const char *bus, const char *dev, const char *args)
 	char *name;
 	size_t len;
 
-	len = strlen(bus) + 1 +
-	      strlen(dev) + 1 +
-	      strlen(args) + 1;
+	len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args);
 	name = calloc(1, len);
 	if (name == NULL) {
 		RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
 		return NULL;
 	}
-	snprintf(name, len, "%s:%s,%s", bus, dev,
-		 args ? args : "");
+	snprintf(name, len, "%s:%s,%s", bus, dev, args);
 	return name;
 }