# HG changeset patch # User Mike Becker # Date 1705234392 -3600 # Node ID 26500fc240582bf3a830a2a01cc9dd898184b988 # Parent 5136f2fc32ecdd5e492285bd6da152a1c0996de5 add constant for reading out printf sbo size - relates to #343 diff -r 5136f2fc32ec -r 26500fc24058 src/cx/printf.h --- a/src/cx/printf.h Sat Jan 13 17:51:42 2024 +0100 +++ b/src/cx/printf.h Sun Jan 14 13:13:12 2024 +0100 @@ -44,6 +44,12 @@ extern "C" { #endif + +/** + * The maximum string length that fits into stack memory. + */ +extern unsigned const cx_printf_sbo_size; + /** * A \c fprintf like function which writes the output to a stream by * using a write_func. diff -r 5136f2fc32ec -r 26500fc24058 src/printf.c --- a/src/printf.c Sat Jan 13 17:51:42 2024 +0100 +++ b/src/printf.c Sun Jan 14 13:13:12 2024 +0100 @@ -34,6 +34,7 @@ #ifndef CX_PRINTF_SBO_SIZE #define CX_PRINTF_SBO_SIZE 512 #endif +unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE; int cx_fprintf( void *stream, @@ -85,9 +86,8 @@ ... ) { va_list ap; - cxmutstr ret; va_start(ap, fmt); - ret = cx_vasprintf_a(allocator, fmt, ap); + cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap); va_end(ap); return ret; } diff -r 5136f2fc32ec -r 26500fc24058 tests/test_printf.c --- a/tests/test_printf.c Sat Jan 13 17:51:42 2024 +0100 +++ b/tests/test_printf.c Sun Jan 14 13:13:12 2024 +0100 @@ -64,23 +64,25 @@ } CX_TEST(test_bprintf_large_string) { + unsigned len = cx_printf_sbo_size; CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); CxAllocator *alloc = &talloc.base; - char *aaa = malloc(512); - char *bbb = malloc(512); - char *expected = malloc(1040); - memset(aaa, 'a', 511); - aaa[511] = 0; - memset(bbb, 'b', 511); - bbb[511] = 0; + char *aaa = malloc(len); + char *bbb = malloc(len); + char *expected = malloc(2*len+16); + memset(aaa, 'a', len-1); + aaa[len-1] = 0; + memset(bbb, 'b', len-1); + bbb[len-1] = 0; sprintf(expected, "After %s comes %s.", aaa, bbb); CX_TEST_DO { CxBuffer buf; cxBufferInit(&buf, NULL, 64, alloc, CX_BUFFER_AUTO_EXTEND); size_t r = cx_bprintf(&buf, "After %s comes %s.", aaa, bbb); - CX_TEST_ASSERT(r == 1036); - CX_TEST_ASSERT(buf.size == 1036); + size_t er = 2*len-2+14; + CX_TEST_ASSERT(r == er); + CX_TEST_ASSERT(buf.size == er); cxBufferPut(&buf, 0); CX_TEST_ASSERT(0 == strcmp(expected, buf.space)); cxBufferDestroy(&buf); @@ -271,17 +273,18 @@ } CX_TEST(test_asprintf_large_string) { - char *aaa = malloc(512); - char *bbb = malloc(512); - char *expected = malloc(1040); - memset(aaa, 'a', 511); - aaa[511] = 0; - memset(bbb, 'b', 511); - bbb[511] = 0; + unsigned len = cx_printf_sbo_size; + char *aaa = malloc(len); + char *bbb = malloc(len); + char *expected = malloc(2*len+16); + memset(aaa, 'a', len-1); + aaa[len-1] = 0; + memset(bbb, 'b', len-1); + bbb[len-1] = 0; sprintf(expected, "After %s comes %s.", aaa, bbb); CX_TEST_DO { cxmutstr r = cx_asprintf("After %s comes %s.", aaa, bbb); - CX_TEST_ASSERT(r.length == 1036); + CX_TEST_ASSERT(r.length == 2*len-2+14); ASSERT_ZERO_TERMINATED(r); CX_TEST_ASSERT(0 == strcmp(r.ptr, expected)); cx_strfree(&r);