Skip to content

Commit 83ca417

Browse files
committed
Docs: Small-string safety comments
1 parent be9e2d7 commit 83ca417

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

include/stringzilla/small_string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ SZ_PUBLIC sz_ptr_t sz_string_expand( //
197197
* Performs no allocations or deallocations and can't fail.
198198
*
199199
* @param string String to clean.
200-
* @param offset Offset of the first byte to remove.
200+
* @param offset Offset of the first byte to remove. If >= length, no bytes are removed.
201201
* @param length Number of bytes to remove. Out-of-bound ranges will be capped.
202-
* @return Number of bytes removed.
202+
* @return Number of bytes removed (0 if offset >= string length).
203203
*/
204204
SZ_PUBLIC sz_size_t sz_string_erase(sz_string_t *string, sz_size_t offset, sz_size_t length);
205205

include/stringzilla/stringzilla.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3452,8 +3452,9 @@ class basic_string {
34523452
// Update the string length appropriately
34533453
if (actual_count > string_length) {
34543454
string_start[actual_count] = '\0';
3455-
// ! Knowing the layout of the string, we can perform this operation safely,
3456-
// ! even if its located on stack.
3455+
// Safe for both SSO and heap strings: on little-endian, internal.length
3456+
// overlaps with LSB of external.length, so += affects only the length byte.
3457+
// On big-endian, the struct layout places them at matching positions.
34573458
string_.external.length += actual_count - string_length;
34583459
}
34593460
else { sz_string_erase(&string_, actual_count, SZ_SIZE_MAX); }
@@ -4190,8 +4191,9 @@ bool basic_string<char_type_, allocator_>::try_resize(size_type count, value_typ
41904191
if (count > string_length) {
41914192
sz_fill(string_start + string_length, count - string_length, character);
41924193
string_start[count] = '\0';
4193-
// Knowing the layout of the string, we can perform this operation safely,
4194-
// even if its located on stack.
4194+
// Safe for both SSO and heap strings: on little-endian, internal.length
4195+
// overlaps with LSB of external.length, so += affects only the length byte.
4196+
// On big-endian, the struct layout places them at matching positions.
41954197
string_.external.length += count - string_length;
41964198
}
41974199
else { sz_string_erase(&string_, count, SZ_SIZE_MAX); }

0 commit comments

Comments
 (0)