From 6eed53217d20c7094329e44558d6a6941edde24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 20 Sep 2022 21:50:50 +0800 Subject: [PATCH] FFstrbuf: fix bugs in ffStrbufAppendS; add more tests --- src/util/FFstrbuf.c | 29 +++++++---------------- tests/strbuf.c | 58 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 4748b0c3a..9fad01961 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -31,10 +31,8 @@ void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src) void ffStrbufInitS(FFstrbuf* strbuf, const char* str) { - uint32_t bufSize = (uint32_t)strlen(str) + 1; - ffStrbufInitA(strbuf, FASTFETCH_STRBUF_DEFAULT_ALLOC > bufSize ? FASTFETCH_STRBUF_DEFAULT_ALLOC : bufSize); - memcpy(strbuf->chars, str, bufSize); - strbuf->length = bufSize - 1; + ffStrbufInitA(strbuf, 0); + ffStrbufAppendS(strbuf, str); } uint32_t ffStrbufGetFree(const FFstrbuf* strbuf) @@ -102,14 +100,7 @@ void ffStrbufAppendS(FFstrbuf* strbuf, const char* value) if(value == NULL) return; - for(uint32_t i = 0; value[i] != '\0'; i++) - { - if(i % 16 == 0) - ffStrbufEnsureFree(strbuf, 16); - strbuf->chars[strbuf->length++] = value[i]; - } - - strbuf->chars[strbuf->length] = '\0'; + ffStrbufAppendNS(strbuf, (uint32_t)strlen(value), value); } void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value) @@ -118,15 +109,8 @@ void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value) return; ffStrbufEnsureFree(strbuf, length); - - for(uint32_t i = 0; i < length; i++) - { - if(value[i] == '\0') - break; - - strbuf->chars[strbuf->length++] = value[i]; - } - + memcpy(&strbuf->chars[strbuf->length], value, length); + strbuf->length += length; strbuf->chars[strbuf->length] = '\0'; } @@ -580,5 +564,8 @@ uint16_t ffStrbufToUInt16(const FFstrbuf* strbuf, uint16_t defaultValue) void ffStrbufDestroy(FFstrbuf* strbuf) { + //Avoid free-after-use. These 3 assignments are cheap so don't remove them + strbuf->allocated = strbuf->length = 0; free(strbuf->chars); + strbuf->chars = NULL; } diff --git a/tests/strbuf.c b/tests/strbuf.c index d5401af53..a096498fb 100644 --- a/tests/strbuf.c +++ b/tests/strbuf.c @@ -26,24 +26,58 @@ int main(int argc, char** argv) //initA - ffStrbufInitA(&strbuf, 64); + ffStrbufInitA(&strbuf, 0); - if(strbuf.allocated != 64) - testFailed(&strbuf, "strbuf.allocated != 64"); + if(strbuf.chars[0] != 0) //make sure chars[0] is accessable + testFailed(&strbuf, "strbuf.chars[0] != 0"); + + if(strbuf.allocated != 0) + testFailed(&strbuf, "strbuf.allocated != 0"); if(strbuf.length != 0) - testFailed(&strbuf, "testSrbuf.length != 0"); + testFailed(&strbuf, "strbuf.length != 0"); //appendS - ffStrbufAppendS(&strbuf, "123456789"); + ffStrbufAppendS(&strbuf, "12345"); + + if(strbuf.length != 5) + testFailed(&strbuf, "strbuf.length != 5"); + + if(strbuf.allocated < 6) + testFailed(&strbuf, "strbuf.allocated < 6"); + + if(ffStrbufCompS(&strbuf, "12345") != 0) + testFailed(&strbuf, "strbuf.data != \"12345\""); + + //appendNS + + ffStrbufAppendNS(&strbuf, 4, "67890"); if(strbuf.length != 9) testFailed(&strbuf, "strbuf.length != 9"); - if(strcmp(strbuf.chars, "123456789") != 0) + if(strbuf.allocated < 10) + testFailed(&strbuf, "strbuf.allocated < 10"); + + if(ffStrbufCompS(&strbuf, "123456789") != 0) testFailed(&strbuf, "strbuf.data != \"123456789\""); + //appendS long + + ffStrbufAppendS(&strbuf, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + + if(strbuf.length != 109) + testFailed(&strbuf, "strbuf.length != 109"); + + if(strbuf.allocated < 110) + testFailed(&strbuf, "strbuf.allocated < 110"); + + if(strbuf.chars[strbuf.length] != 0) + testFailed(&strbuf, "strbuf.chars[strbuf.length] != 0"); + + strbuf.length = 9; + //startsWithS if(!ffStrbufStartsWithS(&strbuf, "123")) @@ -79,6 +113,18 @@ int main(int argc, char** argv) if(strcmp(strbuf.chars, "12316") != 0) testFailed(&strbuf, "strbuf.chars != \"12316\""); + //Destroy + + ffStrbufDestroy(&strbuf); + if(strbuf.allocated != 0) + testFailed(&strbuf, "strbuf.allocated != 0"); + + if(strbuf.length != 0) + testFailed(&strbuf, "strbuf.length != 0"); + + if(strbuf.chars != NULL) + testFailed(&strbuf, "strbuf.chars != NULL"); + //Success puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET); }