Skip to contentSkip to conversation

lib: errno: check for unsupported error number

PATCH Superseded Heinrich Schuchardt 3 participants 9 messages Oct 6, 2019 – Oct 14, 2019 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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().

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 | 50 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 test/lib/test_errno_str.c

--
2.23.0

#1 lore raw

PATCH 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.

---
 lib/errno_str.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/errno_str.c +7 -1
index fbe56395d1..2e5f4a887d 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

#3 lore raw

PATCH 1/2 lib: errno: check for unsupported errors

In reply to Heinrich Schuchardt

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

---
 lib/errno_str.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/errno_str.c +7 -1
index fbe56395d1..2e5f4a887d 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

#5 lore raw

PATCH 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.

---
 test/lib/Makefile         |  1 +
 test/lib/test_errno_str.c | 50 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 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 +50 -0

50 lines not on this page · raw message

--
2.23.0

#6 lore raw

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

Hi Heinrich,

Quoted 37 lines · Heinrich Schuchardt

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

---
 test/lib/Makefile         |  1 +
 test/lib/test_errno_str.c | 50 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 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 +11 -0
new file mode 100644
index 0000000000..77072d04f9
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <[email protected]>
+ *
+ * Unit tests for memory functions
+ *
+ * The architecture dependent implementations run through different lines of
+ * code depending on the alignment and length of memory regions copied or set.
+ * This has to be considered in testing.
+ */
+

test/lib/test_errno_str.c line 21

Quoted 10 lines
test/lib/test_errno_str.c new file +10 -0
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static const char errno_str_ok[] = "Success";
+static const char errno_str_enomem[] = "Out of memory";
+static const char errno_str_unknown[] = "Unknown error";

I think in a test it is better to open-code the strings below rather than put them in separate constants.

Regards,
Simon

#7 lore raw

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

Quoted 45 lines · Simon Glass

Hi Heinrich,

Quoted 42 lines · Heinrich Schuchardt

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

---
  test/lib/Makefile         |  1 +
  test/lib/test_errno_str.c | 50 +++++++++++++++++++++++++++++++++++++++
  2 files changed, 51 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 +16 -0
new file mode 100644
index 0000000000..77072d04f9
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <[email protected]>
+ *
+ * Unit tests for memory functions
+ *
+ * The architecture dependent implementations run through different lines of
+ * code depending on the alignment and length of memory regions copied or set.
+ * This has to be considered in testing.
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <test/lib.h>
+#include <test/test.h>
Quoted 8 lines

test/lib/test_errno_str.c line 21

Quoted 5 lines
test/lib/test_errno_str.c new file +5 -0
+#include <test/ut.h>
+
+static const char errno_str_ok[] = "Success";
+static const char errno_str_enomem[] = "Out of memory";
+static const char errno_str_unknown[] = "Unknown error";

I think in a test it is better to open-code the strings below rather than put them in separate constants.

Thanks for reviewing.

The string errno_str_ok is used twice. That is why I opted for constants. test/compression.c is also using predefined constants. So it is not without precedent.

But this is only a matter of taste.

@Tom
Which way do you want it (as maintainer for 'THE REST')?

Best regards

Heinrich

#8 lore raw

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

Quoted 57 lines · Heinrich Schuchardt
Quoted 45 lines · Simon Glass

Hi Heinrich,

Quoted 42 lines · Heinrich Schuchardt

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

---
  test/lib/Makefile         |  1 +
  test/lib/test_errno_str.c | 50 +++++++++++++++++++++++++++++++++++++++
  2 files changed, 51 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 +16 -0
new file mode 100644
index 0000000000..77072d04f9
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2019 Heinrich Schuchardt <[email protected]>
+ *
+ * Unit tests for memory functions
+ *
+ * The architecture dependent implementations run through different lines of
+ * code depending on the alignment and length of memory regions copied or set.
+ * This has to be considered in testing.
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <test/lib.h>
+#include <test/test.h>
Quoted 8 lines

test/lib/test_errno_str.c line 21

Quoted 5 lines
test/lib/test_errno_str.c new file +5 -0
+#include <test/ut.h>
+
+static const char errno_str_ok[] = "Success";
+static const char errno_str_enomem[] = "Out of memory";
+static const char errno_str_unknown[] = "Unknown error";

I think in a test it is better to open-code the strings below rather than put them in separate constants.

Thanks for reviewing.

Quoted 8 lines

The string errno_str_ok is used twice. That is why I opted for constants. test/compression.c is also using predefined constants. So it is not without precedent.

But this is only a matter of taste.

@Tom
Which way do you want it (as maintainer for 'THE REST')?

I think the compression test is special and otherwise yes, we should open-code them and let the compiler do its thing.

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/20191014/24682d48/attachment.sig>