From 9a93831ccc0ba3ba447552069f230e6d93dcbf3f Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 6 Dec 2017 16:27:33 -0500 Subject: [PATCH] Fix syscall group completion 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 | 5 +++++ gdb/break-catch-syscall.c | 12 ++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 93acc17a52..c9a409831f 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2017-12-06 Simon Marchi + + * break-catch-syscall.c (catch_syscall_completer): Get pointers + to syscall group strings after building the string vector. + 2017-12-06 Pedro Alves * remote.c (remote_query_supported): Don't send "xmlRegisters=" if diff --git a/gdb/break-catch-syscall.c b/gdb/break-catch-syscall.c index a8312f2542..dd7b37997b 100644 --- a/gdb/break-catch-syscall.c +++ b/gdb/break-catch-syscall.c @@ -559,7 +559,6 @@ catch_syscall_completer (struct cmd_list_element *cmd, struct gdbarch *gdbarch = get_current_arch (); gdb::unique_xmalloc_ptr 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 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);