error handing for malloc/fgetc - #25
[smdp.git] / src / parser.c
index c8120cd..e59f1be 100644 (file)
  */
 
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "parser.h"
 
@@ -48,6 +50,11 @@ deck_t *markdown_load(FILE *input) {
     sc++;
 
     while ((c = fgetc(input)) != EOF) {
+        if (ferror(input)) {
+            fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
+            exit(EXIT_FAILURE);
+        }
+
         if(c == '\n') {
 
             // markdown analyse
@@ -266,7 +273,7 @@ deck_t *markdown_load(FILE *input) {
                     }
                     tmp = tmp->next;
                 }
-                
+
                 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
                 }
@@ -337,12 +344,12 @@ int markdown_analyse(cstring_t *text) {
                     case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
                     default: break;
                 }
-                
+
                 break;
             }
         }
     }
-    
+
     if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
        !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
        !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
@@ -518,4 +525,3 @@ int next_blank(cstring_t *text, int i) {
 int next_word(cstring_t *text, int i) {
     return next_nonblank(text, next_blank(text, i));
 }
-