From 6e7d4efab4e3b35a68dfd5cfbeda6373b86520ff Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 21 Apr 2026 21:34:21 +0300 Subject: fix(String): Handle out of memory condition --- String.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/String.c b/String.c index 31b5139..602d854 100644 --- a/String.c +++ b/String.c @@ -26,6 +26,8 @@ S_new(BumpArena b, const char* str) { }; void* start = B_alloc(b, sizeof(sm) + sm.length + 1); + if (NULL == start) + return NULL; memcpy(start, &sm, sizeof(sm)); start += sizeof(sm); @@ -45,6 +47,8 @@ S_newlen(BumpArena b, usize length) { }; void* start = B_alloc(b, sizeof(sm) + sm.length + 1); + if (NULL == start) + return NULL; memcpy(start, &sm, sizeof(sm)); start += sizeof(sm); @@ -66,6 +70,9 @@ S_dup(BumpArena b, String s) { usize allocated = S_allocated(s); void* start = B_alloc(b, allocated); + if (NULL == start) + return NULL; + memcpy(start, s - sizeof(struct StringMeta), allocated); return NULL; @@ -80,7 +87,7 @@ S_length(String s) { Result S_append(String s, const char* str) { - if (NULL == s) + if (NULL == s || NULL == str) return R_Uninitialized; struct StringMeta* sm = S_metadata(s); @@ -142,6 +149,9 @@ S_vconcat(BumpArena b, va_list args) { String result = S_newlen(b, length); + if (NULL == result) + return result; + if (0 < length) { for (const char* str = va_arg(args, const char*); str != NULL; str = va_arg(args, const char*)) strncat(result, str, length); @@ -169,8 +179,10 @@ S_vprintf(BumpArena b, const char* fmt, va_list args) { va_end(temp); String result = S_newlen(b, length); - vsnprintf(result, length + 1, fmt, args); + if (NULL == result) + return NULL; + vsnprintf(result, length + 1, fmt, args); return result; } -- cgit v1.2.3