Skip to content

Commit

Permalink
Handle errors from statedir_set_*_path
Browse files Browse the repository at this point in the history
In the case of the system default paths being set, potential errors
from state_set_*_path functions were being ignored. Handle errors and
exit accordingly.

Previously the directories were not attempted to be created in this
code path so there was no error handling needed but with the mkdir
addition, no longer handling this erro will cause segfaults later on
as the mkdir failure prevents the path setting further causing
uninitalized variables to be used.

Signed-off-by: William Douglas <william.douglas@intel.com>
  • Loading branch information
bryteise committed Mar 7, 2023
1 parent 65d8d02 commit ba9649e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/swupd_lib/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,28 @@ static void set_default_state_dir_cache(void)
globals.state_dir_cache = NULL;
}

static void set_default_cache_dir(void)
static bool set_default_cache_dir(void)
{
(void)statedir_set_cache_path(STATE_DIR);
if (!statedir_set_cache_path(STATE_DIR)) {
error("Unable to set default cache dir '%s'\n", STATE_DIR);
return false;
}

return true;
}

static void set_default_data_dir(void)
static bool set_default_data_dir(void)
{
bool ret;
char *default_data_dir = NULL;

default_data_dir = sys_path_join("%s/%s", globals.path_prefix, STATE_DIR);
(void)statedir_set_data_path(default_data_dir);
ret = statedir_set_data_path(default_data_dir);
if (!ret) {
error("Unable to set default data dir '%s'\n", default_data_dir);
}
FREE(default_data_dir);
return ret;
}

static bool set_format_string(char *format)
Expand Down Expand Up @@ -400,13 +410,17 @@ bool globals_init(void)
}

if (!globals.cache_dir) {
set_default_cache_dir();
if (!set_default_cache_dir()) {
return false;
}
}

// data_dir is relative to the path_prefix so ALWAYS has to run after
// initializing path_prefix
if (!globals.data_dir) {
set_default_data_dir();
if (!set_default_data_dir()) {
return false;
}
}

if (!globals.format_string && !set_default_format_string()) {
Expand Down

0 comments on commit ba9649e

Please sign in to comment.