tools: acrn-crashlog: remove unsafe api sprintf

Using snprintf intead of sprintf.

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-25 10:31:53 +08:00 committed by Xie, Nanlin
parent 5ecf1078ca
commit fb0292846d
2 changed files with 40 additions and 12 deletions

View File

@ -32,6 +32,7 @@
#include "history.h"
#include "log_sys.h"
#include "probeutils.h"
#include "strutils.h"
#define HISTORY_FIRST_LINE_FMT \
"#V1.0 CURRENTUPTIME %-24s\n"
@ -186,7 +187,12 @@ void hist_raise_uptime(char *lastuptime)
return;
}
sprintf(firstline, HISTORY_FIRST_LINE_FMT, boot_time);
ret = snprintf(firstline, sizeof(firstline),
HISTORY_FIRST_LINE_FMT, boot_time);
if (s_not_expect(ret, sizeof(firstline))) {
LOGE("failed to construct the firstline\n");
return;
}
replace_file_head(history_file, firstline);
if (hours / uptime_hours >= loop_uptime_event) {
@ -249,8 +255,9 @@ static int get_time_firstline(char *buffer)
int prepare_history(void)
{
int ret;
int llen;
struct sender_t *crashlog;
char linebuf[MAXLINESIZE] = {0};
char linebuf[MAXLINESIZE];
crashlog = get_sender_by_name("crashlog");
if (!crashlog)
@ -272,7 +279,12 @@ int prepare_history(void)
} else {
/* new history */
LOGW("new history\n");
sprintf(linebuf, HISTORY_FIRST_LINE_FMT, "0000:00:00");
llen = snprintf(linebuf, sizeof(linebuf),
HISTORY_FIRST_LINE_FMT, "0000:00:00");
if (s_not_expect(llen, sizeof(linebuf))) {
LOGE("failed to construct the fristline\n");
return -EINVAL;
}
ret = overwrite_file(history_file, linebuf);
if (ret < 0) {
LOGE("Write (%s, %s) failed, error (%s)\n",

View File

@ -33,6 +33,7 @@
#include "load_conf.h"
#include "log_sys.h"
#include "probeutils.h"
#include "strutils.h"
#define CRASH_CURRENT_LOG "currentcrashlog"
#define STATS_CURRENT_LOG "currentstatslog"
@ -98,6 +99,7 @@ static int compute_key(char *key, size_t key_len, const char *seed)
{
SHA256_CTX sha;
char buf[VERSION_SIZE];
int len;
long long time_ns;
char *tmp_key = key;
unsigned char results[SHA256_DIGEST_LENGTH];
@ -110,7 +112,10 @@ static int compute_key(char *key, size_t key_len, const char *seed)
SHA256_Init(&sha);
time_ns = get_uptime();
snprintf(buf, VERSION_SIZE, "%s%s%lld", gbuildversion, guuid, time_ns);
len = snprintf(buf, VERSION_SIZE, "%s%s%lld",
gbuildversion, guuid, time_ns);
if (s_not_expect(len , VERSION_SIZE))
return -1;
SHA256_Update(&sha, (unsigned char *)buf, strlen(buf));
SHA256_Update(&sha, (unsigned char *)seed, strlen(seed));
@ -118,7 +123,9 @@ static int compute_key(char *key, size_t key_len, const char *seed)
SHA256_Final(results, &sha);
for (i = 0; i < key_len / 2; i++) {
sprintf(tmp_key, "%02x", results[i]);
len = snprintf(tmp_key, 3, "%02x", results[i]);
if (s_not_expect(len, 3))
return -1;
tmp_key += 2;
}
*tmp_key = 0;
@ -194,8 +201,10 @@ char *generate_event_id(const char *seed1, const char *seed2,
static int reserve_log_folder(enum e_dir_mode mode, char *dir,
unsigned int *current)
{
char path[512];
char path[PATH_MAX];
int res;
int plen;
int dlen;
struct sender_t *crashlog;
char *outdir;
unsigned int maxdirs;
@ -208,22 +217,29 @@ static int reserve_log_folder(enum e_dir_mode mode, char *dir,
switch (mode) {
case MODE_CRASH:
sprintf(path, "%s/%s", outdir, CRASH_CURRENT_LOG);
sprintf(dir, "%s/%s", outdir, "crashlog");
plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
CRASH_CURRENT_LOG);
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "crashlog");
break;
case MODE_STATS:
sprintf(path, "%s/%s", outdir, STATS_CURRENT_LOG);
sprintf(dir, "%s/%s", outdir, "stats");
plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
STATS_CURRENT_LOG);
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "stats");
break;
case MODE_VMEVENT:
sprintf(path, "%s/%s", outdir, VM_CURRENT_LOG);
sprintf(dir, "%s/%s", outdir, "vmevent");
plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
VM_CURRENT_LOG);
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "vmevent");
break;
default:
LOGW("Invalid mode %d\n", mode);
return -1;
}
if (s_not_expect(plen, PATH_MAX) || s_not_expect(dlen, PATH_MAX)) {
LOGE("the length of path/dir is too long\n");
return -1;
}
/* Read current value in file */
res = file_read_int(path, current);
if (res < 0)