Skip to content

Commit ad36f17

Browse files
committed
opt(log):1.9.15, 消除AsyncSink中可能存在的数据内存对齐异常隐患
1 parent ca5ab03 commit ad36f17

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

modules/log/async_sink.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,31 @@ void AsyncSink::onLogBackEndReadPipe(const void *data_ptr, size_t data_size)
6363

6464
bool is_need_flush = false;
6565
while (buffer_.readableSize() >= sizeof(LogContent)) {
66-
auto content = reinterpret_cast<LogContent*>(buffer_.readableBegin());
67-
auto frame_size = sizeof(LogContent) + content->text_len;
66+
LogContent content;
67+
::memcpy(&content, buffer_.readableBegin(), sizeof(content));
68+
69+
auto frame_size = sizeof(LogContent) + content.text_len;
6870
if (frame_size > buffer_.readableSize()) //! 总结长度不够
6971
break;
70-
content->text_ptr = reinterpret_cast<const char *>(content + 1);
72+
73+
buffer_.hasRead(sizeof(content));
74+
75+
content.text_ptr = reinterpret_cast<const char*>(buffer_.readableBegin());
7176
onLogBackEnd(content);
77+
7278
is_need_flush = true;
73-
buffer_.hasRead(frame_size);
79+
buffer_.hasRead(content.text_len);
7480
}
7581

7682
if (is_need_flush)
7783
flushLog();
7884
}
7985

80-
void AsyncSink::onLogBackEnd(const LogContent *content)
86+
void AsyncSink::onLogBackEnd(const LogContent &content)
8187
{
8288
size_t buff_size = 1024; //! 初始大小,可应对绝大数情况
8389

84-
udpateTimestampStr(content->timestamp.sec);
90+
udpateTimestampStr(content.timestamp.sec);
8591

8692
//! 加循环为了应对缓冲不够的情况
8793
for (;;) {
@@ -95,34 +101,34 @@ void AsyncSink::onLogBackEnd(const LogContent *content)
95101

96102
//! 开启色彩,显示日志等级
97103
if (enable_color_) {
98-
len = snprintf(WRITE_PTR, REMAIN_SIZE, "\033[%sm", LOG_LEVEL_COLOR_CODE[content->level]);
104+
len = snprintf(WRITE_PTR, REMAIN_SIZE, "\033[%sm", LOG_LEVEL_COLOR_CODE[content.level]);
99105
pos += len;
100106
}
101107

102108
//! 打印等级、时间戳、线程号、模块名
103109
len = snprintf(WRITE_PTR, REMAIN_SIZE, "%c %s.%06u %ld %s ",
104-
LOG_LEVEL_LEVEL_CODE[content->level],
105-
timestamp_str_, content->timestamp.usec,
106-
content->thread_id, content->module_id);
110+
LOG_LEVEL_LEVEL_CODE[content.level],
111+
timestamp_str_, content.timestamp.usec,
112+
content.thread_id, content.module_id);
107113
pos += len;
108114

109-
if (content->func_name != nullptr) {
110-
len = snprintf(WRITE_PTR, REMAIN_SIZE, "%s() ", content->func_name);
115+
if (content.func_name != nullptr) {
116+
len = snprintf(WRITE_PTR, REMAIN_SIZE, "%s() ", content.func_name);
111117
pos += len;
112118
}
113119

114-
if (content->text_len > 0) {
115-
if (REMAIN_SIZE >= content->text_len)
116-
memcpy(WRITE_PTR, content->text_ptr, content->text_len);
117-
pos += content->text_len;
120+
if (content.text_len > 0) {
121+
if (REMAIN_SIZE >= content.text_len)
122+
memcpy(WRITE_PTR, content.text_ptr, content.text_len);
123+
pos += content.text_len;
118124

119125
if (REMAIN_SIZE >= 1) //! 追加一个空格
120126
*WRITE_PTR = ' ';
121127
++pos;
122128
}
123129

124-
if (content->file_name != nullptr) {
125-
len = snprintf(WRITE_PTR, REMAIN_SIZE, "-- %s:%d", content->file_name, content->line);
130+
if (content.file_name != nullptr) {
131+
len = snprintf(WRITE_PTR, REMAIN_SIZE, "-- %s:%d", content.file_name, content.line);
126132
pos += len;
127133
}
128134

modules/log/async_sink.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AsyncSink : public Sink {
4141

4242
virtual void onLogFrontEnd(const LogContent *content) override;
4343
void onLogBackEndReadPipe(const void *data_ptr, size_t data_size);
44-
void onLogBackEnd(const LogContent *content);
44+
void onLogBackEnd(const LogContent &content);
4545
virtual void appendLog(const char *str, size_t len) = 0;
4646
virtual void flushLog() { }
4747

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 9
24-
TBOX_VERSION_REVISION := 14
24+
TBOX_VERSION_REVISION := 15

0 commit comments

Comments
 (0)