From 6e2a60629c68757b0997d38c2d34d8b622ac3041 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Thu, 30 May 2019 11:47:56 -0400 Subject: [PATCH] Make ts_tree_delete and ts_parser_delete NULL-safe. Historically, `free(3)`-style functions have been NULL-safe, to make resource handling functions more robust. This doesn't seem to be the case with tree-sitter's `ts_tree_delete` and `ts_parser_delete` C functions, which immediately dereference the passed pointer. This leads to complexity in client libraries that have to ensure that trees and parsers are cleaned up correctly. This patch adds NULL checks to `parse_delete` and `tree_delete`. They should have negligable performance impacts, since null checks are fast. I didn't change the internal _delete functions, as arguably those being NULL-unsafe is a feature, not a bug. --- lib/src/parser.c | 2 ++ lib/src/tree.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/src/parser.c b/lib/src/parser.c index efb10123..7a30c204 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -1525,6 +1525,8 @@ TSParser *ts_parser_new(void) { } void ts_parser_delete(TSParser *self) { + if (!self) return; + ts_stack_delete(self->stack); if (self->reduce_actions.contents) { array_delete(&self->reduce_actions); diff --git a/lib/src/tree.c b/lib/src/tree.c index 9f294412..04cb1d24 100644 --- a/lib/src/tree.c +++ b/lib/src/tree.c @@ -29,6 +29,8 @@ TSTree *ts_tree_copy(const TSTree *self) { } void ts_tree_delete(TSTree *self) { + if (!self) return; + SubtreePool pool = ts_subtree_pool_new(0); ts_subtree_release(&pool, self->root); ts_subtree_pool_delete(&pool);