Branch: Tag:

2017-08-17

2017-08-17 09:28:24 by Henrik Grubbström (Grubba) <grubba@grubba.org>

String: Fix issue where substrings lost their contents.

Adds new string flag STRING_IS_LOCKED to lock the string data memory.

If a dynamically allocated string (eg "Random.Deterministic") that had
an active substring (eg "Deterministic") later got replaced with a
statically allocated string, the substring would point to freed memory.

Fixes intermittent failures to resolve "Random.Deterministic" in from
Image.Image()->test(), as well as the fatal "Shared string is not zero
terminated properly." from eg _verify_internals().

FIXME: There are most likely still lots of thread race-conditions hiding
with eg I/O operations and this reallocation behavior.

713:    s = make_static_string(str, len, shift);    link_pike_string(s, h);    } else { -  if (!string_is_static(s)) +  /* NB: The following is only possible if there are no substring references +  * to the old string. +  */ +  if (!(s->flags & STRING_IS_LOCKED) && !string_is_static(s))    {    free_string_content(s);    s->alloc_type = STRING_ALLOC_STATIC;
1877:    }    res = ba_alloc(&substring_allocator);    res->parent = s; +  s->flags |= STRING_IS_LOCKED; /* Make sure the string data isn't reallocated. */    add_ref(s);    existing = &res->str;