[U-Boot] [PATCH v2 1/2] lib: errno: check for unsupported error number

Heinrich Schuchardt xypron.glpk at gmx.de
Tue Oct 15 19:46:03 UTC 2019


If errno_str() is called with an unsupported error number, do not return a
random pointer but a reasonable text.

Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
Reviewed-by: Simon Glass <sjg at chromium.org>
---
v2:
	no change
---
 lib/errno_str.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/errno_str.c b/lib/errno_str.c
index 0ba950e970..bb8f9fbeb3 100644
--- a/lib/errno_str.c
+++ b/lib/errno_str.c
@@ -136,6 +136,8 @@ static const char * const errno_message[] = {
 	ERRNO_MSG(EDQUOT, "Quota exceeded"),
 	ERRNO_MSG(ENOMEDIUM, "No medium found"),
 	ERRNO_MSG(EMEDIUMTYPE, "Wrong medium type"),
+	/* Message for unsupported error numbers */
+	ERRNO_MSG(0, "Unknown error"),
 };

 const char *errno_str(int errno)
@@ -143,5 +145,9 @@ const char *errno_str(int errno)
 	if (errno >= 0)
 		return errno_message[0];

-	return errno_message[abs(errno)];
+	errno = -errno;
+	if (errno >= ARRAY_SIZE(errno_message))
+		errno = ARRAY_SIZE(errno_message) - 1;
+
+	return errno_message[errno];
 }
--
2.23.0



More information about the U-Boot mailing list