Fix syscall group completion
authorSimon Marchi <simon.marchi@ericsson.com>
Wed, 6 Dec 2017 21:27:33 +0000 (16:27 -0500)
committerSimon Marchi <simon.marchi@ericsson.com>
Wed, 6 Dec 2017 21:37:29 +0000 (16:37 -0500)
The test gdb.base/catch-syscall.exp has been failing since commit

  3d415c26bad3a15eed00d2ddf85c4268df77a4cc
  Remove cleanups from break-catch-syscall.c

The reason is that we are putting into the group_ptr array a pointer to
the buffer of the local string object.  If the string is small enough to
fit in the internal string buffer (used for small string optimizations),
the pointer will point to the local object directly.  So even if we
std::move the string to the vector, the pointer in group_ptr will still
point to the local object.  When we reuse that object (technically a new
instance, but most likely the same memory) for the next syscall, we'll
overwrite the previous string.  The result is that we'll get less
results than expected, since there will be duplicates.

We'll also run into problems if we push the string to the vector, and
then record the c_str () pointer using the string object in the vector.
The vector might get reallocated, the string may move in memory, and our
pointer in group_ptr will point to stale memory.

Instead, we have to push all the strings first, then, when we know the
vector won't change anymore, build the group_ptr array.  This is what
this patch does.

gdb/ChangeLog:

* break-catch-syscall.c (catch_syscall_completer): Get pointers
to syscall group strings after building the string vector.

gdb/ChangeLog
gdb/break-catch-syscall.c

index 93acc17..c9a4098 100644 (file)
@@ -1,3 +1,8 @@
+2017-12-06  Simon Marchi  <simon.marchi@ericsson.com>
+
+       * break-catch-syscall.c (catch_syscall_completer): Get pointers
+       to syscall group strings after building the string vector.
+
 2017-12-06  Pedro Alves  <palves@redhat.com>
 
        * remote.c (remote_query_supported): Don't send "xmlRegisters=" if
index a8312f2..dd7b379 100644 (file)
@@ -559,7 +559,6 @@ catch_syscall_completer (struct cmd_list_element *cmd,
   struct gdbarch *gdbarch = get_current_arch ();
   gdb::unique_xmalloc_ptr<const char *> group_list;
   const char *prefix;
-  int i;
 
   /* Completion considers ':' to be a word separator, so we use this to
      verify whether the previous word was a group prefix.  If so, we
@@ -587,14 +586,11 @@ catch_syscall_completer (struct cmd_list_element *cmd,
       std::vector<std::string> holders;
 
       /* Append "group:" prefix to syscall groups.  */
-      for (i = 0; group_ptr[i] != NULL; i++)
-       {
-         std::string prefixed_group = string_printf ("group:%s",
-                                                     group_ptr[i]);
+      for (int i = 0; group_ptr[i] != NULL; i++)
+       holders.push_back (string_printf ("group:%s", group_ptr[i]));
 
-         group_ptr[i] = prefixed_group.c_str ();
-         holders.push_back (std::move (prefixed_group));
-       }
+      for (int i = 0; group_ptr[i] != NULL; i++)
+       group_ptr[i] = holders[i].c_str ();
 
       if (syscall_list != NULL)
        complete_on_enum (tracker, syscall_list.get (), word, word);