public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/4] gdb: add assert in cmd_list_element::set_context
Date: Fri, 25 Jun 2021 16:03:20 -0400	[thread overview]
Message-ID: <20210625200322.3845132-3-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20210625200322.3845132-1-simon.marchi@polymtl.ca>

If something tries to set a context pointer on a cmd_list_element and
m_context is no nullptr, it's likely that two parts of the code are
trying to set different contexts, and one will overwrite the other.
This is almost guaranteed to lead to bad behavior or a crash, as one of
the spots will not be using the data it expects.  This happened to me
during development, so I think having this assert would be useful to
catch this problem earlier.

gdb/ChangeLog:

	* cli/cli-decode.h (struct cmd_list_element) <set_context>: Add
	assert.

Change-Id: I1f2e9fda1bf2bec1b732c9b90e7d7910a97f2ac6
---
 gdb/cli/cli-decode.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 1692a6e28352..241535ae5b50 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -94,7 +94,10 @@ struct cmd_list_element
   { return this->func == nullptr; }
 
   void set_context (void *context)
-  { m_context = context; }
+  {
+    gdb_assert (m_context == nullptr);
+    m_context = context;
+  }
 
   void *context () const
   { return m_context; }
-- 
2.32.0


  parent reply	other threads:[~2021-06-25 20:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-25 20:03 [PATCH 0/4] Small cmd_list_element-related cleanups Simon Marchi
2021-06-25 20:03 ` [PATCH 1/4] gdb: add context getter/setter to cmd_list_element Simon Marchi
2021-06-25 21:03   ` Andrew Burgess
2021-06-25 20:03 ` Simon Marchi [this message]
2021-06-25 21:05   ` [PATCH 2/4] gdb: add assert in cmd_list_element::set_context Andrew Burgess
2021-06-25 20:03 ` [PATCH 3/4] gdb: remove context parameter from add_setshow_enum_cmd Simon Marchi
2021-06-25 21:09   ` Andrew Burgess
2021-06-26  1:38     ` Simon Marchi
2021-06-25 20:03 ` [PATCH 4/4] gdb/guile: use return values of add_setshow functions in add_setshow_generic Simon Marchi
2021-06-25 21:17   ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210625200322.3845132-3-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).