From bf8d5da3dc90cec4692c434f4bb0b83f969f16b2 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 19 Jan 2019 12:31:55 -0500 Subject: [PATCH] Fix possible memory leak on buffer overflow. (All buffer push functions can panic (longjmp), skipping deinit. Instead, we should use the garbage collected api). --- src/core/corelib.c | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/core/corelib.c b/src/core/corelib.c index 619a0d78..6142ab0f 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -98,43 +98,31 @@ static Janet janet_core_print(int32_t argc, Janet *argv) { } static Janet janet_core_describe(int32_t argc, Janet *argv) { - JanetBuffer b; - janet_buffer_init(&b, 0); + JanetBuffer *b = janet_buffer(0); for (int32_t i = 0; i < argc; ++i) - janet_description_b(&b, argv[i]); - Janet ret = janet_stringv(b.data, b.count); - janet_buffer_deinit(&b); - return ret; + janet_description_b(b, argv[i]); + return janet_stringv(b->data, b->count); } static Janet janet_core_string(int32_t argc, Janet *argv) { - JanetBuffer b; - janet_buffer_init(&b, 0); + JanetBuffer *b = janet_buffer(0); for (int32_t i = 0; i < argc; ++i) - janet_to_string_b(&b, argv[i]); - Janet ret = janet_stringv(b.data, b.count); - janet_buffer_deinit(&b); - return ret; + janet_to_string_b(b, argv[i]); + return janet_stringv(b->data, b->count); } static Janet janet_core_symbol(int32_t argc, Janet *argv) { - JanetBuffer b; - janet_buffer_init(&b, 0); + JanetBuffer *b = janet_buffer(0); for (int32_t i = 0; i < argc; ++i) - janet_to_string_b(&b, argv[i]); - Janet ret = janet_symbolv(b.data, b.count); - janet_buffer_deinit(&b); - return ret; + janet_to_string_b(b, argv[i]); + return janet_symbolv(b->data, b->count); } static Janet janet_core_keyword(int32_t argc, Janet *argv) { - JanetBuffer b; - janet_buffer_init(&b, 0); + JanetBuffer *b = janet_buffer(0); for (int32_t i = 0; i < argc; ++i) - janet_to_string_b(&b, argv[i]); - Janet ret = janet_keywordv(b.data, b.count); - janet_buffer_deinit(&b); - return ret; + janet_to_string_b(b, argv[i]); + return janet_keywordv(b->data, b->count); } static Janet janet_core_buffer(int32_t argc, Janet *argv) {