From 67df698201335b02a5a594cc0613841dc100a335 Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Wed, 5 Sep 2018 09:19:04 +0300 Subject: [PATCH] [config] Never return NULL mode setting 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 --- src/usb_moded-config.c | 17 +++++++++++++---- src/usb_moded-dbus.c | 7 ------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/usb_moded-config.c b/src/usb_moded-config.c index 0a699d0..f792f9e 100644 --- a/src/usb_moded-config.c +++ b/src/usb_moded-config.c @@ -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 diff --git a/src/usb_moded-dbus.c b/src/usb_moded-dbus.c index 8241ce7..8c51e41 100644 --- a/src/usb_moded-dbus.c +++ b/src/usb_moded-dbus.c @@ -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);