Skip to content

Commit

Permalink
Fix append of empty string to unallocated dynamic string
Browse files Browse the repository at this point in the history
  • Loading branch information
ojwb committed Mar 19, 2024
1 parent 71c99e5 commit 2ca7003
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void s_expand_(string *pstr, int addition) {
void
s_catlen(string* pstr, const char *s, int s_len)
{
if (pstr->capacity - pstr->len < s_len)
if (pstr->capacity - pstr->len < s_len || s_len == 0)
s_expand_(pstr, s_len);
memcpy(pstr->s + pstr->len, s, s_len);
pstr->len += s_len;
Expand All @@ -43,7 +43,7 @@ s_catlen(string* pstr, const char *s, int s_len)
void
s_catn(string *pstr, int n, char c)
{
if (pstr->capacity - pstr->len < n)
if (pstr->capacity - pstr->len < n || n == 0)
s_expand_(pstr, n);
memset(pstr->s + pstr->len, c, n);
pstr->len += n;
Expand Down

0 comments on commit 2ca7003

Please sign in to comment.