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

Cyril Hrubis chrubis@suse.cz
Mon Oct 5 17:12:25 CEST 2026


Hi!
> 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;
> +

This is mostly theoretical, since it's unlikely that the file we are
reading has changed size during the loop that reads it.

I suppose that it would be safer to just fail the load call instead of
loading truncated file that would likely be rejected by the parser later
on anyways.

So maybe just:

diff --git a/ujson_reader.c b/ujson_reader.c
index f4ec607..0f34bc5 100644
--- a/ujson_reader.c
+++ b/ujson_reader.c
@@ -1032,6 +1032,11 @@ ujson_reader *ujson_reader_load(const char *path)
                        goto err1;
                }

+               if (res == 0) {
+                       fprintf(stderr, "read() returned 0 (file was truncated?)\n");
+                       goto err1;
+               }
+
                off += res;
        }


-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list