From 0b696fc36136c3b9705f34630c072c4344a5c7c6 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sun, 14 Aug 2022 14:10:56 +0900 Subject: [PATCH] StringBuilders should be cleared when finished or discarded --- src/obj_str.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/obj_str.c b/src/obj_str.c index 8b1a623..7478006 100644 --- a/src/obj_str.c +++ b/src/obj_str.c @@ -1059,20 +1059,26 @@ void krk_pushStringBuilderStr(struct StringBuilder * sb, const char *str, size_t } } +static void _freeStringBuilder(struct StringBuilder * sb) { + FREE_ARRAY(char,sb->bytes, sb->capacity); + sb->bytes = NULL; + sb->length = 0; + sb->capacity = 0; +} KrkValue krk_finishStringBuilder(struct StringBuilder * sb) { KrkValue out = OBJECT_VAL(krk_copyString(sb->bytes, sb->length)); - FREE_ARRAY(char,sb->bytes, sb->capacity); + _freeStringBuilder(sb); return out; } KrkValue krk_finishStringBuilderBytes(struct StringBuilder * sb) { KrkValue out = OBJECT_VAL(krk_newBytes(sb->length, (uint8_t*)sb->bytes)); - FREE_ARRAY(char,sb->bytes, sb->capacity); + _freeStringBuilder(sb); return out; } KrkValue krk_discardStringBuilder(struct StringBuilder * sb) { - FREE_ARRAY(char,sb->bytes, sb->capacity); + _freeStringBuilder(sb); return NONE_VAL(); }