diff mbox series

[U-Boot,2/8] console: Fix handling of NULL global_data

Message ID 20180609182235.33532-3-sjg@chromium.org
State Superseded
Delegated to: Tom Rini
Headers show
Series Fix some coverity warnings | expand

Commit Message

Simon Glass June 9, 2018, 6:22 p.m. UTC
Both putc() and puts() can be called before global_data is set up. Some of
the code paths don't handle this correctly. Add an explicit test before
any member is accessed.

Reported-by: Coverity (CID: 169030)
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/console.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Heinrich Schuchardt June 9, 2018, 7:32 p.m. UTC | #1
On 06/09/2018 08:22 PM, Simon Glass wrote:
> Both putc() and puts() can be called before global_data is set up. Some of
> the code paths don't handle this correctly. Add an explicit test before
> any member is accessed.
> 
> Reported-by: Coverity (CID: 169030)
> Signed-off-by: Simon Glass <sjg@chromium.org>

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
diff mbox series

Patch

diff --git a/common/console.c b/common/console.c
index 2688af56e1..2ba33dc574 100644
--- a/common/console.c
+++ b/common/console.c
@@ -502,8 +502,10 @@  void putc(const char c)
 		return;
 	}
 #endif
+	if (!gd)
+		return;
 #ifdef CONFIG_CONSOLE_RECORD
-	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
+	if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
 		membuff_putbyte(&gd->console_out, c);
 #endif
 #ifdef CONFIG_SILENT_CONSOLE
@@ -541,8 +543,10 @@  void puts(const char *s)
 		return;
 	}
 #endif
+	if (!gd)
+		return;
 #ifdef CONFIG_CONSOLE_RECORD
-	if (gd && (gd->flags & GD_FLG_RECORD) && gd->console_out.start)
+	if ((gd->flags & GD_FLG_RECORD) && gd->console_out.start)
 		membuff_put(&gd->console_out, s, strlen(s));
 #endif
 #ifdef CONFIG_SILENT_CONSOLE