diff mbox series

[1/1] log: correct and check array size of log categories

Message ID 20201023110001.6261-1-xypron.glpk@gmx.de
State Accepted
Delegated to: Tom Rini
Headers show
Series [1/1] log: correct and check array size of log categories | expand

Commit Message

Heinrich Schuchardt Oct. 23, 2020, 11 a.m. UTC
The log command has led to NULL dereferences if an unknown category name
name was used due to missing entries in the list of category names.

Add compile time checks for the array sizes of log_cat_name and
log_lvl_name to avoid future mishaps.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 common/log.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

--
2.28.0

Comments

Simon Glass Oct. 27, 2020, 4:52 a.m. UTC | #1
On Fri, 23 Oct 2020 at 05:00, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> The log command has led to NULL dereferences if an unknown category name
> name was used due to missing entries in the list of category names.
>
> Add compile time checks for the array sizes of log_cat_name and
> log_lvl_name to avoid future mishaps.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
>  common/log.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>
Tom Rini Oct. 28, 2020, 3:32 p.m. UTC | #2
On Fri, Oct 23, 2020 at 01:00:01PM +0200, Heinrich Schuchardt wrote:

> The log command has led to NULL dereferences if an unknown category name
> name was used due to missing entries in the list of category names.
> 
> Add compile time checks for the array sizes of log_cat_name and
> log_lvl_name to avoid future mishaps.
> 
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/common/log.c b/common/log.c
index b7a6ebe298..9b496b5c40 100644
--- a/common/log.c
+++ b/common/log.c
@@ -13,7 +13,7 @@ 

 DECLARE_GLOBAL_DATA_PTR;

-static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
+static const char *log_cat_name[] = {
 	"none",
 	"arch",
 	"board",
@@ -28,7 +28,10 @@  static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
 	"acpi",
 };

-static const char *log_level_name[LOGL_COUNT] = {
+_Static_assert(ARRAY_SIZE(log_cat_name) == LOGC_COUNT - LOGC_NONE,
+	       "log_cat_name size");
+
+static const char *log_level_name[] = {
 	"EMERG",
 	"ALERT",
 	"CRIT",
@@ -41,6 +44,8 @@  static const char *log_level_name[LOGL_COUNT] = {
 	"IO",
 };

+_Static_assert(ARRAY_SIZE(log_level_name) == LOGL_COUNT, "log_level_name size");
+
 const char *log_get_cat_name(enum log_category_t cat)
 {
 	const char *name;