[sheepdog] [PATCH stable-0.8 4/4] lib/logger: fix potential type conversion error

Hitoshi Mitake mitake.hitoshi at gmail.com
Tue Apr 22 16:57:25 CEST 2014


From: Ruoyu <liangry at ucweb.com>

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 ssize_t. In Comparison,
convert it back to size_t.

Signed-off-by: Ruoyu <liangry at ucweb.com>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 lib/logger.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/logger.c b/lib/logger.c
index af7b32a..6829f45 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;
+	ssize_t 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;
+	ssize_t 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




More information about the sheepdog mailing list