diff options
author | xengineering <me@xengineering.eu> | 2023-01-09 18:14:39 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-01-09 18:14:39 +0100 |
commit | f42744c9284b8349d4b2b9c6e0d22ac7d36d7f7f (patch) | |
tree | f8f60a6b1424fa30f4ba8a0e775f84c15583df12 | |
parent | b8276ea495a81050e8b749bb1a6bad783dd3606d (diff) | |
download | limox-f42744c9284b8349d4b2b9c6e0d22ac7d36d7f7f.tar limox-f42744c9284b8349d4b2b9c6e0d22ac7d36d7f7f.tar.zst limox-f42744c9284b8349d4b2b9c6e0d22ac7d36d7f7f.zip |
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.
-rw-r--r-- | xmpp.c | 25 |
1 files changed, 23 insertions, 2 deletions
@@ -27,6 +27,7 @@ #include <sys/socket.h> #include <arpa/inet.h> #include <netdb.h> +#include <stdbool.h> /* @@ -135,6 +136,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 * * TODO: Error handling is missing. @@ -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); |