gdb: initialize packet_result::m_textual_err_msg

Message ID 20240516163557.529065-1-simon.marchi@polymtl.ca
State New
Headers
Series gdb: initialize packet_result::m_textual_err_msg |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Patch is already merged

Commit Message

Simon Marchi May 16, 2024, 4:35 p.m. UTC
  From: Simon Marchi <simon.marchi@efficios.com>

When building GDB with -O2 and --enable-ubsan, I get some random errors
in the packet_result self test:

  /home/smarchi/src/binutils-gdb/gdb/remote.c:161:7: runtime error: load of value 92, which is not a valid value for type 'bool'

This happens because packet_result::m_textual_err_msg is uninitialized
when using the second constructor.  When such a packet_result object
gets copied, an invalid value for m_textual_err_msg (a bool field) is
loaded, which triggers ubsan.

Avoid this by initializing m_textual_err_msg.

Change-Id: I3ce44816bb0bfc6e442067292f993e5c17301b85
---
 gdb/remote.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


base-commit: 74d2676602ee0b812478797953f95cf9a9de2ed4
  

Comments

Tom Tromey May 16, 2024, 5:24 p.m. UTC | #1
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

Simon> This happens because packet_result::m_textual_err_msg is uninitialized
Simon> when using the second constructor.  When such a packet_result object
Simon> gets copied, an invalid value for m_textual_err_msg (a bool field) is
Simon> loaded, which triggers ubsan.

Simon> Avoid this by initializing m_textual_err_msg.

Looks reasonable to me.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Simon Marchi May 16, 2024, 5:26 p.m. UTC | #2
On 2024-05-16 13:24, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
> 
> Simon> This happens because packet_result::m_textual_err_msg is uninitialized
> Simon> when using the second constructor.  When such a packet_result object
> Simon> gets copied, an invalid value for m_textual_err_msg (a bool field) is
> Simon> loaded, which triggers ubsan.
> 
> Simon> Avoid this by initializing m_textual_err_msg.
> 
> Looks reasonable to me.
> Approved-By: Tom Tromey <tom@tromey.com>
> 
> Tom

Thanks, pushed.

Simon
  

Patch

diff --git a/gdb/remote.c b/gdb/remote.c
index 6e568eb47b23..42b446c7e27e 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -222,7 +222,7 @@  class packet_result
 
   /* True if we have a textual error message, from an "E.MESSAGE"
      response.  */
-  bool m_textual_err_msg;
+  bool m_textual_err_msg = false;
 };
 
 /* Enumeration of packets for a remote target.  */