[binutils-gdb] gdb: add assert in cmd_list_element::set_context

Simon Marchi simark@sourceware.org
Sat Jun 26 01:40:54 GMT 2021


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d6ff04a343f97026f7d23bb036e0f858b21f3bfd

commit d6ff04a343f97026f7d23bb036e0f858b21f3bfd
Author: Simon Marchi <simon.marchi@polymtl.ca>
Date:   Fri Jun 25 21:38:51 2021 -0400

    gdb: add assert in cmd_list_element::set_context
    
    If something tries to set a context pointer on a cmd_list_element and
    m_context is not 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

Diff:
---
 gdb/ChangeLog        | 5 +++++
 gdb/cli/cli-decode.h | 5 ++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index c523186cdd7..6ba91d8ec6e 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2021-06-25  Simon Marchi  <simon.marchi@polymtl.ca>
+
+	* cli/cli-decode.h (struct cmd_list_element) <set_context>: Add
+	assert.
+
 2021-06-25  Simon Marchi  <simon.marchi@polymtl.ca>
 
 	* cli/cli-decode.h (struct cmd_list_element) <set_context,
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 1692a6e2835..241535ae5b5 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; }


More information about the Gdb-cvs mailing list