[LTP] [PATCH 15/15] ujson: writer: Flush buffer before direct writes

Cyril Hrubis chrubis@suse.cz
Fri Oct 9 18:05:29 CEST 2026


Hi!
> 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))

The check for buf_len == 0 should be inside the out_writer_fiel_write()
so that we do not have to handle it in each caller.

diff --git a/libs/ujson/ujson_writer.c b/libs/ujson/ujson_writer.c
index c86a3b2af..29a863287 100644
--- a/libs/ujson/ujson_writer.c
+++ b/libs/ujson/ujson_writer.c
@@ -389,6 +389,9 @@ struct json_writer_file {
 
 static int out_writer_file_write(ujson_writer *self, int fd, const char *buf, ssize_t buf_len)
 {
+       if (!buf_len)
+               return 0;
+
        do {

> +			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,

For example we need the check here.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list