tools: acrn-crashlog: fix build warnings with gcc8.1.1

This patch is to fix the build warning with gcc8.1.1. Most of them are
warnings for buffer overflow from snprintf and strncpy.

Signed-off-by: CHEN Gang <gang.c.chen@intel.com>
Reviewed-by: Zhi Jin <zhi.jin@intel.com>
Reviewed-by: Liu, Xinwu <xinwu.liu@intel.com>
Reviewed-by: xiaojin2 <xiaojing.liu@intel.com>
This commit is contained in:
CHEN Gang 2018-07-30 09:43:08 +08:00 committed by lijinxia
parent 6e77a8d5f1
commit 87a4abdd9d
7 changed files with 33 additions and 22 deletions

View File

@ -56,10 +56,10 @@ static void entry_to_history_line(struct history_entry *entry,
newline[0] = 0;
if (entry->log != NULL) {
char *ptr;
char tmp[MAXLINESIZE];
char tmp[PATH_MAX];
strncpy(tmp, entry->log, MAXLINESIZE);
tmp[MAXLINESIZE-1] = 0;
strncpy(tmp, entry->log, PATH_MAX);
tmp[PATH_MAX - 1] = 0;
ptr = strrchr(tmp, '/');
if (ptr && ptr[1] == 0)
ptr[0] = 0;

View File

@ -23,10 +23,14 @@
#define __PROPERTY_H__
#include "load_conf.h"
#define VERSION_SIZE 256
#define VERSION_SIZE 256
/* UUID_SIZE contains the UUID number, dashes and some buffer*/
#define UUID_SIZE 48
/* General BUILD_VERSION like 23690 */
#define BUILD_VERSION_SIZE 16
char guuid[VERSION_SIZE];
char gbuildversion[VERSION_SIZE];
char guuid[UUID_SIZE];
char gbuildversion[BUILD_VERSION_SIZE];
int init_properties(struct sender_t *sender);
int swupdated(struct sender_t *sender);

View File

@ -106,7 +106,7 @@ int get_current_time_long(char *buf)
static int compute_key(char *key, char *seed1, char *seed2)
{
static SHA_CTX *sha;
char buf[256] = {'\0',};
char buf[VERSION_SIZE] = {'\0',};
long long time_ns = 0;
char *tmp_key = key;
unsigned char results[SHA_DIGEST_LENGTH];
@ -134,7 +134,7 @@ static int compute_key(char *key, char *seed1, char *seed2)
return -1;
time_ns = get_uptime();
snprintf(buf, 256, "%s%s%s%s%lld", gbuildversion, guuid, seed1,
snprintf(buf, VERSION_SIZE, "%s%s%s%s%lld", gbuildversion, guuid, seed1,
seed2, time_ns);
ret = SHA1_Update(sha, (unsigned char *)buf, strlen(buf));
@ -171,7 +171,7 @@ static int compute_key(char *key, char *seed1, char *seed2)
static int compute_key256(char *key, char *seed)
{
static SHA256_CTX *sha;
char buf[256] = {'\0',};
char buf[VERSION_SIZE] = {'\0',};
long long time_ns = 0;
char *tmp_key = key;
unsigned char results[SHA256_DIGEST_LENGTH];
@ -199,7 +199,7 @@ static int compute_key256(char *key, char *seed)
return -1;
time_ns = get_uptime();
snprintf(buf, 256, "%s%s%s%lld", gbuildversion, guuid, seed,
snprintf(buf, VERSION_SIZE, "%s%s%s%lld", gbuildversion, guuid, seed,
time_ns);
ret = SHA256_Update(sha, (unsigned char *)buf, strlen(buf));
@ -430,7 +430,7 @@ void generate_crashfile(char *dir, char *event, char *hashkey,
*/
char *generate_log_dir(enum e_dir_mode mode, char *hashkey)
{
char path[PATH_MAX];
char *path;
char dir[PATH_MAX];
unsigned int current;
int ret;
@ -439,19 +439,22 @@ char *generate_log_dir(enum e_dir_mode mode, char *hashkey)
if (ret)
return NULL;
snprintf(path, sizeof(path), "%s%d_", dir, current);
strncat(path, hashkey,
(strlen(path) + strlen(hashkey) >= sizeof(path)) ?
(sizeof(path) - strlen(path)) : strlen(hashkey));
ret = asprintf(&path, "%s%d_%s", dir, current, hashkey);
if (ret == -1) {
LOGE("construct log path failed, out of memory\n");
hist_raise_infoerror("DIR CREATE");
return NULL;
}
ret = mkdir(path, 0777);
if (ret == -1) {
LOGE("Cannot create dir %s\n", path);
hist_raise_infoerror("DIR CREATE");
free(path);
return NULL;
}
return strdup(path);
return path;
}
int is_boot_id_changed(void)

View File

@ -47,7 +47,7 @@ static void get_device_id(struct sender_t *sender)
return;
}
ret = file_read_string(MACHINE_ID, guuid, VERSION_SIZE);
ret = file_read_string(MACHINE_ID, guuid, BUILD_VERSION_SIZE);
if (ret <= 0)
LOGE("Could not get mmc id: %d (%s)\n",
ret, strerror(-ret));
@ -56,7 +56,8 @@ static void get_device_id(struct sender_t *sender)
LOGE("Could not find DeviceId, set it to '%s'\n",
DEVICE_ID_UNKNOWN);
strncpy(guuid, DEVICE_ID_UNKNOWN, strlen(DEVICE_ID_UNKNOWN));
strncpy(guuid, DEVICE_ID_UNKNOWN, UUID_SIZE);
guuid[UUID_SIZE - 1] = '\0';
write:
overwrite_file(loguuid, guuid);
@ -66,7 +67,7 @@ write:
static int get_buildversion(struct sender_t *sender)
{
int ret;
char lastbuild[VERSION_SIZE];
char lastbuild[BUILD_VERSION_SIZE];
char *logbuildid;
char *currentbuild = gbuildversion;
@ -84,7 +85,7 @@ static int get_buildversion(struct sender_t *sender)
return ret;
}
ret = file_read_string(logbuildid, lastbuild, VERSION_SIZE);
ret = file_read_string(logbuildid, lastbuild, BUILD_VERSION_SIZE);
if (ret == -ENOENT ||
!ret ||
(ret > 0 && strcmp(currentbuild, lastbuild))) {

View File

@ -32,7 +32,7 @@
#define KB (1024)
#define MB (KB * KB)
#define MAXLINESIZE (4 * KB)
#define MAXLINESIZE (PATH_MAX + 128)
#define CPBUFFERSIZE (4 * KB)
#define PAGE_SIZE (4 * KB)

View File

@ -24,6 +24,8 @@
#define DUMP_FILE "/tmp/core"
#define BUFFER_SIZE 8196
#define LINK_LEN 512
/* 128 means the length of the DUMP_FILE */
#define FORMAT_LENGTH (LINK_LEN + 128)
static void loginfo(int fd, const char *fmt, ...)
{
@ -110,7 +112,7 @@ static int save_coredump(const char *filename)
static int get_backtrace(int pid, int fd, int sig, const char *comm)
{
char *membkt;
char format[512];
char format[FORMAT_LENGTH];
loginfo(fd, "\nBackTrace:\n\n");
memset(format, 0, sizeof(format));

View File

@ -112,6 +112,7 @@ static int socket_bind(int fd, const char *name)
if (name_len >= SUN_PATH_MAX)
return -1;
strncpy(addr.sun_path, name, SUN_PATH_MAX);
addr.sun_path[SUN_PATH_MAX - 1] = '\0';
unlink(addr.sun_path);
alen = strlen(addr.sun_path) + sizeof(addr.sun_family);