[U-Boot,v2,1/8] log: Fix incorect range check in log_get_cat_name()

Message ID 20180612060502.196817-2-sjg@chromium.org
State Under Review
Delegated to: Tom Rini
Headers show
Series
  • Fix some coverity warnings
Related show

Commit Message

Simon Glass June 12, 2018, 6:04 a.m.
This allows access to an element after the end of the array. Fix it.

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

Changes in v2:
- Change log_get_cat_name() to always return a valid string, even if the
    uclass is not found

 common/log.c  | 10 +++++++---
 include/log.h |  3 ++-
 2 files changed, 9 insertions(+), 4 deletions(-)

Comments

Tom Rini June 19, 2018, 6:42 p.m. | #1
On Tue, Jun 12, 2018 at 12:04:55AM -0600, Simon Glass wrote:

> This allows access to an element after the end of the array. Fix it.
> 
> Reported-by: Coverity (CID: 173279)
> Signed-off-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

Patch

diff --git a/common/log.c b/common/log.c
index 3b5588ebe7..59869cd29d 100644
--- a/common/log.c
+++ b/common/log.c
@@ -38,12 +38,16 @@  static const char *log_level_name[LOGL_COUNT] = {
 
 const char *log_get_cat_name(enum log_category_t cat)
 {
-	if (cat > LOGC_COUNT)
-		return "invalid";
+	const char *name;
+
+	if (cat < 0 || cat >= LOGC_COUNT)
+		return "<invalid>";
 	if (cat >= LOGC_NONE)
 		return log_cat_name[cat - LOGC_NONE];
 
-	return uclass_get_name((enum uclass_id)cat);
+	name = uclass_get_name((enum uclass_id)cat);
+
+	return name ? name : "<missing>";
 }
 
 enum log_category_t log_get_cat_by_name(const char *name)
diff --git a/include/log.h b/include/log.h
index a3edd25546..3e99d6e62b 100644
--- a/include/log.h
+++ b/include/log.h
@@ -274,7 +274,8 @@  struct log_filter {
  * log_get_cat_name() - Get the name of a category
  *
  * @cat: Category to look up
- * @return category name (which may be a uclass driver name)
+ * @return category name (which may be a uclass driver name) if found, or
+ *	 "<invalid>" if invalid, or "<missing>" if not found
  */
 const char *log_get_cat_name(enum log_category_t cat);