Skip to content

Commit 2ba6b01

Browse files
authored
Fix some memory issues with ctl::string (#1201)
There were a few errors in how capacity and memory was being handled for small strings. The capacity errors meant that small strings would become big strings too soon, and the memory error introduced undefined behavior that was caught by CheckMemoryLeaks in our test file but only sometimes. The crucial change is in reserve: we only copy n bytes into p2, and then we manually set the null terminator instead of expecting it to have been there already. (E.g. it might not be there for an empty small string.) We also fix one other doozy in append when we were exactly at the small- to-big string boundary: we set the last byte (i.e., the remainder field) to 0, then decremented it, giving us size_t max. Whoops. We boneheadedly fix this by setting the 0 byte after we've fixed up the remainder, so it is at worst a no-op. Otherwise, capacity now works the same for small strings as it does with big strings: it's the amount of space available including the null byte. We test all of this with a new test that only gets included if our class under test is not std::string (presumably meaning it's ctl::string.) The test manually verifies that the small string optimization behaves how we expect. Since this test checks against std::string, we go ahead and include that other header from the STL. Also modifies the new test we introduced to also run on std::string, but it just does the append without expecting anything about how its data is stored. We also check that the string has the right value afterwards.
1 parent f3effcb commit 2ba6b01

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

ctl/string.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ string::reserve(size_t c2) noexcept
9999
if (!isbig()) {
100100
if (!(p2 = (char*)malloc(c2)))
101101
__builtin_trap();
102-
memcpy(p2, data(), size() + 1);
102+
memcpy(p2, data(), size());
103+
p2[size()] = 0;
103104
} else {
104105
if (!(p2 = (char*)realloc(big()->p, c2)))
105106
__builtin_trap();
@@ -134,18 +135,18 @@ string::append(char ch) noexcept
134135
if (ckd_add(&n2, size(), 2))
135136
__builtin_trap();
136137
if (n2 > capacity()) {
137-
size_t c2 = capacity() + 2;
138+
size_t c2 = capacity();
138139
if (ckd_add(&c2, c2, c2 >> 1))
139140
__builtin_trap();
140141
reserve(c2);
141142
}
142143
data()[size()] = ch;
143-
data()[size() + 1] = 0;
144144
if (isbig()) {
145145
++big()->n;
146146
} else {
147147
--small()->rem;
148148
}
149+
data()[size()] = 0;
149150
}
150151

151152
void

ctl/string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class string
151151
if (isbig() && big()->c <= __::sso_max)
152152
__builtin_trap();
153153
#endif
154-
return isbig() ? __::big_mask & big()->c : __::sso_max;
154+
return isbig() ? __::big_mask & big()->c : __::string_size;
155155
}
156156

157157
iterator begin() noexcept

test/ctl/string_test.cc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "ctl/string.h"
2020

21+
#include <__type_traits/is_same.h>
2122
#include <__utility/move.h>
2223

2324
#include "libc/runtime/runtime.h"
@@ -312,7 +313,7 @@ main()
312313
s.append(" world");
313314
}
314315
if (s != "hello world world world world world world world world world "
315-
"world world") {
316+
"world world") {
316317
return 64;
317318
}
318319
}
@@ -353,6 +354,33 @@ main()
353354
return 78;
354355
}
355356

357+
{
358+
ctl::string s;
359+
#undef ctl
360+
if constexpr (std::is_same_v<ctl::string, decltype(s)>) {
361+
// tests the small-string optimization on ctl::string
362+
char* d = s.data();
363+
for (int i = 0; i < 23; ++i) {
364+
s.append("a");
365+
if (s.data() != d) {
366+
return 79 + i;
367+
}
368+
}
369+
s.append("a");
370+
if (s.data() == d) {
371+
return 103;
372+
}
373+
} else {
374+
// just check that append in a loop works
375+
for (int i = 0; i < 24; ++i) {
376+
s.append("a");
377+
}
378+
}
379+
if (s != "aaaaaaaaaaaaaaaaaaaaaaaa") {
380+
return 104;
381+
}
382+
}
383+
356384
CheckForMemoryLeaks();
357385
return 0;
358386
}

0 commit comments

Comments
 (0)