raw/ioat: Check for the NULL pointer after calling malloc

Message ID tencent_1793202DABFB8619F43A098D8CD0CA4FBA07@qq.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series raw/ioat: 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/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-abi-testing success Testing PASS

Commit Message

biggest dreamer June 27, 2022, 5:52 p.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: b7aaf417f93 ("raw/ioat: add bus driver for device scanning automatically")

Signed-off-by: Shiqi Liu <835703180@qq.com>
---
 drivers/raw/ioat/idxd_bus.c | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

Bruce Richardson July 5, 2022, 10:14 a.m. UTC | #1
On Tue, Jun 28, 2022 at 01:52:26AM +0800, 835703180@qq.com wrote:
> 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: b7aaf417f93 ("raw/ioat: add bus driver for device scanning automatically")
> 
> Signed-off-by: Shiqi Liu <835703180@qq.com>

Acked-by: Bruce Richardson <bruce.richardson@intel.com>
  
Thomas Monjalon July 5, 2022, 7:43 p.m. UTC | #2
27/06/2022 19:52, 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: b7aaf417f93 ("raw/ioat: add bus driver for device scanning automatically")
> 
> Signed-off-by: Shiqi Liu <835703180@qq.com>
> ---
> --- a/drivers/raw/ioat/idxd_bus.c
> +++ b/drivers/raw/ioat/idxd_bus.c
> @@ -301,6 +301,10 @@ dsa_scan(void)
>  		IOAT_PMD_DEBUG("%s(): found %s/%s", __func__, path, wq->d_name);
>  
>  		dev = malloc(sizeof(*dev));
> +		if (dev == NULL) {
> +			closedir(dev_dir);
> +			return ENOMEM;

Isn't it supposed to be a negative value?
  
Bruce Richardson July 6, 2022, 9:05 a.m. UTC | #3
On Tue, Jul 05, 2022 at 09:43:34PM +0200, Thomas Monjalon wrote:
> 27/06/2022 19:52, 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: b7aaf417f93 ("raw/ioat: add bus driver for device scanning automatically")
> > 
> > Signed-off-by: Shiqi Liu <835703180@qq.com>
> > ---
> > --- a/drivers/raw/ioat/idxd_bus.c
> > +++ b/drivers/raw/ioat/idxd_bus.c
> > @@ -301,6 +301,10 @@ dsa_scan(void)
> >  		IOAT_PMD_DEBUG("%s(): found %s/%s", __func__, path, wq->d_name);
> >  
> >  		dev = malloc(sizeof(*dev));
> > +		if (dev == NULL) {
> > +			closedir(dev_dir);
> > +			return ENOMEM;
> 
> Isn't it supposed to be a negative value?
> 
Yes, I missed that on my review. Reading the definition of what the bus
"scan" method is to return it indicates <0 on error.

/Bruce
  

Patch

diff --git a/drivers/raw/ioat/idxd_bus.c b/drivers/raw/ioat/idxd_bus.c
index 539f51b1b1..8ab4ed5885 100644
--- a/drivers/raw/ioat/idxd_bus.c
+++ b/drivers/raw/ioat/idxd_bus.c
@@ -301,6 +301,10 @@  dsa_scan(void)
 		IOAT_PMD_DEBUG("%s(): found %s/%s", __func__, path, wq->d_name);
 
 		dev = malloc(sizeof(*dev));
+		if (dev == NULL) {
+			closedir(dev_dir);
+			return ENOMEM;
+		}
 		if (dsa_addr_parse(wq->d_name, &dev->addr) < 0) {
 			IOAT_PMD_ERR("Error parsing WQ name: %s", wq->d_name);
 			free(dev);