From 01016a318bfc616c0049eacef13f5a2725b3a452 Mon Sep 17 00:00:00 2001 From: nullgemm Date: Mon, 9 Jul 2018 19:23:03 +0200 Subject: [PATCH] fixed segfault at startup --- src/desktop.c | 29 +++++++++++++++++++++++++---- src/main.c | 5 +++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/desktop.c b/src/desktop.c index 43ffa2c..c6a7ef5 100644 --- a/src/desktop.c +++ b/src/desktop.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "lang.h" #include "config.h" @@ -12,7 +13,7 @@ #define LY_XSESSION_NAME "Name=" // searches a folder -void get_desktops(char* sessions_dir, struct delist_t* list, int* remote_count, short x) +short get_desktops(char* sessions_dir, struct delist_t* list, int* remote_count, short x) { /* xsession */ FILE* file; @@ -24,6 +25,11 @@ void get_desktops(char* sessions_dir, struct delist_t* list, int* remote_count, char* command; int count = *remote_count; + if(access(sessions_dir, F_OK) == -1 ) + { + return -1; + } + /* reads xorg's desktop environments entries */ dir = opendir(sessions_dir); @@ -32,7 +38,7 @@ void get_desktops(char* sessions_dir, struct delist_t* list, int* remote_count, { error_print(LY_ERR_DELIST); end_list(list, count); - return; + return -2; } /* cycles through the folder */ @@ -83,6 +89,8 @@ void get_desktops(char* sessions_dir, struct delist_t* list, int* remote_count, closedir(dir); *remote_count = count; + + return 0; } /* returns a list containing all the DE for all the display servers */ @@ -90,10 +98,23 @@ struct delist_t* list_de(void) { /* de list */ int count = 2; + short errors = 0; struct delist_t* list = init_list(count); - get_desktops(LY_PATH_XSESSIONS, list, &count, true); - get_desktops(LY_PATH_WSESSIONS, list, &count, false); + if(get_desktops(LY_PATH_XSESSIONS, list, &count, true) != 0) + { + ++errors; + } + + if(get_desktops(LY_PATH_WSESSIONS, list, &count, false)) + { + ++errors; + } + + if(errors > 1) { + return NULL; + } + end_list(list, count); return list; } diff --git a/src/main.c b/src/main.c index 03ce1c1..5a14fc7 100644 --- a/src/main.c +++ b/src/main.c @@ -48,6 +48,11 @@ int main(void) char* cmd; /* gets desktop entries */ de_list = list_de(); + + if(de_list == NULL) { + return EXIT_FAILURE; + } + de_props = de_list->props; de_names = de_list->names; de_count = de_list->count;