[LTP] [PATCH v2] fs/doio:Use the snprintf function to prevent buffer overflow

Cyril Hrubis chrubis@suse.cz
Mon Apr 24 11:29:36 CEST 2023


Hi!
> Use the snprintf function instead of sprintf in the write_log.c file
> 
> Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
> ---
>  testcases/kernel/fs/doio/write_log.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/testcases/kernel/fs/doio/write_log.c b/testcases/kernel/fs/doio/write_log.c
> index e8ef9c7cb..44e6fd165 100644
> --- a/testcases/kernel/fs/doio/write_log.c
> +++ b/testcases/kernel/fs/doio/write_log.c
> @@ -141,7 +141,7 @@ int wlog_open(struct wlog_file *wfile, int trunc, int mode)
>  
>  	oflags = O_RDWR;
>  	if ((wfile->w_rfd = open(wfile->w_file, oflags)) == -1) {
> -		sprintf(Wlog_Error_String,
> +		snprintf(Wlog_Error_String, sizeof(Wlog_Error_String),
>  			"Could not open write log - open(%s, %#o) failed:  %s\n",
>  			wfile->w_file, oflags, strerror(errno));
>  		close(wfile->w_afd);

There is one more sprintf() in the wlong_open() you have missed, can you
please fix that one as well?

Also with new enough GCC I'm still getting warnings about possible
truncation, looking at the Wlog_Error_String size it would make sense to
bump the buffer size to something as 2048.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list