The change to dynamically-allocated string options in r218617
introduced an issue with nested contexts, which were simply taking a
copy of the string pointer, rather than owning their own buffer.

Visible as various read-after-free errors when running
test-nested-contexts.c under valgrind, where a child context's dtor
would free the string option, but which would then be reused by a new
sibling context.

Fix it by creating new buffers for string options for child contexts.

Committed to trunk as r218972.

With this, jit.sum has:
  # of expected passes          4888

gcc/jit/ChangeLog:
        * jit-recording.c (gcc::jit::recording::context::context): When
        copying string options from a parent context, take a copy of the
        underlying buffers, rather than simply copying the pointer.
---
 gcc/jit/jit-recording.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/gcc/jit/jit-recording.c b/gcc/jit/jit-recording.c
index e52021d..d2b3a10 100644
--- a/gcc/jit/jit-recording.c
+++ b/gcc/jit/jit-recording.c
@@ -198,11 +198,14 @@ recording::context::context (context *parent_ctxt)
 
   if (parent_ctxt)
     {
-      /* Inherit options from parent.
-        Note that the first memcpy means copying pointers to strings.  */
-      memcpy (m_str_options,
-             parent_ctxt->m_str_options,
-             sizeof (m_str_options));
+      /* Inherit options from parent.  */
+      for (unsigned i = 0;
+          i < sizeof (m_str_options) / sizeof (m_str_options[0]);
+          i++)
+       {
+         const char *parent_opt = parent_ctxt->m_str_options[i];
+         m_str_options[i] = parent_opt ? xstrdup (parent_opt) : NULL;
+       }
       memcpy (m_int_options,
              parent_ctxt->m_int_options,
              sizeof (m_int_options));
-- 
1.8.5.3

Reply via email to