From aaa8f6460337fdfce2ed2e132b3e7e024b114ab0 Mon Sep 17 00:00:00 2001 From: Longda Date: Mon, 9 Jan 2023 14:02:40 +0800 Subject: [PATCH] Closes #131, replace sprintf with snprintf (#134) --- deps/common/lang/string.cpp | 2 +- deps/common/log/log.h | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/deps/common/lang/string.cpp b/deps/common/lang/string.cpp index b79310c..626269c 100644 --- a/deps/common/lang/string.cpp +++ b/deps/common/lang/string.cpp @@ -194,7 +194,7 @@ char *bin_to_hex(const char *s, const int len, char *hex_buff) int new_len = 0; unsigned char *end = (unsigned char *)s + len; for (unsigned char *p = (unsigned char *)s; p < end; p++) { - new_len += sprintf(hex_buff + new_len, "%02x", *p); + new_len += snprintf(hex_buff + new_len, 2, "%02x", *p); } hex_buff[new_len] = '\0'; diff --git a/deps/common/log/log.h b/deps/common/log/log.h index bca3577..5966e5d 100644 --- a/deps/common/log/log.h +++ b/deps/common/log/log.h @@ -157,14 +157,16 @@ extern Log *g_log; #define __FILE_NAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #endif +#define LOG_HEAD_SIZE 64 + #define LOG_HEAD(prefix, level) \ if (common::g_log) { \ time_t now_time; \ time(&now_time); \ struct tm *p = localtime(&now_time); \ - char sz_head[64] = {0}; \ + char sz_head[LOG_HEAD_SIZE] = {0}; \ if (p) { \ - sprintf(sz_head, \ + snprintf(sz_head, LOG_HEAD_SIZE, \ "%d-%d-%d %d:%d:%u pid:%u tid:%llx ", \ p->tm_year + 1900, \ p->tm_mon + 1, \ -- GitLab