[sheepdog] [PATCH] lib/logger: fix potential type conversion error

Liu Yuan namei.unix at gmail.com
Tue Apr 22 08:02:57 CEST 2014


On Mon, Apr 21, 2014 at 05:42:30PM +0800, Ruoyu wrote:
> The return type of snprintf is int. If an output error is encountered,
> a negative value is returned. Assigning a negative value to a variable
> in type of size_t is danger. The result will be a huge positive number
> and the error branch will never be reached.
> To avoid it, change the type of variable 'len' to int. In Comparison,
> convert it back to size_t.
> 
> Signed-off-by: Ruoyu <liangry at ucweb.com>
> ---
>  lib/logger.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/logger.c b/lib/logger.c
> index af7b32a..6e860c5 100644
> --- a/lib/logger.c
> +++ b/lib/logger.c
> @@ -171,7 +171,7 @@ static int server_log_formatter(char *buff, size_t size,
>  {
>  	char *p = buff;
>  	struct tm tm;
> -	size_t len;
> +	int len;
>  	char thread_name[MAX_THREAD_NAME_LEN];
>  
>  	if (print_time) {
> @@ -193,7 +193,7 @@ static int server_log_formatter(char *buff, size_t size,
>  		       msg->str, colorize ? TEXT_NORMAL : "");
>  	if (len < 0)
>  		len = 0;
> -	p += min(len, size - 1);
> +	p += min((size_t)len, size - 1);
>  
>  	return p - buff;
>  }
> @@ -212,7 +212,7 @@ static int json_log_formatter(char *buff, size_t size,
>  			      const struct logmsg *msg, bool print_time)
>  {
>  	char *p = buff;
> -	size_t len;
> +	int len;
>  
>  	assert(logger_user_info);
>  
> @@ -229,7 +229,7 @@ static int json_log_formatter(char *buff, size_t size,
>  		       msg->worker_idx, msg->func, msg->line);
>  	if (len < 0)
>  		return 0;
> -	len = min(len, size - 1);
> +	len = min((size_t)len, size - 1);
>  	p += len;
>  	size -= len;
>  
> -- 
> 1.8.3.2
> 
> 
> -- 
> sheepdog mailing list
> sheepdog at lists.wpkg.org
> http://lists.wpkg.org/mailman/listinfo/sheepdog

Probably ssize_t is better since strftime return size_t.

Thanks
Yuan



More information about the sheepdog mailing list