Skip to content

Commit a1f2245

Browse files
committed
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.
1 parent 6de8dbb commit a1f2245

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

py/vstr.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,15 @@ char *vstr_add_len(vstr_t *vstr, size_t len) {
181181

182182
// Doesn't increase len, just makes sure there is a null byte at the end
183183
char *vstr_null_terminated_str(vstr_t *vstr) {
184-
if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) {
184+
if (vstr->had_error) {
185185
return NULL;
186186
}
187+
// If there's no more room, add single byte
188+
if (vstr->alloc == vstr->len) {
189+
if (vstr_extend(vstr, 1) == NULL) {
190+
return NULL;
191+
}
192+
}
187193
vstr->buf[vstr->len] = '\0';
188194
return vstr->buf;
189195
}

0 commit comments

Comments
 (0)