summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/objstr.c6
-rw-r--r--py/vstr.c5
2 files changed, 8 insertions, 3 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 0afc538b01..85b8fa3db3 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -1922,7 +1922,11 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) {
o->base.type = type;
o->len = vstr->len;
o->hash = qstr_compute_hash((byte*)vstr->buf, vstr->len);
- o->data = (byte*)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1);
+ if (vstr->len + 1 == vstr->alloc) {
+ o->data = (byte*)vstr->buf;
+ } else {
+ o->data = (byte*)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1);
+ }
((byte*)o->data)[o->len] = '\0'; // add null byte
vstr->buf = NULL;
vstr->alloc = 0;
diff --git a/py/vstr.c b/py/vstr.c
index ba85fa43c7..3367ae581d 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -52,9 +52,10 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
vstr->fixed_buf = false;
}
-// Init the vstr so it allocs exactly enough ram to hold given length, and set the length.
+// Init the vstr so it allocs exactly enough ram to hold a null-terminated
+// string of the given length, and set the length.
void vstr_init_len(vstr_t *vstr, size_t len) {
- vstr_init(vstr, len);
+ vstr_init(vstr, len + 1);
vstr->len = len;
}