aboutsummaryrefslogtreecommitdiff
path: root/gdb/tracefile-tfile.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2019-01-10 17:52:39 +0000
committerPedro Alves <palves@redhat.com>2019-01-10 18:04:02 +0000
commit67aa1f3c2881e607081d9e1b57be3e7544c2c45c (patch)
tree445e67a51e5704a6560ba2665df76a37d6607705 /gdb/tracefile-tfile.c
parent2f667667e24357ff54701f3e046820cf08d649cf (diff)
downloadgdb-67aa1f3c2881e607081d9e1b57be3e7544c2c45c.zip
gdb-67aa1f3c2881e607081d9e1b57be3e7544c2c45c.tar.gz
gdb-67aa1f3c2881e607081d9e1b57be3e7544c2c45c.tar.bz2
Fix tracepoint.c:parse_tracepoint_definition leak (and one more)
Coverity points out that gdb/tracepoint.c:parse_tracepoint_definition can leak 'cond' in this line: cond = (char *) xmalloc (2 * xlen + 1); That can leak because we're in a loop and 'cond' may have already been xmalloc'ed into in a previous iteration. That won't normally happen, because we don't expect to see a tracepoint definition with multiple conditions listed, but, it doesn't hurt to be pedantically correct, in case some stub manages to send something odd back to GDB. At first I thought I'd just replace the xmalloc call with: cond = (char *) xrealloc (cond, 2 * xlen + 1); and be done with it. However, my pedantic self realizes that warning() can throw as well (due to pagination + Ctrl-C), so I fixed it using gdb::unique_xmalloc_ptr instead. While doing this, I noticed that these vectors in struct uploaded_tp: std::vector<char *> actions; std::vector<char *> step_actions; hold heap-allocated strings, but nothing is freeing the strings, AFAICS. So I ended up switching all the heap-allocated strings in uploaded_tp to unique pointers. This patch is the result of that. I also wrote an alternative, but similar patch that uses std::string throughout instead of gdb::unique_xmalloc_ptr, but in the end reverted it because the code didn't look that much better, and I kind of dislike replacing pointers with fat std::string's (3 or 4 times the size of a pointer) in structures. gdb/ChangeLog: 2019-01-10 Pedro Alves <palves@redhat.com> * breakpoint.c (read_uploaded_action) (create_tracepoint_from_upload): Adjust to use gdb::unique_xmalloc_ptr. * ctf.c (ctf_write_uploaded_tp): (SET_ARRAY_FIELD): Use emplace_back. (SET_STRING_FIELD): Adjust to use gdb::unique_xmalloc_ptr. * tracefile-tfile.c (tfile_write_uploaded_tp): * tracepoint.c (parse_tracepoint_definition): Adjust to use gdb::unique_xmalloc_ptr. * tracepoint.h (struct uploaded_tp) <cond, actions, step_actions, at_string, cond_string, cmd_strings>: Replace char pointers with gdb::unique_xmalloc_ptr.
Diffstat (limited to 'gdb/tracefile-tfile.c')
-rw-r--r--gdb/tracefile-tfile.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index afff422..831f162 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -262,31 +262,32 @@ tfile_write_uploaded_tp (struct trace_file_writer *self,
fprintf (writer->fp, ":F%x", utp->orig_size);
if (utp->cond)
fprintf (writer->fp,
- ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
- utp->cond);
+ ":X%x,%s", (unsigned int) strlen (utp->cond.get ()) / 2,
+ utp->cond.get ());
fprintf (writer->fp, "\n");
- for (char *act : utp->actions)
+ for (const auto &act : utp->actions)
fprintf (writer->fp, "tp A%x:%s:%s\n",
- utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
- for (char *act : utp->step_actions)
+ utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
+ for (const auto &act : utp->step_actions)
fprintf (writer->fp, "tp S%x:%s:%s\n",
- utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
+ utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act.get ());
if (utp->at_string)
{
encode_source_string (utp->number, utp->addr,
- "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
+ "at", utp->at_string.get (),
+ buf, MAX_TRACE_UPLOAD);
fprintf (writer->fp, "tp Z%s\n", buf);
}
if (utp->cond_string)
{
encode_source_string (utp->number, utp->addr,
- "cond", utp->cond_string,
+ "cond", utp->cond_string.get (),
buf, MAX_TRACE_UPLOAD);
fprintf (writer->fp, "tp Z%s\n", buf);
}
- for (char *act : utp->cmd_strings)
+ for (const auto &act : utp->cmd_strings)
{
- encode_source_string (utp->number, utp->addr, "cmd", act,
+ encode_source_string (utp->number, utp->addr, "cmd", act.get (),
buf, MAX_TRACE_UPLOAD);
fprintf (writer->fp, "tp Z%s\n", buf);
}