Skip to content

Commit b325b1c

Browse files
committed
Improve: Self-equality & overflow protection
1 parent 83ca417 commit b325b1c

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

include/stringzilla/small_string.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ SZ_PUBLIC void sz_string_unpack( //
256256
}
257257

258258
SZ_PUBLIC sz_bool_t sz_string_equal(sz_string_t const *a, sz_string_t const *b) {
259+
// Fast path for self-comparison
260+
if (a == b) return sz_true_k;
259261
// Tempting to say that the external.length is bitwise the same even if it includes
260262
// some bytes of the on-stack payload, but we don't at this writing maintain that invariant.
261263
// (An on-stack string includes noise bytes in the high-order bits of external.length. So do this
@@ -394,6 +396,9 @@ SZ_PUBLIC sz_ptr_t sz_string_expand( //
394396
// The user intended to extend the string.
395397
offset = sz_min_of_two(offset, string_length);
396398

399+
// Guard against integer overflow in size calculation.
400+
if (added_length > SZ_SSIZE_MAX - string_length - 1) return SZ_NULL_CHAR;
401+
397402
// If we are lucky, no memory allocations will be needed.
398403
if (string_length + added_length < string_space) {
399404
sz_move(string_start + offset + added_length, string_start + offset, string_length - offset);

0 commit comments

Comments
 (0)