[dpdk-dev,3/5] malloc: fix potential negative return

Message ID 05d5fd855d95a7427da810166ec715b470d73e8e.1523979264.git.anatoly.burakov@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers

Checks

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

Commit Message

Anatoly Burakov April 17, 2018, 3:48 p.m. UTC
  Return from rte_socket_id_by_idx() may be negative, which would
result in negative array index.

Coverity issue: 272590

Fixes: 1403f87d4fb8 ("malloc: enable memory hotplug support")
Cc: anatoly.burakov@intel.com

Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
---
 lib/librte_eal/common/malloc_heap.c | 4 ++++
 1 file changed, 4 insertions(+)
  

Patch

diff --git a/lib/librte_eal/common/malloc_heap.c b/lib/librte_eal/common/malloc_heap.c
index 5cf7231..f81aaf3 100644
--- a/lib/librte_eal/common/malloc_heap.c
+++ b/lib/librte_eal/common/malloc_heap.c
@@ -563,6 +563,10 @@  malloc_heap_alloc(const char *type, size_t size, int socket_arg,
 	/* try other heaps */
 	for (i = 0; i < (int) rte_socket_count(); i++) {
 		cur_socket = rte_socket_id_by_idx(i);
+		if (cur_socket < 0) {
+			RTE_LOG(ERR, EAL, "Invalid socket index: %i\n", i);
+			continue;
+		}
 		if (cur_socket == socket)
 			continue;
 		ret = heap_alloc_on_socket(type, size, cur_socket, flags,