From bca38d6c015727c66248cd59908591806165ac83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Wed, 18 Sep 2019 00:20:38 +0800 Subject: Fix crash when not all properties filled By properly initialising everything and checking before freeing --- main.c | 33 ++++++++++++++++----- rtclient/user.h | 7 +++-- user.c | 91 ++++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 96 insertions(+), 35 deletions(-) diff --git a/main.c b/main.c index 19e43c2..b2545b6 100644 --- a/main.c +++ b/main.c @@ -32,14 +32,31 @@ int main(void) rtclient_user_show(&user, name); if (user) { - printf("id: %s\npassword: %s\nname: %s\nemailaddress: %s\nrealname: %s\nnickname: %s\ngecos: %s\norganization: %s\naddress1: %s\naddress2: %s\ncity: %s\nstate: %s\nzip: %s\ncountry: %s\nhomephone: %s\nworkphone: %s\nmobilephone: %s\npagerphone: %s\ncontactinfo: %s\ncomments: %s\nsignature: %s\nlang: %s\nprivileged: %d\ndisabled: %d\n" - , user->id, user->password, user->name, user->emailaddress - , user->realname, user->nickname, user->gecos - , user->organization, user->address1, user->address2 - , user->city, user->state, user->zip, user->country - , user->homephone, user->workphone, user->mobilephone - , user->pagerphone, user->contactinfo, user->comments - , user->signature, user->lang, user->privileged + printf("id: %s\nname: %s\npassword: %s\nemailaddress: %s\nrealname: %s\nnickname: %s\norganization: %s\naddress1: %s\naddress2: %s\ncity: %s\nstate: %s\nzip: %s\ncountry: %s\nhomephone: %s\nworkphone: %s\nmobilephone: %s\npagerphone: %s\ncontactinfo: %s\ncomments: %s\nsignature: %s\ngecos: %s\nlang: %d\ntimezone: %d\nprivileged: %d\ndisabled: %d\n" + , user->id + , user->name + , user->password + , user->emailaddress + , user->realname + , user->nickname + , user->organization + , user->address1 + , user->address2 + , user->city + , user->state + , user->zip + , user->country + , user->homephone + , user->workphone + , user->mobilephone + , user->pagerphone + , user->contactinfo + , user->comments + , user->signature + , user->gecos + , user->lang + , user->timezone + , user->privileged , user->disabled); rtclient_user_free(user); diff --git a/rtclient/user.h b/rtclient/user.h index 7c457f0..9511f9f 100644 --- a/rtclient/user.h +++ b/rtclient/user.h @@ -412,12 +412,11 @@ enum rt_timezone { struct rt_user { char *id; - char *password; char *name; + char *password; char *emailaddress; char *realname; char *nickname; - char *gecos; char *organization; char *address1; char *address2; @@ -432,7 +431,9 @@ struct rt_user { char *contactinfo; char *comments; char *signature; - char lang[2]; + char *gecos; + enum rt_lang lang; + enum rt_timezone timezone; bool privileged; bool disabled; }; diff --git a/user.c b/user.c index e1ac41c..cef4411 100644 --- a/user.c +++ b/user.c @@ -14,6 +14,31 @@ static size_t show_callback(void *contents, size_t size, size_t nmemb rt_user **userptr = (rt_user **)writedata; rt_user *user = *userptr; + user->id = NULL; + user->name = NULL; + user->password = NULL; + user->emailaddress = NULL; + user->realname = NULL; + user->nickname = NULL; + user->organization = NULL; + user->address1 = NULL; + user->address2 = NULL; + user->city = NULL; + user->state = NULL; + user->zip = NULL; + user->country = NULL; + user->homephone = NULL; + user->workphone = NULL; + user->mobilephone = NULL; + user->pagerphone = NULL; + user->contactinfo = NULL; + user->comments = NULL; + user->signature = NULL; + user->gecos = NULL; + user->lang = RT_LANG_NONE; + user->timezone = RT_TIMEZONE_NONE; + user->privileged = false; + user->disabled = true; char *linesaveptr = NULL; char *line = strtok_r(response, "\n", &linesaveptr); @@ -106,9 +131,6 @@ static size_t show_callback(void *contents, size_t size, size_t nmemb token = strtok_r(NULL, ":", &tokensaveptr); user->signature = malloc(strlen(token)); strcpy(user->signature, ++token); - } else if (!strcmp(token, "Lang")) { - token = strtok_r(NULL, ":", &tokensaveptr); - strncpy(user->lang, ++token, 2); } else if (!strcmp(token, "Privileged")) { token = strtok_r(NULL, ":", &tokensaveptr); user->privileged = (bool)atoi(++token); @@ -259,27 +281,48 @@ void rtclient_user_new(const char *name void rtclient_user_free(rt_user *user) { - free(user->id); - free(user->password); - free(user->name); - free(user->emailaddress); - free(user->realname); - free(user->nickname); - free(user->gecos); - free(user->organization); - free(user->address1); - free(user->address2); - free(user->city); - free(user->state); - free(user->zip); - free(user->country); - free(user->homephone); - free(user->workphone); - free(user->mobilephone); - free(user->pagerphone); - free(user->contactinfo); - free(user->comments); - free(user->signature); + if (user->id) + free(user->id); + if (user->name) + free(user->name); + if (user->password) + free(user->password); + if (user->emailaddress) + free(user->emailaddress); + if (user->realname) + free(user->realname); + if (user->nickname) + free(user->nickname); + if (user->organization) + free(user->organization); + if (user->address1) + free(user->address1); + if (user->address2) + free(user->address2); + if (user->city) + free(user->city); + if (user->state) + free(user->state); + if (user->zip) + free(user->zip); + if (user->country) + free(user->country); + if (user->homephone) + free(user->homephone); + if (user->workphone) + free(user->workphone); + if (user->mobilephone) + free(user->mobilephone); + if (user->pagerphone) + free(user->pagerphone); + if (user->contactinfo) + free(user->contactinfo); + if (user->comments) + free(user->comments); + if (user->signature) + free(user->signature); + if (user->gecos) + free(user->gecos); free(user); user = NULL; } -- cgit v1.2.3