Commit a1f2245a authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

py/vstr: vstr_null_terminated_str(): Extend string by at most one byte.

vstr_null_terminated_str is almost certainly a vstr finalization operation,
so it should add the requested NUL byte, and not try to pre-allocate more.
The previous implementation could actually allocate double of the buffer
size.
parent 6de8dbb4
......@@ -181,9 +181,15 @@ char *vstr_add_len(vstr_t *vstr, size_t len) {
// Doesn't increase len, just makes sure there is a null byte at the end
char *vstr_null_terminated_str(vstr_t *vstr) {
if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) {
if (vstr->had_error) {
return NULL;
}
// If there's no more room, add single byte
if (vstr->alloc == vstr->len) {
if (vstr_extend(vstr, 1) == NULL) {
return NULL;
}
}
vstr->buf[vstr->len] = '\0';
return vstr->buf;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment