Quellcode durchsuchen

ncd: NCDValCons, NCDValParser: report depth limit errors properly

ambrop7 vor 13 Jahren
Ursprung
Commit
e98f8fa871
3 geänderte Dateien mit 13 neuen und 2 gelöschten Zeilen
  1. 4 2
      ncd/NCDValCons.c
  2. 1 0
      ncd/NCDValCons.h
  3. 8 0
      ncd/NCDValParser.c

+ 4 - 2
ncd/NCDValCons.c

@@ -95,7 +95,8 @@ static int complete_value (NCDValCons *o, NCDValConsVal val, NCDValSafeRef *out,
                 NCDValRef elem = NCDVal_FromSafe(o->mem, o->elems[elemidx].ref);
                 
                 if (!NCDVal_ListAppend(list, elem)) {
-                    goto fail_memory;
+                    *out_error = NCDVALCONS_ERROR_DEPTH;
+                    return 0;
                 }
                 
                 elemidx = o->elems[elemidx].next;
@@ -125,7 +126,8 @@ static int complete_value (NCDValCons *o, NCDValConsVal val, NCDValSafeRef *out,
                 
                 int inserted;
                 if (!NCDVal_MapInsert(map, key, value, &inserted)) {
-                    goto fail_memory;
+                    *out_error = NCDVALCONS_ERROR_DEPTH;
+                    return 0;
                 }
                 if (!inserted) {
                     *out_error = NCDVALCONS_ERROR_DUPLICATE_KEY;

+ 1 - 0
ncd/NCDValCons.h

@@ -76,6 +76,7 @@ typedef struct {
 
 #define NCDVALCONS_ERROR_MEMORY 1
 #define NCDVALCONS_ERROR_DUPLICATE_KEY 2
+#define NCDVALCONS_ERROR_DEPTH 3
 
 /**
  * Initializes a value constructor.

+ 8 - 0
ncd/NCDValParser.c

@@ -53,6 +53,7 @@ struct value {
 #define ERROR_FLAG_TOKENIZATION  (1 << 1)
 #define ERROR_FLAG_SYNTAX        (1 << 2)
 #define ERROR_FLAG_DUPLICATE_KEY (1 << 3)
+#define ERROR_FLAG_DEPTH         (1 << 4)
 
 struct parser_state {
     NCDValCons cons;
@@ -78,6 +79,9 @@ static void handle_cons_error (struct parser_state *state)
         case NCDVALCONS_ERROR_DUPLICATE_KEY:
             state->error_flags |= ERROR_FLAG_DUPLICATE_KEY;
             break;
+        case NCDVALCONS_ERROR_DEPTH:
+            state->error_flags |= ERROR_FLAG_DEPTH;
+            break;
         default:
             ASSERT(0);
     }
@@ -172,6 +176,10 @@ fail:
         BLog(BLOG_ERROR, "line %zu, character %zu: duplicate key in map error", line, line_char);
     }
     
+    if ((state->error_flags & ERROR_FLAG_DEPTH)) {
+        BLog(BLOG_ERROR, "line %zu, character %zu: depth limit exceeded", line, line_char);
+    }
+    
     return 0;
 }