Unconditionally appending '\0' is not a big problem, but that does
trigger the buffer to potentially be re-allocated. Avoid that by
checking the last char is not already NUL.
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
const char *strbuf_str(struct strbuf *buf)
{
- if (!strbuf_reserve_extra(buf, 1))
- return NULL;
- buf->bytes[buf->used] = '\0';
+ if (!buf->used || buf->bytes[buf->used - 1]) {
+ if (!strbuf_reserve_extra(buf, 1))
+ return NULL;
+ buf->bytes[buf->used] = '\0';
+ }
return buf->bytes;
}