[LTP] [PATCH 04/15] ujson: writer: Fix open() return value check

Cyril Hrubis chrubis@suse.cz
Mon Oct 5 17:15:18 CEST 2026


Hi!
> open() returns -1 on failure and a non-negative file descriptor on
> success (including 0). Checking !writer_file->fd wrongly rejects fd 0
> as an error and accepts -1 on open failure. Check for negative fd
> instead.
> 
> Fixes: c50a0e0751c8 ("libs: Vendor ujson library")
> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  libs/ujson/ujson_writer.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/libs/ujson/ujson_writer.c b/libs/ujson/ujson_writer.c
> index b5da494d1..040ae680e 100644
> --- a/libs/ujson/ujson_writer.c
> +++ b/libs/ujson/ujson_writer.c
> @@ -475,7 +475,7 @@ ujson_writer *ujson_writer_file_open(const char *path)
>  	writer_file = (void*)ret + sizeof(ujson_writer);
>  
>  	writer_file->fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0664);
> -	if (!writer_file->fd) {
> +	if (writer_file->fd < 0) {
>  		free(ret);
>  		return NULL;
>  	}

This one is also very minor, since it's very unlikely that the fd 0
wouldn't be taken by stdin.

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list