[U-Boot] [PATCH v2 0/2] 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

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@gmx.de Reviewed-by: Simon Glass sjg@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

On Tue, Oct 15, 2019 at 09:46:03PM +0200, Heinrich Schuchardt wrote:
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@gmx.de Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!

Provide a unit test for errno_str(). Test that known and unknown error numbers are handled correctly.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de --- 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
diff --git a/test/lib/Makefile b/test/lib/Makefile index 308c61708e..b13aaca7ce 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -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 diff --git a/test/lib/test_errno_str.c b/test/lib/test_errno_str.c new file mode 100644 index 0000000000..8a9f1fd980 --- /dev/null +++ b/test/lib/test_errno_str.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2019 Heinrich Schuchardt xypron.glpk@gmx.de + * + * 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> +#include <test/ut.h> + +/** + * lib_errno_str() - unit test for errno_str() + * + * Test errno_str() with varied alignment and length of the copied buffer. + * + * @uts: unit test state + * Return: 0 = success, 1 = failure + */ +static int lib_errno_str(struct unit_test_state *uts) +{ + const char *msg; + + msg = errno_str(1); + ut_asserteq_str("Success", msg); + + msg = errno_str(0); + ut_asserteq_str("Success", msg); + + msg = errno_str(-ENOMEM); + ut_asserteq_str("Out of memory", msg); + + msg = errno_str(-99999); + ut_asserteq_str("Unknown error", msg); + + return 0; +} + +LIB_TEST(lib_errno_str, 0); -- 2.23.0

On Tue, 15 Oct 2019 at 13:46, Heinrich Schuchardt xypron.glpk@gmx.de wrote:
Provide a unit test for errno_str(). Test that known and unknown error numbers are handled correctly.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de
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
Reviewed-by: Simon Glass sjg@chromium.org

On Tue, Oct 15, 2019 at 09:46:04PM +0200, Heinrich Schuchardt wrote:
Provide a unit test for errno_str(). Test that known and unknown error numbers are handled correctly.
Signed-off-by: Heinrich Schuchardt xypron.glpk@gmx.de Reviewed-by: Simon Glass sjg@chromium.org
Applied to u-boot/master, thanks!
participants (3)
-
Heinrich Schuchardt
-
Simon Glass
-
Tom Rini