Skip to content

Commit

Permalink
[config] Never return NULL mode setting
Browse files Browse the repository at this point in the history
If usb-moded configuration is corrupted, it can lead to segfaults as the
return value from config_get_mode_setting() is not always checked.

Make config_get_mode_setting() always return a valid c-string.

Use MODE_CHARGING as fallback value, and remove any alternative fallback
strategies used elsewhere in the code base.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Sep 5, 2018
1 parent 92bcb71 commit 67df698
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/usb_moded-config.c
Expand Up @@ -400,11 +400,20 @@ static char * config_get_kcmdline_string(const char *entry)

char * config_get_mode_setting(void)
{
char * mode = config_get_kcmdline_string(MODE_SETTING_KEY);
if (mode != NULL)
return(mode);
char *mode = 0;

return(config_get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
/* Kernel command line can be used to override settings */
if( (mode = config_get_kcmdline_string(MODE_SETTING_KEY)) )
goto EXIT;

if( (mode = config_get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY)) )
goto EXIT;

/* If no default mode is configured, treat it as charging only */
mode = g_strdup(MODE_CHARGING);

EXIT:
return mode;
}
/*
* @param settingsfile: already opened settingsfile we want to read an entry from
Expand Down
7 changes: 0 additions & 7 deletions src/usb_moded-dbus.c
Expand Up @@ -417,13 +417,6 @@ static DBusHandlerResult umdbus_msg_handler(DBusConnection *const connection, DB
{
char *config = config_get_mode_setting();

if(!config)
{
/* Config is corrupted or we do not have a mode
* configured, fallback to undefined. */
config = g_strdup(MODE_UNDEFINED);
}

if((reply = dbus_message_new_method_return(msg)))
dbus_message_append_args (reply, DBUS_TYPE_STRING, &config, DBUS_TYPE_INVALID);
g_free(config);
Expand Down

0 comments on commit 67df698

Please sign in to comment.