diff mbox series

[meta-oe,master,2/2] linux-serial-test: add patch to fix potential hang in while loop

Message ID 20251017104053.2983626-3-ghidoliemanuele@gmail.com
State New
Headers show
Series linux-serial-test: fix returned error code and potential hang in while loop | expand

Commit Message

Emanuele Ghidoli Oct. 17, 2025, 10:40 a.m. UTC
From: Emanuele Ghidoli <emanuele.ghidoli@toradex.com>

After commit 392f0f0ea76d ("linux-serial-test: Bump SRCREV to allow CMake 4+ compatibility"),
an infinite while loop can occur even when the timeout has been reached.

This patch fixes that regression.

Signed-off-by: Emanuele Ghidoli <emanuele.ghidoli@toradex.com>
---
 ...t.c-fix-potential-hang-in-while-loop.patch | 60 +++++++++++++++++++
 .../linux-serial-test_git.bb                  |  1 +
 2 files changed, 61 insertions(+)
 create mode 100644 meta-oe/recipes-test/linux-serial-test/files/0002-linux-serial-test.c-fix-potential-hang-in-while-loop.patch
diff mbox series

Patch

diff --git a/meta-oe/recipes-test/linux-serial-test/files/0002-linux-serial-test.c-fix-potential-hang-in-while-loop.patch b/meta-oe/recipes-test/linux-serial-test/files/0002-linux-serial-test.c-fix-potential-hang-in-while-loop.patch
new file mode 100644
index 000000000000..f0b7f65a601a
--- /dev/null
+++ b/meta-oe/recipes-test/linux-serial-test/files/0002-linux-serial-test.c-fix-potential-hang-in-while-loop.patch
@@ -0,0 +1,60 @@ 
+From 9cf6c1d80c2f159dbb69967fbe934bf6de73c9e8 Mon Sep 17 00:00:00 2001
+From: Max Krummenacher <max.krummenacher@toradex.com>
+Date: Tue, 5 Aug 2025 09:35:06 +0200
+Subject: [PATCH 2/2] linux-serial-test.c: fix potential hang in while loop
+
+process_read_data() assumes that we can always wait for reception
+of 1024 chars. However that is not true if one sets the number of
+chars with the '-w' cmdline parameter or chars are lost. Maybe there
+are other reasons.
+
+Replace the magic number of 1024 by calculating the number of expected
+chars from _cl_tx_bytes.
+
+Brake a possible infinite while loop by adding a timeout to the loop
+calculated from the expected chars times chartime.
+
+Upstream-Status: Submitted [https://github.com/cbrake/linux-serial-test/pull/61/]
+Fixes: 7fd1057f8a95 ("Add loop to read all data in rcv buffer")
+Signed-off-by: Max Krummenacher <max.krummenacher@toradex.com>
+Signed-off-by: Emanuele Ghidoli <emanuele.ghidoli@toradex.com>
+---
+ linux-serial-test.c | 14 ++++++++++++--
+ 1 file changed, 12 insertions(+), 2 deletions(-)
+
+diff --git a/linux-serial-test.c b/linux-serial-test.c
+index c2c8882d601b..294f53882570 100644
+--- a/linux-serial-test.c
++++ b/linux-serial-test.c
+@@ -543,8 +543,13 @@ static unsigned char next_count_value(unsigned char c)
+ static void process_read_data(void)
+ {
+ 	unsigned char rb[1024];
++	int loopcounter = 0;
+ 	int actual_read_count = 0;
+-	while (actual_read_count < 1024) {
++	int expected_read_count = _cl_tx_bytes == 0 ? 1024 : _cl_tx_bytes;
++	/* time for one char at current baudrate in us */
++	int chartime = 1000000 * (8 + _cl_parity + 1 + _cl_2_stop_bit) / _cl_baud;
++
++	while (actual_read_count < expected_read_count) {
+ 		int c = read(_fd, &rb, sizeof(rb));
+ 		if (c > 0) {
+ 			if (_cl_rx_dump) {
+@@ -577,7 +582,12 @@ static void process_read_data(void)
+ 			if (errno != EAGAIN) {
+ 				perror("read failed");
+ 			}
+-			continue; // Retry the read
++
++			if (loopcounter++ < expected_read_count) {
++				usleep(chartime);
++				continue; // Retry the read
++			}
++			break;
+ 		} else {
+ 		    break;
+ 		}
+-- 
+2.43.0
+
diff --git a/meta-oe/recipes-test/linux-serial-test/linux-serial-test_git.bb b/meta-oe/recipes-test/linux-serial-test/linux-serial-test_git.bb
index 66511bd9c312..7a206319e77d 100644
--- a/meta-oe/recipes-test/linux-serial-test/linux-serial-test_git.bb
+++ b/meta-oe/recipes-test/linux-serial-test/linux-serial-test_git.bb
@@ -5,6 +5,7 @@  LIC_FILES_CHKSUM = "file://LICENSES/MIT;md5=544799d0b492f119fa04641d1b8868ed"
 
 SRC_URI = "git://github.com/cbrake/linux-serial-test.git;protocol=https;branch=master \
            file://0001-linux-serial-test.c-fix-returned-error-code.patch \
+           file://0002-linux-serial-test.c-fix-potential-hang-in-while-loop.patch \
 "
 PV = "0+git"
 SRCREV = "1a81f3c7be086ee01a9be8589a606426276c86d5"