Skip to content

Memory leak when parsing failed #22

@grievejia

Description

@grievejia

Here's the input that will trigger the issue:
leak.txt
Feed it into the toml_parse() function on master(03e8a3a) built with AddressSanitizer will crash the program:

> ./parse leak.txt
not in start, line 1

=================================================================
==17347==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x4d63d0 in __interceptor_malloc /home/grieve/LLVM/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:88
    #1 0x5423eb in make_stack_item (/home/grieve/scratch/libtoml/parse+0x5423eb)
    #2 0x526368 in toml_parse (/home/grieve/scratch/libtoml/parse+0x526368)
    #3 0x50dc77 in main (/home/grieve/scratch/libtoml/parse+0x50dc77)
    #4 0x7f53e0b764c9 in __libc_start_main (/usr/lib/libc.so.6+0x204c9)

SUMMARY: AddressSanitizer: 32 byte(s) leaked in 1 allocation(s).

Here's the full source code of parse.c I used in testing:

#include "toml.h"
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv) {
  if (argc > 1) {
    const char* filename = argv[1];
    FILE* file = fopen(filename, "rb");
    if (file != NULL) {
      fseek(file, 0, SEEK_END);
      long file_size = ftell(file);
      rewind(file);

      char* buf = (char*)malloc(file_size + 1);
      if (buf != NULL) {
        fread(buf, file_size, 1, file);
        buf[file_size] = 0;

        struct toml_node* root;
        toml_init(&root);
        toml_parse(root, buf, file_size);
        toml_free(root);

        free(buf);
      }
      fclose(file);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions