Skip to content

Commit

Permalink
logging: Fix RTT backend drop mode and coverity issue
Browse files Browse the repository at this point in the history
Drop mode in RTT backend was broken. It is not the default
one so most likely it was unnoticed for a long time. Fixed
to report drop message when logs are lost.

Additionally, added assert to ensure that memmove does not
do memory overwrite.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
  • Loading branch information
nordic-krch authored and nashif committed Apr 12, 2019
1 parent 9411abb commit f35b854
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions subsys/logging/log_backend_rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#define DROP_MAX 99

#define DROP_MSG "\nmessages dropped: \r"
#define DROP_MSG "messages dropped: \r\n"

#define DROP_MSG_LEN (sizeof(DROP_MSG) - 1)

Expand Down Expand Up @@ -90,15 +90,15 @@ static int data_out_drop_mode(u8_t *data, size_t length, void *ctx)

static int char_out_drop_mode(u8_t data)
{
if (data == '\r') {
if (data == '\n') {
if (line_out_drop_mode()) {
return 1;
}
line_pos = drop_cnt > 0 ? line_buf + DROP_MSG_LEN : line_buf;
line_pos = line_buf;
return 0;
}

if (line_pos < line_buf + sizeof(line_buf) - 1) {
if (line_pos < line_buf + MESSAGE_SIZE - 1) {
*line_pos++ = data;
}

Expand All @@ -108,18 +108,27 @@ static int char_out_drop_mode(u8_t data)

static int line_out_drop_mode(void)
{
*line_pos = '\r';
/* line cannot be empty */
__ASSERT_NO_MSG(line_pos > line_buf);

/* Handle the case if line contains only '\n' */
if (line_pos - line_buf == 1) {
line_pos++;
}

*(line_pos - 1) = '\r';
*line_pos++ = '\n';

if (drop_cnt > 0 && !drop_warn) {
memmove(line_buf + DROP_MSG_LEN, line_buf,
line_pos - line_buf);
int cnt = MIN(drop_cnt, DROP_MAX);

__ASSERT_NO_MSG(line_pos - line_buf <= MESSAGE_SIZE);

memmove(line_buf + DROP_MSG_LEN, line_buf, line_pos - line_buf);
(void)memcpy(line_buf, drop_msg, DROP_MSG_LEN);
line_pos += DROP_MSG_LEN;
drop_warn = 1;
}

if (drop_warn) {
int cnt = MIN(drop_cnt, DROP_MAX);

if (cnt < 10) {
line_buf[DROP_MSG_LEN - 2] = ' ';
Expand All @@ -134,16 +143,16 @@ static int line_out_drop_mode(void)

RTT_LOCK();
int ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
line_buf, line_pos - line_buf + 1);
line_buf, line_pos - line_buf);
RTT_UNLOCK();

if (ret == 0) {
drop_cnt++;
return 0;
} else {
drop_cnt = 0;
drop_warn = 0;
}

drop_cnt = 0;
drop_warn = 0;
return 0;
}

Expand Down

0 comments on commit f35b854

Please sign in to comment.