From f42744c9284b8349d4b2b9c6e0d22ac7d36d7f7f Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 9 Jan 2023 18:14:39 +0100 Subject: Prevent NULL pointer crash It used to be assumed that the user sets the LIMOX_USER and LIMOX_PWD shell variables but there was no error checking if this was not the case. This commit adds the check preventing a crash of the whole program. Furthermore an error message is printed to the terminal. --- xmpp.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/xmpp.c b/xmpp.c index 77567e8..c718df4 100644 --- a/xmpp.c +++ b/xmpp.c @@ -27,6 +27,7 @@ #include #include #include +#include /* @@ -134,6 +135,23 @@ int start_stream(int sock_fd, char *jid, char *domainpart) } } +/* + * Get JID and password and return true on success + */ +bool get_credentials(char **jid, char **pwd) +{ + if (getenv("LIMOX_USER") == NULL || getenv("LIMOX_PWD") == NULL) { + printf("Failed to connect.\n"); + printf("Please set the LIMOX_USER and LIMOX_PWD shell variables.\n"); + return false; + } + + *jid = getenv("LIMOX_USER"); + *pwd = getenv("LIMOX_PWD"); + + return true; +} + /* * Initialize the network connection to the XMPP server and return socket fd * @@ -143,8 +161,11 @@ int xmpp_connect(void) { printf("net_init()\n"); - char *jid = getenv("LIMOX_USER"); - char *pwd = getenv("LIMOX_PWD"); + char *jid; + char *pwd; + if (!get_credentials(&jid, &pwd)) { + return -1; + } printf("Trying to connect as '%s' with '%s'.\n", jid, pwd); char *domain = get_domainpart(jid); -- cgit v1.2.3-70-g09d2