Commits

ripencc committed 099ed83

don't segfault when bad input file given

  • Participants
  • Parent commits 64def4a

Comments (0)

Files changed (2)

     setbuffer(stdout, buffer, sizeof buffer);
     
     BGPDUMP *my_dump = bgpdump_open_dump(argv[0]);
+    if(! my_dump)
+        return 1;
     
     do {
         BGPDUMP_ENTRY *my_entry = bgpdump_read_next(my_dump);

File bgpdump_lib.c

 
 
 BGPDUMP *bgpdump_open_dump(const char *filename) {
-    BGPDUMP *this_dump=NULL;
-    CFRFILE *f;
 
-    this_dump = malloc(sizeof(BGPDUMP));
-
-    f = cfr_open(filename);
-    if((filename == NULL) || (strcmp(filename, "-") == 0)) {
-	strcpy(this_dump->filename, "[STDIN]");
-    } else
-    {
+    CFRFILE *f = cfr_open(filename);
+    if(! f) {
+        perror("can't open dumpfile");
+        return NULL;
+    }
+    
+    BGPDUMP *this_dump = malloc(sizeof(BGPDUMP));
+    strcpy(this_dump->filename, "[STDIN]");
+    if(filename && strcmp(filename, "-")) {
 	strcpy(this_dump->filename, filename);
     }
 
-    if(f == NULL) {
-	free(this_dump);
-	return NULL;
-    }
-
     this_dump->f = f;
     this_dump->eof=0;
     this_dump->parsed = 0;
 }
 
 BGPDUMP_ENTRY*	bgpdump_read_next(BGPDUMP *dump) {
+    assert(dump);
+
     BGPDUMP_ENTRY *this_entry=NULL;
     struct mstream s;
     u_char *buffer;