tools: acrn-crashlog: remove unsafe strlen in common

Remove strlen in common apis, and change their caller if necessary.

Tracked-On: #1254
Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
Acked-by: Chen Gang <gang.c.chen@intel.com>
This commit is contained in:
Liu, Xinwu
2018-09-27 14:00:34 +08:00
committed by Xie, Nanlin
parent f25bc50e68
commit 40dbdcde4f
14 changed files with 378 additions and 461 deletions

View File

@@ -4,46 +4,35 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-journal.h>
#include <stdarg.h>
#include "log_sys.h"
void do_log(const int level,
#ifdef DEBUG_ACRN_CRASHLOG
const char *func, const int line,
#endif
...)
void debug_log(const int level, const char *func, const int line, ...)
{
va_list args;
char *fmt;
char log[MAX_LOG_LEN];
int n = 0;
#ifdef DEBUG_ACRN_CRASHLOG
const char header_fmt[] = "<%-20s%5d>: ";
#endif
char *head;
char *msg;
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 */
vsnprintf(log + n, sizeof(log) - (size_t)n, fmt, args);
log[sizeof(log) - 1] = 0;
if (vasprintf(&msg, fmt, args) == -1)
return;
va_end(args);
sd_journal_print(level, "%s", log);
if (asprintf(&head, "<%-20s%5d>: ", func, line) == -1) {
free(msg);
return;
}
sd_journal_print(level, "%s%s", head, msg);
free(msg);
free(head);
}