[binutils-gdb] gdb/python: cast to ui_file_style::intensity after validating value

Simon Marchi simark@sourceware.org
Wed Oct 8 18:58:54 GMT 2025


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

commit 4cab44595eed299ec196902a54710be2fd224083
Author: Simon Marchi <simon.marchi@polymtl.ca>
Date:   Wed Oct 8 11:53:10 2025 -0400

    gdb/python: cast to ui_file_style::intensity after validating value
    
    When GDB is built with undefined behavior sanitizer,
    gdb.python/py-style.exp fails because of this:
    
        $ ./gdb -q -nx --data-directory=data-directory -ex "python filename_style = gdb.Style('filename')" -ex "python filename_style.intensity = -3"
        /home/simark/src/binutils-gdb/gdb/python/py-style.c:239:11: runtime error: load of value 4294967293, which is not a valid value for type 'intensity'
    
    Fix it by casting the value to ui_file_style::intensity only after
    validating the raw value.
    
    Change-Id: I38eb471a9cb3bfc3bb8b2c88afa76b8025e4e893
    Approved-By: Tom Tromey <tom@tromey.com>

Diff:
---
 gdb/python/py-style.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/gdb/python/py-style.c b/gdb/python/py-style.c
index cf65e3115b2..b10a45fced9 100644
--- a/gdb/python/py-style.c
+++ b/gdb/python/py-style.c
@@ -234,14 +234,12 @@ stylepy_init_from_style_name (PyObject *self, const char *style_name)
 static std::optional<ui_file_style::intensity>
 stylepy_long_to_intensity (long intensity_value)
 {
-  ui_file_style::intensity intensity
-    = static_cast<ui_file_style::intensity> (intensity_value);
-  switch (intensity)
+  switch (intensity_value)
     {
     case ui_file_style::NORMAL:
     case ui_file_style::DIM:
     case ui_file_style::BOLD:
-      break;
+      return static_cast<ui_file_style::intensity> (intensity_value);
 
     default:
       PyErr_Format
@@ -249,8 +247,6 @@ stylepy_long_to_intensity (long intensity_value)
 	 intensity_value);
       return {};
     }
-
-  return intensity;
 }
 
 /* Initialise a gdb.Style object from a foreground and background


More information about the Gdb-cvs mailing list