Skip to content

Commit

Permalink
Fixes from coverity scan
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusor committed Nov 22, 2017
1 parent f0c2262 commit 6b9218a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
35 changes: 21 additions & 14 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ struct http_request {
};

struct xml_node *xml_node_new(void);
static void xml_node_free(struct xml_node*);
struct xml_attribute *xml_attribute_new(const char*, size_t, const char*, size_t);
struct xml_node *xml_node_append_child(struct xml_node*, struct xml_node*);
static void XMLCALL begin_element(void *data, const char *element_name, const char **attributes)
Expand All @@ -180,7 +181,10 @@ static void XMLCALL begin_element(void *data, const char *element_name, const ch
struct xml_node *node = xml_node_new();
_trace("xml::begin_element(%p:%p): %s", state, node, element_name);
node->name_length = strlen(element_name);
if (node->name_length == 0) { return; }
if (node->name_length == 0) {
xml_node_free(node);
return;
}

node->name = get_zero_string(node->name_length);
strncpy(node->name, element_name, node->name_length);
Expand Down Expand Up @@ -246,9 +250,7 @@ static void XMLCALL begin_element(void *data, const char *element_name, const ch

node->parent = state->current_node;
_trace("xml::current_node::is(%p):%s", state->current_node, state->current_node->name);
if (NULL != state->current_node) {
xml_node_append_child(state->current_node, node);
}
xml_node_append_child(state->current_node, node);
state->current_node = node;
}

Expand Down Expand Up @@ -291,23 +293,27 @@ static void XMLCALL text_handle(void *data, const char *incoming, int length)

if (NULL == data) { return; }
if (NULL == incoming) { return; }
if (0 == length) { return; }

struct xml_state *state = data;
if (NULL == state->current_node) { return; }

if (string_trim((char**)&incoming, length, NULL) == 0) { return; }

struct xml_node *node = state->current_node;
char *local_cont = get_zero_string(length);
strncpy(local_cont, incoming, length);
size_t new_len = string_trim((char**)&local_cont, length, NULL);
if (new_len == 0) {
free(local_cont);
return;
} else {
struct xml_node *node = state->current_node;

if (length > 0) {
if (node->type == api_node_type_error) { }

node->content_length = length;
node->content = calloc(1, sizeof(char) * (length + 1));
strncpy (node->content, incoming, length);
_trace("xml::text_handle(%p//%u):%s", node, length, node->content);
node->content = get_zero_string(new_len);
strncpy (node->content, local_cont, new_len);
_trace("xml::text_handle(%p//%u):%s", node, new_len, node->content);
}
free(local_cont);
}

static void http_response_parse_xml_body(struct http_response *res, struct xml_node *document)
Expand Down Expand Up @@ -350,12 +356,13 @@ static inline struct xml_node *api_get_root_node(struct xml_node *doc)
return api_is_valid_doc(doc) ? doc->children[0] : NULL;
}

#if 0
char *api_response_get_name(struct xml_node *doc)
{
struct xml_node *root = api_get_root_node(doc);
if (NULL == root) { return NULL; }
return NULL;
}
#endif

char *api_response_get_session_key(struct xml_node *doc)
{
Expand Down Expand Up @@ -683,7 +690,7 @@ char *api_get_auth_url(enum api_type type, const char *token)
break;
case unknown:
default:
base_url = NULL;
return NULL;
}
const char *api_key = api_get_application_key(type);
size_t token_len = strlen(token);
Expand Down
19 changes: 8 additions & 11 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,19 @@ static char *get_config_file(struct configuration *config)
if (NULL == path) { return NULL; }

snprintf(path, path_len + 1, TOKENIZED_CONFIG_PATH, config->env->xdg_config_home, config->name, CONFIG_FILE_SUFFIX);
if (NULL == path) { return NULL; }

return path;
}

