cosmopolitan/test
Jōshin 2ba6b0158f
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.
2024-06-07 01:15:37 -04:00
..
ctl Fix some memory issues with ctl::string (#1201) 2024-06-07 01:15:37 -04:00
dsp Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
libc Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
libcxx vim C++ filetype is still spelled "cpp" 2024-06-05 16:34:47 -07:00
math Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
net Refactor and improve CTL and other code 2024-06-04 05:45:48 -07:00
posix Add some noexcept annotations 2024-06-01 03:19:53 -07:00
tool Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
BUILD.mk Introduce Cosmopolitan Templates Library (CTL) 2024-06-03 09:21:59 -07:00