[LTP] [PATCH 03/15] ujson: reader: Prevent infinite loop on EOF

Andrea Cervesato andrea.cervesato@suse.de
Mon Oct 5 15:25:31 CEST 2026


From: Andrea Cervesato <andrea.cervesato@suse.com>

When read() returns 0 before reaching the size reported by lseek()
(e.g. sysfs files or truncated files), off does not advance and the
loop spins indefinitely. Treat read() == 0 as EOF by breaking the loop
and updating the buffer length and terminator.

Fixes: c50a0e0751c8 ("libs: Vendor ujson library")
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 libs/ujson/ujson_reader.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libs/ujson/ujson_reader.c b/libs/ujson/ujson_reader.c
index 9f86f25b7..2219c5801 100644
--- a/libs/ujson/ujson_reader.c
+++ b/libs/ujson/ujson_reader.c
@@ -1032,9 +1032,15 @@ ujson_reader *ujson_reader_load(const char *path)
 			goto err1;
 		}
 
+		if (res == 0)
+			break;
+
 		off += res;
 	}
 
+	ret->buf[off] = 0;
+	ret->len = off;
+
 	close(fd);
 
 	return ret;

-- 
2.51.0



More information about the ltp mailing list