static char *get_credentials_cache_path(struct configuration *config, const char *file_name)
static char *get_credentials_cache_path(struct configuration *config, char *file_name)
{
if (NULL == config) { return NULL; }
if (NULL == config->name) { return NULL; }
if (NULL == config->env) { return NULL; }
if (NULL == config->env->xdg_data_home) { return NULL; }

bool free_file_name = false;
if (NULL == file_name) {
file_name = get_zero_string(0);
free_file_name = true;
file_name = "";
}

size_t name_len = strlen(config->name);
Expand All @@ -286,9 +283,6 @@ static char *get_credentials_cache_path(struct configuration *config, const char
if (NULL == path) { return NULL; }

snprintf(path, path_len + 1, TOKENIZED_CREDENTIALS_PATH, config->env->xdg_data_home, config->name, file_name);

if (free_file_name) { free((char*)file_name); }

return path;
}

Expand Down Expand Up @@ -318,7 +312,6 @@ char *get_pid_file(struct configuration *config)
if (NULL == path) { return NULL; }

snprintf(path, path_len + 1, TOKENIZED_PID_PATH, config->env->xdg_runtime_dir, config->name, PID_SUFFIX);
if (NULL == path) { return NULL; }

return path;
}
Expand Down Expand Up @@ -397,9 +390,8 @@ void load_from_ini_file(struct configuration *config, FILE *file)
if (NULL == file) { return; }

char *buffer = NULL;
if (!file) { goto _error; }

size_t file_size;

fseek(file, 0L, SEEK_END);
file_size = ftell(file);
file_size = imax(file_size, MAX_CONF_LENGTH);
Expand Down Expand Up @@ -630,7 +622,12 @@ int write_credentials_file(struct configuration *config)
if (NULL != file_path) {
_debug("saving::credentials[%u]: %s", config->credentials_length, file_path);
FILE *file = fopen(file_path, "w+");
if (NULL == file) {
_warn("saving::credentials:failed: %s", file_path);
goto _return;
}
status = write_ini_file(to_write, file);
fclose(file);
}

_return:
Expand Down
1 change: 1 addition & 0 deletions src/ini.rl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ ini_config *ini_load(char *data)
%% write exec;

free(group_name);
free(group);
free(value);
free(key);

Expand Down
2 changes: 1 addition & 1 deletion src/scrobble.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void scrobbles_append(struct mpris_player *player, const struct mpris_properties
struct mpris_properties *n = mpris_properties_new();
mpris_properties_copy(n, m);

if (player->queue_length > QUEUE_MAX_LENGTH) {
if (player->queue_length >= QUEUE_MAX_LENGTH) {
_error("scrobbler::queue_length_exceeded: please check connection");
mpris_properties_free(n);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/sdbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static void load_metadata(DBusMessageIter *iter, struct mpris_metadata *track, s
if (!strncmp(key, MPRIS_METADATA_BITRATE, strlen(MPRIS_METADATA_BITRATE))) {
//track->bitrate = extract_int32_var(&dictIter, &err);
extract_int32_var(&dictIter, (int32_t*)&track->bitrate, &err);
_debug(" loaded::metadata:bitrate: %d", track->bitrate);
_debug(" loaded::metadata:bitrate: %" PRId32, track->bitrate);
}
if (!strncmp(key, MPRIS_METADATA_ART_URL, strlen(MPRIS_METADATA_ART_URL))) {
//track->art_url = extract_string_var(&dictIter, &err);
Expand Down
4 changes: 3 additions & 1 deletion src/signon.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ static void reload_daemon(struct configuration *config)
return;
}

size_t pid;
size_t pid = 0;
if (fscanf(pid_file, "%lu", &pid) == 1 && kill(pid, SIGHUP) == 0) {
_info("signon::daemon_reload[%lu]: ok", pid);
} else {
_warn("signon::daemon_reload[%lu]: failed", pid);
}
fclose(pid_file);
}

static void get_session(struct api_credentials *creds)
Expand Down Expand Up @@ -122,6 +123,7 @@ static void get_token(struct api_credentials *creds)
} else {
_debug("xdg::opened[nok]: %s", auth_url);
}
free(auth_url);
free(open_cmd);
}

Expand Down
6 changes: 3 additions & 3 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ struct mpris_metadata {
char *url;
char *art_url; //mpris specific
uint64_t length; // mpris specific
unsigned short track_number;
unsigned short bitrate;
unsigned short disc_number;
unsigned track_number;
unsigned bitrate;
unsigned disc_number;
};

struct mpris_properties {
Expand Down
6 changes: 2 additions & 4 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,10 @@ struct parsed_arguments *parse_command_line(enum binary_type which_bin, int argc
args->service = unknown;
args->log_level = warning | error;

char *argument = NULL;
if (argc > 0) { argument = argv[1]; }

args->name = basename(argv[0]);

for (int i = 0 ; i < argc; i++) {
char *argument = NULL;
for (int i = 1 ; i < argc; i++) {
argument = argv[i];
if (strcmp(argument, ARG_HELP) == 0) {
args->has_help = true;
Expand Down

0 comments on commit 6b9218a

Please sign in to comment.