summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-08-30 10:04:25 +0200
committerTyge Løvset <[email protected]>2020-08-30 10:04:25 +0200
commita5b9c6890eb18c43cd6e851d4f96b05febbd31d3 (patch)
tree4971ab84aac7cf1b8a6ec9ec2707b090921ca0e8
parent7b80957af77ee75ee6d32f6cacafac8430c1c097 (diff)
downloadSTC-modified-a5b9c6890eb18c43cd6e851d4f96b05febbd31d3.tar.gz
STC-modified-a5b9c6890eb18c43cd6e851d4f96b05febbd31d3.zip
Fixed an important bug in cstr.h append().
-rw-r--r--stc/cstr.h5
1 files changed, 4 insertions, 1 deletions
diff --git a/stc/cstr.h b/stc/cstr.h
index 6e6bcedb..b5aab764 100644
--- a/stc/cstr.h
+++ b/stc/cstr.h
@@ -275,8 +275,11 @@ STC_API cstr_t*
cstr_append_n(cstr_t* self, const char* str, size_t len) {
if (len) {
size_t oldlen = cstr_size(*self), newlen = oldlen + len;
- if (newlen > cstr_capacity(*self))
+ if (newlen > cstr_capacity(*self)) {
+ size_t off = (size_t) (str - self->str);
cstr_reserve(self, newlen * 3 / 2);
+ if (off <= oldlen) str = self->str + off;
+ }
memmove(&self->str[oldlen], str, len);
self->str[_cstr_size(*self) = newlen] = '\0';
}