diff mbox series

[02/13] test: Detect when expect_str is too small

Message ID 20210317181840.3975687-3-sjg@chromium.org
State Changes Requested
Delegated to: Tom Rini
Headers show
Series display_options: Start to unify print_buffer() and hexdump | expand

Commit Message

Simon Glass March 17, 2021, 6:18 p.m. UTC
If a line of more than 256 bytes is generated, the test will fail but the
reason is not clear. Add a check for this condition and print a helpful
message.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/ut.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/test/ut.c b/test/ut.c
index a0fe5facac7..350509a2926 100644
--- a/test/ut.c
+++ b/test/ut.c
@@ -68,11 +68,17 @@  static int readline_check(struct unit_test_state *uts)
 int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
 {
 	va_list args;
+	int len;
 	int ret;
 
 	va_start(args, fmt);
-	vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
+	len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
 	va_end(args);
+	if (len >= sizeof(uts->expect_str)) {
+		ut_fail(uts, __FILE__, __LINE__, __func__,
+			"unit_test_state->expect_str too small");
+		return -EOVERFLOW;
+	}
 	ret = readline_check(uts);
 	if (ret < 0)
 		return ret;
@@ -83,11 +89,17 @@  int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
 int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
 {
 	va_list args;
+	int len;
 	int ret;
 
 	va_start(args, fmt);
-	vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
+	len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
 	va_end(args);
+	if (len >= sizeof(uts->expect_str)) {
+		ut_fail(uts, __FILE__, __LINE__, __func__,
+			"unit_test_state->expect_str too small");
+		return -EOVERFLOW;
+	}
 	ret = readline_check(uts);
 	if (ret < 0)
 		return ret;