# From: Jordan Metzmeier <jmetzmeier01@gmail.com>
# Subject: Fix segfault when -F is a file that does not exist
# When -F is passed a file that does not exist, jshon was attempting to
# read from a null pointer. This patch checks the pointer to make sure it
# is not null. If it is, it will print an error to stderr and exit
--- a/jshon.c
+++ b/jshon.c
@@ -373,6 +373,10 @@
     FILE* fp;
     char* content;
     fp = fopen(path, "r");
+    if ( !fp ) {
+      fprintf(stderr, "unable to read file %s: %s\n", path, strerror(errno));
+      return NULL;
+    }
     content = read_stream(fp);
     fclose(fp);
     return content;
@@ -918,6 +922,11 @@
         {content = read_file(file_path);}
     else
         {content = read_stdin();}
+    if (!content) {
+      fprintf(stderr, "error: failed to read input\n");
+      exit(1);
+    }
+
     if (!content[0] && !quiet)
         {fprintf(stderr, "warning: nothing to read\n");}
 
