[sheepdog] [PATCH] lib/logger: fix potential type conversion error
Ruoyu
liangry at ucweb.com
Mon Apr 21 11:42:30 CEST 2014
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
More information about the sheepdog
mailing list