[PATCH] tty/serial: Check for null pointer after calling devm_ioremap

Jiasheng Jiang posted 1 patch 4 years, 5 months ago
drivers/tty/serial/milbeaut_usio.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH] tty/serial: Check for null pointer after calling devm_ioremap
Posted by Jiasheng Jiang 4 years, 5 months ago
As the possible failure of the allocation, the devm_ioremap() may return
NULL pointer.
And the 'port->membase' will be directly used in mlb_usio_startup().
Therefore, in order to avoid the dereference of the NULL pointer, it
should be better to add the sanity check.

Fixes: ba44dc043004 ("serial: Add Milbeaut serial control")
Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
---
 drivers/tty/serial/milbeaut_usio.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/tty/serial/milbeaut_usio.c b/drivers/tty/serial/milbeaut_usio.c
index 8f2cab7f66ad..1ecbf6d0dc79 100644
--- a/drivers/tty/serial/milbeaut_usio.c
+++ b/drivers/tty/serial/milbeaut_usio.c
@@ -523,6 +523,10 @@ static int mlb_usio_probe(struct platform_device *pdev)
 	}
 	port->membase = devm_ioremap(&pdev->dev, res->start,
 				resource_size(res));
+	if (!port->membase) {
+		ret = -ENOMEM;
+		goto failed;
+	}
 
 	ret = platform_get_irq_byname(pdev, "rx");
 	mlb_usio_irq[index][RX] = ret;
-- 
2.25.1

Re: [PATCH] tty/serial: Check for null pointer after calling devm_ioremap
Posted by Greg KH 4 years, 5 months ago
On Thu, Jan 06, 2022 at 05:06:31PM +0800, Jiasheng Jiang wrote:
> As the possible failure of the allocation, the devm_ioremap() may return
> NULL pointer.

I do not understand this sentence.

> And the 'port->membase' will be directly used in mlb_usio_startup().

This does not make sense either.


> Therefore, in order to avoid the dereference of the NULL pointer, it
> should be better to add the sanity check.

What do you mean by "sanity check"?

confused,

greg k-h