Skip to content

Commit 6eed532

Browse files
committed
FFstrbuf: fix bugs in ffStrbufAppendS; add more tests
1 parent 254c4e5 commit 6eed532

File tree

2 files changed

+60
-27
lines changed

2 files changed

+60
-27
lines changed

src/util/FFstrbuf.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src)
3131

3232
void ffStrbufInitS(FFstrbuf* strbuf, const char* str)
3333
{
34-
uint32_t bufSize = (uint32_t)strlen(str) + 1;
35-
ffStrbufInitA(strbuf, FASTFETCH_STRBUF_DEFAULT_ALLOC > bufSize ? FASTFETCH_STRBUF_DEFAULT_ALLOC : bufSize);
36-
memcpy(strbuf->chars, str, bufSize);
37-
strbuf->length = bufSize - 1;
34+
ffStrbufInitA(strbuf, 0);
35+
ffStrbufAppendS(strbuf, str);
3836
}
3937

4038
uint32_t ffStrbufGetFree(const FFstrbuf* strbuf)
@@ -102,14 +100,7 @@ void ffStrbufAppendS(FFstrbuf* strbuf, const char* value)
102100
if(value == NULL)
103101
return;
104102

105-
for(uint32_t i = 0; value[i] != '\0'; i++)
106-
{
107-
if(i % 16 == 0)
108-
ffStrbufEnsureFree(strbuf, 16);
109-
strbuf->chars[strbuf->length++] = value[i];
110-
}
111-
112-
strbuf->chars[strbuf->length] = '\0';
103+
ffStrbufAppendNS(strbuf, (uint32_t)strlen(value), value);
113104
}
114105

115106
void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value)
@@ -118,15 +109,8 @@ void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value)
118109
return;
119110

120111
ffStrbufEnsureFree(strbuf, length);
121-
122-
for(uint32_t i = 0; i < length; i++)
123-
{
124-
if(value[i] == '\0')
125-
break;
126-
127-
strbuf->chars[strbuf->length++] = value[i];
128-
}
129-
112+
memcpy(&strbuf->chars[strbuf->length], value, length);
113+
strbuf->length += length;
130114
strbuf->chars[strbuf->length] = '\0';
131115
}
132116

@@ -580,5 +564,8 @@ uint16_t ffStrbufToUInt16(const FFstrbuf* strbuf, uint16_t defaultValue)
580564

581565
void ffStrbufDestroy(FFstrbuf* strbuf)
582566
{
567+
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
568+
strbuf->allocated = strbuf->length = 0;
583569
free(strbuf->chars);
570+
strbuf->chars = NULL;
584571
}

tests/strbuf.c

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,58 @@ int main(int argc, char** argv)
2626

2727
//initA
2828

29-
ffStrbufInitA(&strbuf, 64);
29+
ffStrbufInitA(&strbuf, 0);
3030

31-
if(strbuf.allocated != 64)
32-
testFailed(&strbuf, "strbuf.allocated != 64");
31+
if(strbuf.chars[0] != 0) //make sure chars[0] is accessable
32+
testFailed(&strbuf, "strbuf.chars[0] != 0");
33+
34+
if(strbuf.allocated != 0)
35+
testFailed(&strbuf, "strbuf.allocated != 0");
3336

3437
if(strbuf.length != 0)
35-
testFailed(&strbuf, "testSrbuf.length != 0");
38+
testFailed(&strbuf, "strbuf.length != 0");
3639

3740
//appendS
3841

39-
ffStrbufAppendS(&strbuf, "123456789");
42+
ffStrbufAppendS(&strbuf, "12345");
43+
44+
if(strbuf.length != 5)
45+
testFailed(&strbuf, "strbuf.length != 5");
46+
47+
if(strbuf.allocated < 6)
48+
testFailed(&strbuf, "strbuf.allocated < 6");
49+
50+
if(ffStrbufCompS(&strbuf, "12345") != 0)
51+
testFailed(&strbuf, "strbuf.data != \"12345\"");
52+
53+
//appendNS
54+
55+
ffStrbufAppendNS(&strbuf, 4, "67890");
4056

4157
if(strbuf.length != 9)
4258
testFailed(&strbuf, "strbuf.length != 9");
4359

44-
if(strcmp(strbuf.chars, "123456789") != 0)
60+
if(strbuf.allocated < 10)
61+
testFailed(&strbuf, "strbuf.allocated < 10");
62+
63+
if(ffStrbufCompS(&strbuf, "123456789") != 0)
4564
testFailed(&strbuf, "strbuf.data != \"123456789\"");
4665

66+
//appendS long
67+
68+
ffStrbufAppendS(&strbuf, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
69+
70+
if(strbuf.length != 109)
71+
testFailed(&strbuf, "strbuf.length != 109");
72+
73+
if(strbuf.allocated < 110)
74+
testFailed(&strbuf, "strbuf.allocated < 110");
75+
76+
if(strbuf.chars[strbuf.length] != 0)
77+
testFailed(&strbuf, "strbuf.chars[strbuf.length] != 0");
78+
79+
strbuf.length = 9;
80+
4781
//startsWithS
4882

4983
if(!ffStrbufStartsWithS(&strbuf, "123"))
@@ -79,6 +113,18 @@ int main(int argc, char** argv)
79113
if(strcmp(strbuf.chars, "12316") != 0)
80114
testFailed(&strbuf, "strbuf.chars != \"12316\"");
81115

116+
//Destroy
117+
118+
ffStrbufDestroy(&strbuf);
119+
if(strbuf.allocated != 0)
120+
testFailed(&strbuf, "strbuf.allocated != 0");
121+
122+
if(strbuf.length != 0)
123+
testFailed(&strbuf, "strbuf.length != 0");
124+
125+
if(strbuf.chars != NULL)
126+
testFailed(&strbuf, "strbuf.chars != NULL");
127+
82128
//Success
83129
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
84130
}

0 commit comments

Comments
 (0)