File: fix_segfault_nonexistent_file

package info (click to toggle)
jshon 20131010-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 180 kB
  • ctags: 152
  • sloc: ansic: 1,706; makefile: 54
file content (30 lines) | stat: -rw-r--r-- 935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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");}