Skip to content

Commit

Permalink
coverity: fix coverity issue report in crashlog
Browse files Browse the repository at this point in the history
1. fix the resource leak in check_aplogs_tobackup
2. fix the Memory - corruptions in add_section
3. fix Uninitialized variables in enable_watchdog
4. fix the resource leak in find_system_last_kmsg
5. fix the Uninitialized variables in do_last_kmsg_copy

Tests Done:
1. Build and boot
2. crashlog function works

Tracked-On: OAM-126073
Signed-off-by: Bo He <[email protected]>
  • Loading branch information
bhe4 committed Oct 8, 2024
1 parent 245f8a4 commit 18a2336
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crashlog/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static void add_section(char *config, pconfig_handle conf_handle) {
conf_handle->current->next = newsect;
}
conf_handle->current = newsect;
newsect->name = malloc(strlen(config));
newsect->name = malloc(strlen(config) + 1);
if(!newsect->name) {
if(newsect) {
free(newsect);
Expand Down
9 changes: 5 additions & 4 deletions crashlog/crashutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,14 @@ unsigned long long get_uptime(int refresh, int *error)
// Find system last kmsg from dropbox
static int find_system_last_kmsg(char *source, int source_length) {
struct dirent *entry;
DIR *dir = opendir(DROPBOX_DIR);
DIR *dir;
int file_exist = 0;

if (source == NULL) {
LOGE("source is NULL.\n");
return file_exist;
}
dir = opendir(DROPBOX_DIR);
if (dir == NULL) {
LOGE("No such directory: %s\n",DROPBOX_DIR);
return file_exist;
Expand All @@ -188,9 +189,9 @@ static int find_system_last_kmsg(char *source, int source_length) {
}

void do_last_kmsg_copy(char *dir) {
char destion[PATHMAX];
char source[PATHMAX];
char source[PATHMAX] = {0};
char sourcepath[PATHMAX];
char destion[PATHMAX];

if ( file_exists(LAST_KMSG) ) {
snprintf(destion, sizeof(destion), "%s/%s", dir, LAST_KMSG_FILE);
Expand All @@ -206,7 +207,7 @@ void do_last_kmsg_copy(char *dir) {
snprintf(destion, sizeof(destion), "%s/%s", dir, FTRACE_RAMOOPS_FILE);
do_copy_tail(FTRACE_RAMOOPS, destion, MAXFILESIZE);
}
if (find_system_last_kmsg(&source, sizeof(source))) {
if (find_system_last_kmsg(source, sizeof(source))) {
snprintf(destion, sizeof(destion), "%s/%s", dir, source);
snprintf(sourcepath, sizeof(sourcepath), "%s/%s", DROPBOX_DIR, source);
do_copy_tail(sourcepath, destion, MAXFILESIZE);
Expand Down
6 changes: 6 additions & 0 deletions crashlog/inotify_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ int receive_inotify_events(int inotify_fd) {
}
/* copy the last bytes received */
memcpy(lastevent, buffer, len);
/* Ensure there is enough space in the buffer for missing_bytes */
if (len + missing_bytes > sizeof(lastevent)) {
LOGE("%s: Buffer overflow prevented. The lastevent buffer is too small to hold the additional data.\n",
__FUNCTION__);
return -1;
}
/* now, reads the full last event, including its name field */
res = read(inotify_fd, &lastevent[len], missing_bytes);
if ( res != missing_bytes ) {
Expand Down
2 changes: 1 addition & 1 deletion crashlog/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ int do_monitor() {
return -1;
} else if( get_missing_watched_dir_nb() ) {
/* One or several directories couldn't have been added to inotify watcher */
handle_missing_watched_dir(file_monitor_fd);
handle_missing_watched_dir();
}

/* Set the inotify event callbacks */
Expand Down
3 changes: 3 additions & 0 deletions crashlog/panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ static int check_aplogs_tobackup(char *filename) {
LOGE("%s: Cannot transform the property %s(which is %s) into an array... error is %d - %s\n",
__FUNCTION__, PROP_IPANIC_PATTERN, ipanic_chain, nbpatterns, strerror(-nbpatterns));
/* allocated memory is freed in commachain_to_fixedarray */
if (patterns_array_32) {
free(patterns_array_32);
}
return 0;
}
if (nbpatterns == 0) {
Expand Down
2 changes: 1 addition & 1 deletion crashlog/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void crashlog_wd_handler(int signal,

int enable_watchdog(unsigned int timeout) {
struct sigaction sigact;
struct sigevent sevp;
struct sigevent sevp = {0};
struct itimerspec;

if (timeout == 0)
Expand Down

0 comments on commit 18a2336

Please sign in to comment.