Skip to content

Commit 936cc84

Browse files
authored
Merge pull request #102 from amaanq/fix-leaks
Fix memory bugs
2 parents 231f316 + 1fd87e0 commit 936cc84

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

tree-sitter-markdown/src/scanner.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ const bool paragraph_interrupt_symbols[] = {
159159
false, // NO_INDENTED_CHUNK,
160160
false, // ERROR,
161161
false, // TRIGGER_ERROR,
162+
false, // EOF,
163+
false, // MINUS_METADATA,
164+
false, // PLUS_METADATA,
162165
true, // PIPE_TABLE_START,
163166
false, // PIPE_TABLE_LINE_ENDING,
164167
};
@@ -257,7 +260,6 @@ static unsigned serialize(Scanner *s, char *buffer) {
257260
static void deserialize(Scanner *s, const char *buffer, unsigned length) {
258261
s->open_blocks.size = 0;
259262
s->open_blocks.capacity = 0;
260-
s->open_blocks.items = NULL;
261263
s->state = 0;
262264
s->matched = 0;
263265
s->indentation = 0;
@@ -274,7 +276,6 @@ static void deserialize(Scanner *s, const char *buffer, unsigned length) {
274276
if (blocks_size > 0) {
275277
size_t blocks_count = blocks_size / sizeof(Block);
276278
s->open_blocks.capacity = roundup_32(blocks_count);
277-
s->open_blocks.items = (Block*) malloc(sizeof(Block) * s->open_blocks.capacity);
278279
memcpy(s->open_blocks.items, &buffer[i], blocks_size);
279280
s->open_blocks.size = blocks_count;
280281
}
@@ -1451,6 +1452,7 @@ static bool scan(Scanner *s, TSLexer *lexer, const bool *valid_symbols) {
14511452

14521453
void *tree_sitter_markdown_external_scanner_create() {
14531454
Scanner *s = (Scanner *)malloc(sizeof(Scanner));
1455+
s->open_blocks.items = (Block *)calloc(1, sizeof(Block));
14541456

14551457
assert(ATX_H6_MARKER == ATX_H1_MARKER + 5);
14561458
deserialize(s, NULL, 0);
@@ -1487,5 +1489,6 @@ void tree_sitter_markdown_external_scanner_deserialize(
14871489

14881490
void tree_sitter_markdown_external_scanner_destroy(void *payload) {
14891491
Scanner *scanner = (Scanner *)payload;
1492+
free(scanner->open_blocks.items);
14901493
free(scanner);
14911494
}

0 commit comments

Comments
 (0)