Skip to content

Commit

Permalink
Merge pull request oracle#23 from drakenclimber/coverity
Browse files Browse the repository at this point in the history
Fix coverity warnings in adaptived
  • Loading branch information
sidkumar99 authored Nov 7, 2024
2 parents 69e33b1 + 6eac631 commit 4d3fcb3
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 35 deletions.
2 changes: 2 additions & 0 deletions adaptived/src/effects/cgroup_setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ static int _cgroup_setting_init(struct adaptived_effect * const eff, struct json
ret = -ENOMEM;
goto error;
}

memset(opts, 0, sizeof(struct cg_opts));
opts->value.type = ADAPTIVED_CGVAL_CNT;
opts->limit.type = ADAPTIVED_CGVAL_CNT;
opts->limit_provided = false;
Expand Down
2 changes: 2 additions & 0 deletions adaptived/src/effects/kill_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ int kill_cgroup_init(struct adaptived_effect * const eff, struct json_object *ar
goto error;
}

memset(opts, 0, sizeof(struct kill_cg_opts));

ret = adaptived_parse_string(args_obj, "cgroup", &cgroup_path_str);
if (ret)
goto error;
Expand Down
5 changes: 3 additions & 2 deletions adaptived/src/effects/kill_processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static int _kill_processes_init(struct adaptived_effect * const eff, struct json
goto error;
}

memset(opts->proc_names, '0', sizeof(char *) * opts->proc_name_cnt);
memset(opts->proc_names, '\0', sizeof(char *) * opts->proc_name_cnt);

for (i = 0; i < opts->proc_name_cnt; i++) {
proc_name_obj = json_object_array_get_idx(proc_names_obj, i);
Expand Down Expand Up @@ -165,7 +165,8 @@ static int _kill_processes_init(struct adaptived_effect * const eff, struct json
return ret;

error:
free_opts(opts);
if (opts)
free_opts(opts);

return ret;
}
Expand Down
32 changes: 16 additions & 16 deletions adaptived/src/effects/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,24 +299,23 @@ int logger_main(struct adaptived_effect * const eff)
strftime(dateline, FILENAME_MAX, opts->date_format, localtime(&now));
strcpy(&separator[strlen(separator)], dateline);
}
if (opts->separator_postfix) {

if (opts->separator_postfix)
strcpy(&separator[strlen(separator)], opts->separator_postfix);
if (ret < 0)
goto error;
}

adaptived_dbg("%s: separator = %s\n", __func__, separator);

write = fwrite(separator, 1, strlen(separator), log);
if (write != strlen(separator)) {
adaptived_err("logger_main: amount written (%d) != strlen(separator) (%d)\n",
adaptived_err("logger_main: amount written (%ld) != strlen(separator) (%ld)\n",
write, strlen(separator));
ret = -EINVAL;
goto error;
}
filep = opts->file_list;
do {
struct stat statbuf;
int size;
long size;

fnp = fopen(filep->filename, "r");
if (fnp == NULL)
Expand All @@ -332,7 +331,7 @@ int logger_main(struct adaptived_effect * const eff)
goto error;
}
size = statbuf.st_size;
if (!size || size >= opts->max_file_size)
if (size == 0 || size >= opts->max_file_size)
size = opts->max_file_size;
buf = malloc(size + 1);
if (!buf) {
Expand All @@ -355,23 +354,24 @@ int logger_main(struct adaptived_effect * const eff)
read = fread(&buf[strlen(buf)], 1,
min(opts->max_file_size, size), fnp);
fclose(fnp);
fnp = NULL;
if (read <= 0) {
adaptived_err("logger_main: amount read from %s (%d) != size (%d)\n",
adaptived_err("logger_main: amount read from %s (%ld) != size (%ld)\n",
filep->filename, read, size);
ret = -EINVAL;
goto error;
}
write = fwrite(separator, 1, strlen(separator), log);
if (write != strlen(separator)) {
adaptived_err("logger_main: amount written (%d) != "
"strlen(separator) (%d)\n", write, strlen(separator));
adaptived_err("logger_main: amount written (%ld) != "
"strlen(separator) (%ld)\n", write, strlen(separator));
ret = -EINVAL;
goto error;
}
write = fwrite(buf, 1, strlen(buf), log);
if (write != strlen(buf)) {
adaptived_err("logger_main: amount written (%d) != "
"strlen(buf) (%d) of %s\n",
adaptived_err("logger_main: amount written (%ld) != "
"strlen(buf) (%ld) of %s\n",
write, strlen(buf), filep->filename);
ret = -EINVAL;
goto error;
Expand All @@ -396,9 +396,9 @@ int logger_main(struct adaptived_effect * const eff)
return ret;
}

void logger_exit(struct adaptived_effect * const eff)
{
struct logger_opts *opts = (struct logger_opts *)eff->data;
void logger_exit(struct adaptived_effect * const eff)
{
struct logger_opts *opts = (struct logger_opts *)eff->data;

free_opts(opts);
free_opts(opts);
}
1 change: 1 addition & 0 deletions adaptived/src/effects/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ int print_init(struct adaptived_effect * const eff, struct json_object *args_obj
ret = -ENOMEM;
goto error;
}
memset(opts, 0, sizeof(struct print_opts));

ret = adaptived_parse_string(args_obj, "message", &msg_str);
if (ret == -ENOENT) {
Expand Down
2 changes: 1 addition & 1 deletion adaptived/src/effects/snooze.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int snooze_main(struct adaptived_effect * const eff)
diff = difftime(cur_time, opts->prev_trigger);
adaptived_dbg("Snooze duration: %d, Current diff: %.0lf\n", opts->duration, diff);

if (diff < (double)(opts->duration / 1000))
if (diff < (double)(opts->duration / 1000.0f))
/* inform adaptived to skip the remaining effects in this rule */
return -EALREADY;

Expand Down
14 changes: 10 additions & 4 deletions adaptived/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ API struct adaptived_ctx *adaptived_init(const char * const config_file)

API void adaptived_release(struct adaptived_ctx **ctx)
{
cleanup(*ctx);
if (ctx == NULL)
return;

if (*ctx)
free(*ctx);
cleanup(*ctx);
free(*ctx);

(*ctx) = NULL;
}
Expand Down Expand Up @@ -440,18 +441,22 @@ API int adaptived_loop(struct adaptived_ctx * const ctx, bool parse)
if (ret)
return ret;
}

pthread_mutex_lock(&ctx->ctx_mutex);
if (ctx->daemon_mode) {
adaptived_dbg("adaptived_loop: Try to run as daemon, nochdir = %d, noclose = %d\n",
ctx->daemon_nochdir, ctx->daemon_noclose);
ret = daemon(ctx->daemon_nochdir, ctx->daemon_noclose);
if (ret) {
adaptived_err("Failed to become daemon: %d.\n", errno);
pthread_mutex_unlock(&ctx->ctx_mutex);
return -errno;
}
adaptived_dbg("adaptived_loop: running as daemon.\n");
} else {
adaptived_dbg("adaptived_loop: Debug mode. Skip running as daemon.\n");
}
pthread_mutex_unlock(&ctx->ctx_mutex);

ctx->loop_cnt = 0;

Expand Down Expand Up @@ -546,7 +551,8 @@ API int adaptived_loop(struct adaptived_ctx * const ctx, bool parse)
if (!skip_sleep) {
sleep.tv_sec = interval / 1000;
sleep.tv_nsec = (interval % 1000) * 1000000LL;
adaptived_dbg("sleeping for %d seconds and %ld nanoseconds\n", sleep.tv_sec, sleep.tv_nsec);
adaptived_dbg("sleeping for %ld seconds and %ld nanoseconds\n",
sleep.tv_sec, sleep.tv_nsec);

ret = nanosleep(&sleep, NULL);
if (ret)
Expand Down
17 changes: 11 additions & 6 deletions adaptived/src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ API int adaptived_parse_string(struct json_object * const obj, const char * cons
json_bool exists;
int ret = 0;

if (!value && !(*value)) {
if (!value) {
ret = -EINVAL;
goto error;
}
Expand All @@ -86,6 +86,9 @@ API int adaptived_parse_string(struct json_object * const obj, const char * cons
return ret;

error:
if (value)
(*value) = NULL;

return ret;
}

Expand All @@ -95,7 +98,7 @@ API int adaptived_parse_int(struct json_object * const obj, const char * const k
int ret = 0;
char *end;

if (!value && !(*value)) {
if (!value) {
ret = -EINVAL;
goto error;
}
Expand All @@ -121,7 +124,7 @@ API int adaptived_parse_float(struct json_object * const obj, const char * const
int ret = 0;
char *end;

if (!value && !(*value)) {
if (!value) {
ret = -EINVAL;
goto error;
}
Expand All @@ -147,7 +150,7 @@ API int adaptived_parse_long_long(struct json_object * const obj, const char * c
int ret = 0;
char *end;

if (!value && !(*value)) {
if (!value) {
ret = -EINVAL;
goto error;
}
Expand Down Expand Up @@ -605,7 +608,9 @@ int parse_rule(struct adaptived_ctx * const ctx, struct json_object * const rule
return ret;

error:
rule_destroy(&rule);
if (rule)
rule_destroy(&rule);

return ret;
}

Expand Down Expand Up @@ -676,7 +681,7 @@ int parse_config(struct adaptived_ctx * const ctx)

chars_read = fread(buf, sizeof(char), config_size, config_fd);
if (chars_read != config_size) {
adaptived_err("Expected to read %d bytes but read %d bytes\n",
adaptived_err("Expected to read %ld bytes but read %ld bytes\n",
config_size, chars_read);
ret = -EIO;
goto out;
Expand Down
4 changes: 2 additions & 2 deletions adaptived/src/utils/cgroup_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
API int adaptived_cgroup_set_ll(const char * const setting, long long value, uint32_t flags)
{
long long validate_value;
size_t bytes_written;
ssize_t bytes_written;
char buf[LL_MAX];
int ret = 0;
int fd;
Expand Down Expand Up @@ -202,7 +202,7 @@ API int adaptived_cgroup_get_float(const char * const setting, float * const val
API int adaptived_cgroup_set_str(const char * const setting, const char * const value, uint32_t flags)
{
char *validate_value;
size_t bytes_written;
ssize_t bytes_written;
int ret = 0;
int fd;

Expand Down
2 changes: 1 addition & 1 deletion adaptived/src/utils/path_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ API int adaptived_path_walk_next(struct adaptived_path_walk_handle **handle, cha

path_len = strlen(whandle->path) + strlen(de->d_name) + 2;
*path = malloc(sizeof(char) * path_len);
if (!path)
if (!(*path))
return -ENOMEM;

sprintf(*path, "%s/%s", whandle->path, de->d_name);
Expand Down
3 changes: 3 additions & 0 deletions adaptived/src/utils/sched_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ API int adaptived_get_schedstat(const char * const schedstat_file, struct adapti
goto error;
}
} else if (0 == strncmp(line, "domain", 6)) {
if (cpu < 0)
continue;

token = strtok(line, " ");
domain = atoi(token + 6);
if (domain < 0 || domain >= MAX_DOMAIN_LEVELS) {
Expand Down
10 changes: 7 additions & 3 deletions adaptived/src/utils/sd_bus_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ static int handle_special_properties(sd_bus_message *m, const char *property, co
x = UINT64_MAX;
} else if (strcmp(real_property, "CPUQuotaPerSecUSec") == 0){
if (endswith(value->value.str_value, "%")) {
int percent = -1;
unsigned int percent;

items = sscanf(value->value.str_value, "%d%%", &percent);
if ((items != 1) || (percent == -1)) {
if (items != 1) {
adaptived_err("%s: sd_bus_message_append() failed, r=%d\n",
__func__, r);
return -EINVAL;
Expand Down Expand Up @@ -425,8 +425,12 @@ API int adaptived_sd_bus_set_str(const char * const target, const char * const p
char *validate_value = NULL;

ret = adaptived_sd_bus_get_str(target, property, &validate_value);
if (ret)
if (ret) {
if (validate_value)
free(validate_value);

return ret;
}

if (!validate_value) {
adaptived_err("Failed to validate %s.\n", property);
Expand Down

0 comments on commit 4d3fcb3

Please sign in to comment.