[U-Boot] [PATCH 4/5] serial: zynq: Check priv pointer get via dev_get_priv()

Michal Simek michal.simek at xilinx.com
Thu Jun 14 09:32:57 UTC 2018


Make sure that functions are working with proper strcture.

Signed-off-by: Michal Simek <michal.simek at xilinx.com>
---

Reported by: Coverity (local)

---
 drivers/serial/serial_zynq.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/drivers/serial/serial_zynq.c b/drivers/serial/serial_zynq.c
index cc14bfa39cff..0fd9e4b4a857 100644
--- a/drivers/serial/serial_zynq.c
+++ b/drivers/serial/serial_zynq.c
@@ -107,10 +107,12 @@ int zynq_serial_setbrg(struct udevice *dev, int baudrate)
 {
 	struct zynq_uart_priv *priv = dev_get_priv(dev);
 	unsigned long clock;
-
 	int ret;
 	struct clk clk;
 
+	if (!priv)
+		return -EINVAL;
+
 	ret = clk_get_by_index(dev, 0, &clk);
 	if (ret < 0) {
 		dev_err(dev, "failed to get clock\n");
@@ -139,6 +141,9 @@ static int zynq_serial_probe(struct udevice *dev)
 {
 	struct zynq_uart_priv *priv = dev_get_priv(dev);
 
+	if (!priv)
+		return -EINVAL;
+
 	/* No need to reinitialize the UART after relocation */
 	if (gd->flags & GD_FLG_RELOC)
 		return 0;
@@ -150,8 +155,13 @@ static int zynq_serial_probe(struct udevice *dev)
 
 static int zynq_serial_getc(struct udevice *dev)
 {
+	struct uart_zynq *regs;
 	struct zynq_uart_priv *priv = dev_get_priv(dev);
-	struct uart_zynq *regs = priv->regs;
+
+	if (!priv)
+		return -EINVAL;
+
+	regs = priv->regs;
 
 	if (readl(&regs->channel_sts) & ZYNQ_UART_SR_RXEMPTY)
 		return -EAGAIN;
@@ -163,13 +173,21 @@ static int zynq_serial_putc(struct udevice *dev, const char ch)
 {
 	struct zynq_uart_priv *priv = dev_get_priv(dev);
 
+	if (!priv)
+		return -EINVAL;
+
 	return _uart_zynq_serial_putc(priv->regs, ch);
 }
 
 static int zynq_serial_pending(struct udevice *dev, bool input)
 {
 	struct zynq_uart_priv *priv = dev_get_priv(dev);
-	struct uart_zynq *regs = priv->regs;
+	struct uart_zynq *regs;
+
+	if (!priv)
+		return -EINVAL;
+
+	regs = priv->regs;
 
 	if (input)
 		return !(readl(&regs->channel_sts) & ZYNQ_UART_SR_RXEMPTY);
@@ -181,6 +199,9 @@ static int zynq_serial_ofdata_to_platdata(struct udevice *dev)
 {
 	struct zynq_uart_priv *priv = dev_get_priv(dev);
 
+	if (!priv)
+		return -EINVAL;
+
 	priv->regs = (struct uart_zynq *)dev_read_addr(dev);
 	if (IS_ERR(priv->regs))
 		return PTR_ERR(priv->regs);
-- 
1.9.1



More information about the U-Boot mailing list