tools: acrn-crashlog: fix potential issues

Changes include:
1. check the parameter of snprintf
2. remove atoi
3. remove sscanf
4. fix one memleak

Tracked-On: #1024
Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com>
Reviewed-by: Huang, Yonghua <yonghua.huang@intel.com>
Acked-by: Chen, Gang <gang.c.chen@intel.com>
This commit is contained in:
Liu, Xinwu
2018-10-30 17:26:44 +08:00
committed by lijinxia
parent 111f9726d0
commit 3ffa9686ca
13 changed files with 236 additions and 118 deletions

View File

@@ -62,6 +62,7 @@ int get_uptime_string(char *newuptime, int *hours)
{
long long tm;
int seconds, minutes;
int len;
tm = get_uptime();
if (tm == -1)
@@ -78,8 +79,11 @@ int get_uptime_string(char *newuptime, int *hours)
/* hours */
*hours /= 60;
return snprintf(newuptime, UPTIME_SIZE, "%04d:%02d:%02d", *hours,
minutes, seconds);
len = snprintf(newuptime, UPTIME_SIZE, "%04d:%02d:%02d", *hours,
minutes, seconds);
if (s_not_expect(len, UPTIME_SIZE))
return -1;
return 0;
}
int get_current_time_long(char *buf)
@@ -208,7 +212,7 @@ static int reserve_log_folder(enum e_dir_mode mode, char *dir,
int dlen;
struct sender_t *crashlog;
const char *outdir;
unsigned int maxdirs;
int maxdirs;
crashlog = get_sender_by_name("crashlog");
if (!crashlog)
@@ -246,9 +250,15 @@ static int reserve_log_folder(enum e_dir_mode mode, char *dir,
if (res < 0)
return res;
maxdirs = atoi(crashlog->maxcrashdirs);
if (cfg_atoi(crashlog->maxcrashdirs, crashlog->maxcrashdirs_len,
&maxdirs) == -1)
return -1;
if (maxdirs <= 0) {
LOGE("failed to reserve dir, maxdirs must be greater than 0\n");
return -1;
}
/* Open file in read/write mode to update the new current */
res = file_update_int(path, *current, maxdirs);
res = file_update_int(path, *current, (unsigned int)maxdirs);
if (res < 0)
return res;
@@ -408,8 +418,11 @@ int is_boot_id_changed(void)
if (res == -1 || !size)
return result;
snprintf(logged_boot_id_path, sizeof(logged_boot_id_path), "%s/%s",
crashlog->outdir, BOOTID_LOG);
res = snprintf(logged_boot_id_path, sizeof(logged_boot_id_path),
"%s/%s", crashlog->outdir, BOOTID_LOG);
if (s_not_expect(res, sizeof(logged_boot_id_path)))
goto out;
if (file_exists(logged_boot_id_path)) {
res = read_file(logged_boot_id_path, &size, &logged_boot_id);
if (res == -1 || !size)