Commits

Kirill Simonov committed c58d94d

Fix some leaks, segfaults and warnings.

Comments (0)

Files changed (6)

         case YAML_SCALAR_TOKEN:
             yaml_free(token->data.scalar.value);
             break;
+
+        default:
+            break;
     }
 
     memset(token, 0, sizeof(yaml_token_t));
             yaml_free(event->data.mapping_start.anchor);
             yaml_free(event->data.mapping_start.tag);
             break;
+
+        default:
+            break;
     }
 
     memset(event, 0, sizeof(yaml_event_t));
         default:
             assert(1);      /* Invalid state. */
     }
+
+    return 0;
 }
 
 /*
     if (tag_directives_start_ref) {
         if (STACK_EMPTY(parser, tag_directives)) {
             *tag_directives_start_ref = *tag_directives_end_ref = NULL;
+            STACK_DEL(parser, tag_directives);
         }
         else {
             *tag_directives_start_ref = tag_directives.start;
-            *tag_directives_end_ref = tag_directives.end;
+            *tag_directives_end_ref = tag_directives.top;
         }
     }
+    else {
+        STACK_DEL(parser, tag_directives);
+    }
 
     return 1;
 
 {
     yaml_tag_directive_t *tag_directive;
     yaml_tag_directive_t copy = { NULL, NULL };
-    int length;
 
     for (tag_directive = parser->tag_directives.start;
             tag_directive != parser->tag_directives.top; tag_directive ++) {
 
         while (parser->raw_buffer.pointer != parser->raw_buffer.last)
         {
-            unsigned int value, value2;
+            unsigned int value = 0, value2 = 0;
             int incomplete = 0;
             unsigned char octet;
-            unsigned int width;
+            unsigned int width = 0;
             int k, low, high;
             int raw_unread = parser->raw_buffer.last - parser->raw_buffer.pointer;
 
 
                         /* Get the next character. */
 
-                        unsigned int value2 = parser->raw_buffer.pointer[low+2]
+                        value2 = parser->raw_buffer.pointer[low+2]
                             + (parser->raw_buffer.pointer[high+2] << 8);
 
                         /* Check for a low surrogate area. */
                     }
 
                     break;
+
+                default:
+                    assert(1);      /* Impossible. */
             }
 
             /* Check if the raw buffer contains enough bytes to form a character. */
     if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
     if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
 
-    start_mark = parser->mark;
+    start_mark = end_mark = parser->mark;
 
     /* Consume the content of the plain scalar. */
 

tests/run-parser.c

 int
 main(int argc, char *argv[])
 {
-    FILE *file;
-    yaml_parser_t parser;
-    yaml_event_t event;
-    int done = 0;
-    int count = 0;
+    int number;
 
-    if (argc != 2) {
-        printf("Usage: %s file.yaml\n", argv[0]);
+    if (argc < 2) {
+        printf("Usage: %s file1.yaml ...\n", argv[0]);
         return 0;
     }
-    file = fopen(argv[1], "rb");
-    assert(file);
 
-    assert(yaml_parser_initialize(&parser));
+    for (number = 1; number < argc; number ++)
+    {
+        FILE *file;
+        yaml_parser_t parser;
+        yaml_event_t event;
+        int done = 0;
+        int count = 0;
+        int error = 0;
 
-    yaml_parser_set_input_file(&parser, file);
+        printf("[%d] Parsing '%s': ", number, argv[number]);
+        fflush(stdout);
 
-    while (!done)
-    {
-        assert(yaml_parser_parse(&parser, &event));
+        file = fopen(argv[number], "rb");
+        assert(file);
 
-        done = (event.type == YAML_STREAM_END_EVENT);
+        assert(yaml_parser_initialize(&parser));
 
-        yaml_event_delete(&event);
+        yaml_parser_set_input_file(&parser, file);
 
-        count ++;
+        while (!done)
+        {
+            if (!yaml_parser_parse(&parser, &event)) {
+                error = 1;
+                break;
+            }
+
+            done = (event.type == YAML_STREAM_END_EVENT);
+
+            yaml_event_delete(&event);
+
+            count ++;
+        }
+
+        yaml_parser_delete(&parser);
+
+        assert(!fclose(file));
+
+        printf("%s (%d events)\n", (error ? "FAILURE" : "SUCCESS"), count);
     }
 
-    yaml_parser_delete(&parser);
-
-    fclose(file);
-
-    printf("Parsing the file '%s': %d events\n", argv[1], count);
-
     return 0;
 }
 

tests/run-scanner.c

 int
 main(int argc, char *argv[])
 {
-    FILE *file;
-    yaml_parser_t parser;
-    yaml_token_t token;
-    int done = 0;
-    int count = 0;
+    int number;
 
-    if (argc != 2) {
-        printf("Usage: %s file.yaml\n", argv[0]);
+    if (argc < 2) {
+        printf("Usage: %s file1.yaml ...\n", argv[0]);
         return 0;
     }
-    file = fopen(argv[1], "rb");
-    assert(file);
 
-    assert(yaml_parser_initialize(&parser));
+    for (number = 1; number < argc; number ++)
+    {
+        FILE *file;
+        yaml_parser_t parser;
+        yaml_token_t token;
+        int done = 0;
+        int count = 0;
+        int error = 0;
 
-    yaml_parser_set_input_file(&parser, file);
+        printf("[%d] Scanning '%s': ", number, argv[number]);
+        fflush(stdout);
 
-    while (!done)
-    {
-        assert(yaml_parser_scan(&parser, &token));
+        file = fopen(argv[number], "rb");
+        assert(file);
 
-        done = (token.type == YAML_STREAM_END_TOKEN);
+        assert(yaml_parser_initialize(&parser));
 
-        yaml_token_delete(&token);
+        yaml_parser_set_input_file(&parser, file);
 
-        count ++;
+        while (!done)
+        {
+            if (!yaml_parser_scan(&parser, &token)) {
+                error = 1;
+                break;
+            }
+
+            done = (token.type == YAML_STREAM_END_TOKEN);
+
+            yaml_token_delete(&token);
+
+            count ++;
+        }
+
+        yaml_parser_delete(&parser);
+
+        assert(!fclose(file));
+
+        printf("%s (%d tokens)\n", (error ? "FAILURE" : "SUCCESS"), count);
     }
 
-    yaml_parser_delete(&parser);
-
-    fclose(file);
-
-    printf("Parsing the file '%s': %d tokens\n", argv[1], count);
-
     return 0;
 }