Skip to content

Commit

Permalink
Properly handle RichString_extendLen when an external buffer already …
Browse files Browse the repository at this point in the history
…exists
  • Loading branch information
BenBE committed Apr 6, 2024
1 parent 85c3c3a commit a6306f0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions RichString.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ in the source distribution for its full text.
#define charBytes(n) (sizeof(CharType) * (n))

static void RichString_extendLen(RichString* this, int len) {
if (this->chlen <= RICHSTRING_MAXLEN) {
if (this->chptr == this->chstr) {
// String is in internal buffer
if (len > RICHSTRING_MAXLEN) {
// Copy from internal buffer to allocated string
this->chptr = xMalloc(charBytes(len + 1));
memcpy(this->chptr, this->chstr, charBytes(this->chlen));
} else {
// Still fits in internal buffer, do nothing
assert(this->chlen <= RICHSTRING_MAXLEN);
}
} else {
if (len <= RICHSTRING_MAXLEN) {
// String is managed externally
if (len > RICHSTRING_MAXLEN) {
// Just reallocate the buffer accordingly
this->chptr = xRealloc(this->chptr, charBytes(len + 1));
} else {
// Move string into internal buffer and free resources
memcpy(this->chstr, this->chptr, charBytes(len));
free(this->chptr);
this->chptr = this->chstr;
} else {
this->chptr = xRealloc(this->chptr, charBytes(len + 1));
}
}

Expand Down

0 comments on commit a6306f0

Please sign in to comment.