Skip to contentSkip to conversation

lib: errno: check for unsupported error number

PATCH New Heinrich Schuchardt 3 participants 6 messages Oct 15, 2019 – Nov 1, 2019 Library code

lore
Share & tools
lore
#0 lore raw

v2 cover lib: errno: check for unsupported error number

errno_str() should not return a random pointer for unknown error codes.

Provide a unit test for errno_str().

v2:
Do not use constants for error messages in unit test.

Heinrich Schuchardt (2):
lib: errno: check for unsupported error number
test: provide test for errno_str()

 lib/errno_str.c           |  8 ++++++-
 test/lib/Makefile         |  1 +
 test/lib/test_errno_str.c | 46 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 test/lib/test_errno_str.c

--
2.23.0

#1 lore raw

PATCH v2 1/2 lib: errno: check for unsupported error number

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

---
v2:
no change

---
 lib/errno_str.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/errno_str.c +7 -1
index 0ba950e970..bb8f9fbeb3 100644
@@ -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

#2 lore raw

PATCH v2 1/2 lib: errno: check for unsupported error number

Quoted 5 lines · Heinrich Schuchardt

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

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191101/a8351c1e/attachment.sig>
#3 lore raw

PATCH v2 2/2 test: provide test for errno_str()

In reply to Heinrich Schuchardt

Provide a unit test for errno_str(). Test that known and unknown error numbers are handled correctly.

---
v2:
Do no use constants for strings.

---
 test/lib/Makefile         |  1 +
 test/lib/test_errno_str.c | 46 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 test/lib/test_errno_str.c
test/lib/Makefile +1 -0
index 308c61708e..b13aaca7ce 100644
@@ -6,3 +6,4 @@ obj-y += cmd_ut_lib.o
 obj-y += hexdump.o
 obj-y += lmb.o
 obj-y += string.o
+obj-$(CONFIG_ERRNO_STR) += test_errno_str.o
test/lib/test_errno_str.c new file +46 -0

46 lines not on this page · raw message

--
2.23.0

#4 lore raw

PATCH v2 2/2 test: provide test for errno_str()

Quoted 5 lines · Heinrich Schuchardt

Provide a unit test for errno_str(). Test that known and unknown error numbers are handled correctly.

Quoted 8 lines

---
v2:
Do no use constants for strings.

---
 test/lib/Makefile         |  1 +
 test/lib/test_errno_str.c | 46 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 test/lib/test_errno_str.c
#5 lore raw

PATCH v2 2/2 test: provide test for errno_str()

In reply to Heinrich Schuchardt
Quoted 5 lines · Heinrich Schuchardt

Provide a unit test for errno_str(). Test that known and unknown error numbers are handled correctly.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191101/b3f8c8bd/attachment.sig>