acrn-hypervisor/tools/acrn-crashlog/common/log_sys.c
CHEN Gang 6494708f2f tools: acrn-crashlog: fix potential buffer overflow issues
This patch is to fix the potential buffer overflow issues.

Signed-off-by: CHEN Gang <gang.c.chen@intel.com>
Reviewed-by: Zhi Jin <zhi.jin@intel.com>
Reviewed-by: xiaojin2 <xiaojing.liu@intel.com>
2018-07-09 09:25:15 +08:00

54 lines
978 B
C

/*
* Copyright (C) <2018> Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include <systemd/sd-journal.h>
#include "log_sys.h"
void do_log(const int level,
#ifdef DEBUG_ACRN_CRASHLOG
const char *func, const int line,
#endif
...)
{
va_list args;
char *fmt;
char log[MAX_LOG_LEN];
char *msg_log;
int n = 0;
int msg_len = 0;
#ifdef DEBUG_ACRN_CRASHLOG
const char header_fmt[] = "<%-20s%5d>: ";
#endif
if (level > LOG_LEVEL)
return;
#ifdef DEBUG_ACRN_CRASHLOG
va_start(args, line);
#else
va_start(args, level);
#endif
fmt = va_arg(args, char *);
if (!fmt)
return;
#ifdef DEBUG_ACRN_CRASHLOG
/* header */
n = snprintf(log, sizeof(log), header_fmt, func, line);
if (n < 0 || (size_t)n >= sizeof(log))
n = 0;
#endif
msg_log = log + n;
msg_len = sizeof(log) - n;
/* msg */
vsnprintf(msg_log, msg_len, fmt, args);
log[sizeof(log) - 1] = 0;
va_end(args);
sd_journal_print(level, "%s", log);
}