From 52a55685ee297227d59bd5f3670eec0b91a05cc7 Mon Sep 17 00:00:00 2001 From: Ryooooooga Date: Sat, 16 Dec 2023 19:36:21 +0900 Subject: [PATCH] feat: avoid using `vasprintf` --- cute.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/cute.h b/cute.h index f64832f..d985735 100644 --- a/cute.h +++ b/cute.h @@ -1,10 +1,6 @@ #ifndef INCLUDE_CUTE_H #define INCLUDE_CUTE_H -#ifndef __STDC_WANT_LIB_EXT2__ -# define __STDC_WANT_LIB_EXT2__ 1 -#endif // __STDC_WANT_LIB_EXT2__ - #include #include #include @@ -451,8 +447,16 @@ static inline cute_result_t *cute_result_new_subtest_v(cute_result_t *parent, co r->_.subtest.ok = -1; if (format != NULL) { - if (vasprintf(&r->message, format, args) == -1) { - r->message = NULL; + va_list args_copy; + va_copy(args_copy, args); + const int size = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); + + if (size >= 0) { + r->message = (char *)malloc(size + 1); + if (r->message != NULL) { + vsnprintf(r->message, size + 1, format, args); + } } } @@ -471,8 +475,16 @@ static inline cute_result_t *cute_result_new_assert_v(cute_result_t *parent, con r->_.assert.ok = ok; if (format != NULL) { - if (vasprintf(&r->message, format, args) == -1) { - r->message = NULL; + va_list args_copy; + va_copy(args_copy, args); + const int size = vsnprintf(NULL, 0, format, args_copy); + va_end(args_copy); + + if (size >= 0) { + r->message = (char *)malloc(size + 1); + if (r->message != NULL) { + vsnprintf(r->message, size + 1, format, args); + } } }