From 7989bf927839b42eee0616b57abb7a7405c6b36c Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 27 Nov 2019 13:26:07 -0500 Subject: [PATCH] Fix segfault when missing host on add subcommand --- main.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 88e9abf..948d4d0 100644 --- a/main.c +++ b/main.c @@ -14,7 +14,7 @@ const char *blockString = "0.0.0.0 "; FILE *fopenHostsFile(int mode) { - switch(mode) + switch(mode) { case 0: return fopen("/etc/hosts", "r"); @@ -89,22 +89,21 @@ void readToHost(char *host, FILE *hostsFile) */ int main(int argc, char **argv) { - FILE *hostsFile = fopenHostsFile(0); - - readToHost("reddit.com", hostsFile); - - fclose(hostsFile); - hostsFile = NULL; - + FILE *hostsFile; if (getuid() != 0) { fprintf(stderr, "hb: Must run as root using sudo!\n"); } + printf("Argc: %d\n", argc); for (int i = 0; i < argc; i++) { if (strcmp(argv[i], "add") == 0) { + if (argc < 3) { + printf("Please provide a host!\n"); + exit(1); + } hostsFile = fopenHostsFile(1); blockHost(hostsFile, argv[i+1]); }