diff --git a/include/QF/dstring.h b/include/QF/dstring.h index 073de9011..9a05861e4 100644 --- a/include/QF/dstring.h +++ b/include/QF/dstring.h @@ -140,12 +140,12 @@ void dstring_copystr (dstring_t *dstr, const char *str); void dstring_copysubstr (dstring_t *dstr, const char *str, size_t len); /** Append the null terminated string to the end of the dstring. The dstring does not have to be null terminated but will become so. - However, any embedded nulls will be treated as the end of the dstring. + \note any embedded nulls will be ignored. */ void dstring_appendstr (dstring_t *dstr, const char *str); /** Append up to len bytes from the string to the end of the dstring. The dstring does not have to be null terminated but will become so. - However, any embedded nulls will be treated as the end of the dstring. + \note any embedded nulls will be ignored. */ void dstring_appendsubstr (dstring_t *dstr, const char *str, size_t len); /** Insert the null terminated string into the dstring at pos. The dstring diff --git a/libs/util/dstring.c b/libs/util/dstring.c index 48dd43ba0..05e6f75bc 100644 --- a/libs/util/dstring.c +++ b/libs/util/dstring.c @@ -271,9 +271,12 @@ dstring_copysubstr (dstring_t *dstr, const char *str, size_t len) VISIBLE void dstring_appendstr (dstring_t *dstr, const char *str) { - size_t pos = strnlen (dstr->str, dstr->size); + size_t pos = dstr->size; size_t len = strlen (str); + if (pos && !dstr->str[pos - 1]) { + pos--; + } dstr->size = pos + len + 1; dstring_adjust (dstr); strcpy (dstr->str + pos, str); @@ -282,8 +285,11 @@ dstring_appendstr (dstring_t *dstr, const char *str) VISIBLE void dstring_appendsubstr (dstring_t *dstr, const char *str, size_t len) { - size_t pos = strnlen (dstr->str, dstr->size); + size_t pos = dstr->size; + if (pos && !dstr->str[pos - 1]) { + pos--; + } len = strnlen (str, len); dstr->size = pos + len + 1; dstring_adjust (dstr);