lib: Check for the NULL pointer after calling malloc

Message ID tencent_7A58A935B4A00F49E866CC56C6D08D05DD0A@qq.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series lib: Check for the NULL pointer after calling malloc |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation success Compilation OK
ci/github-robot: build success github build: passed
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/intel-Testing success Testing PASS

Commit Message

biggest dreamer July 7, 2022, 9:23 a.m. UTC
  From: Shiqi Liu <835703180@qq.com>

As the possible failure of the malloc(), the not_checked and
checked could be NULL pointer.
Therefore, it should be better to check it in order to avoid
the dereference of the NULL pointer.

Fixes: fa8054c8c88 ("lib/node: add ethdev control")
Signed-off-by: Shiqi Liu <835703180@qq.com>
---
 lib/node/ethdev_ctrl.c | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Thomas Monjalon Oct. 10, 2022, 3:46 p.m. UTC | #1
07/07/2022 11:23, 835703180@qq.com:
> From: Shiqi Liu <835703180@qq.com>
> 
> As the possible failure of the malloc(), the not_checked and
> checked could be NULL pointer.
> Therefore, it should be better to check it in order to avoid
> the dereference of the NULL pointer.
> 
> Fixes: fa8054c8c88 ("lib/node: add ethdev control")
> Signed-off-by: Shiqi Liu <835703180@qq.com>

Removed useless braces and applied, thanks.
  

Patch

diff --git a/lib/node/ethdev_ctrl.c b/lib/node/ethdev_ctrl.c
index 5294607619..eef0785c21 100644
--- a/lib/node/ethdev_ctrl.c
+++ b/lib/node/ethdev_ctrl.c
@@ -75,6 +75,9 @@  rte_node_eth_config(struct rte_node_ethdev_config *conf, uint16_t nb_confs,
 
 			/* Add it to list of ethdev rx nodes for lookup */
 			elem = malloc(sizeof(ethdev_rx_node_elem_t));
+			if (elem == NULL) {
+				return -ENOMEM;
+			}
 			memset(elem, 0, sizeof(ethdev_rx_node_elem_t));
 			elem->ctx.port_id = port_id;
 			elem->ctx.queue_id = j;