[LTP] [PATCH 15/15] ujson: writer: Flush buffer before direct writes
Andrea Cervesato
andrea.cervesato@suse.de
Mon Oct 5 15:25:43 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
out_writer_file() writes chunks larger than a quarter of its buffer
directly to the file, bypassing data still sitting in the buffer. Any
string longer than 256 bytes was therefore written before the preceding
output, producing corrupted JSON. Flush the buffer first.
Fixes: c50a0e0751c8 ("libs: Vendor ujson library")
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
libs/ujson/ujson_writer.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/libs/ujson/ujson_writer.c b/libs/ujson/ujson_writer.c
index 2553d8cd2..df67b1f4d 100644
--- a/libs/ujson/ujson_writer.c
+++ b/libs/ujson/ujson_writer.c
@@ -424,8 +424,16 @@ static int out_writer_file(ujson_writer *self, const char *buf, size_t buf_len)
size_t buf_size = sizeof(writer_file->buf);
size_t buf_avail = buf_size - writer_file->buf_used;
- if (buf_len > buf_size/4)
+ if (buf_len > buf_size / 4) {
+ if (writer_file->buf_used &&
+ out_writer_file_write(self, writer_file->fd,
+ writer_file->buf, writer_file->buf_used))
+ return 1;
+
+ writer_file->buf_used = 0;
+
return out_writer_file_write(self, writer_file->fd, buf, buf_len);
+ }
if (buf_len >= buf_avail) {
if (out_writer_file_write(self, writer_file->fd,
--
2.51.0
More information about the ltp
mailing list