]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/tui: use init_extended_color where possible
authorAndrew Burgess <aburgess@redhat.com>
Tue, 21 Jul 2026 13:37:45 +0000 (14:37 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Thu, 13 Aug 2026 11:07:35 +0000 (12:07 +0100)
After commit:

  commit fbe7f20a0f098ca03913452b29f50f0dc8568f77
  Date:   Sat May 9 23:27:43 2026 +0200

    gdb/tui: fix unexpected reuse of color pairs

which converted GDB to use init_extended_pair where possible, I
realised we could also make use of init_extended_color.

The motivation for using init_extended_color is slightly less than
init_extended_pair.  Assuming the terminal supports it the standard
init_color API supports up to SHRT_MAX (32767) different colors,
switching to init_extended_color removes the SHRT_MAX limit on color
indices, allowing us to support the full range of COLORS.

But the cost of making this change is minimal, we already track the
color indices as an `int` within the global COLOR_MAP, so it's mostly
just a case of calling init_extended_color where needed.

We only use init_extended_color when both that function and
init_extended_pair is available.  The fallback to init_extended_pair
is init_pair, which expects the color indices to be shorts.  If we are
using the init_pair fallback then using init_extended_color is
pointless.

In reality init_extended_pair and init_extended_color were both added
in ncurses 6.1, so should both be available together.

There is one additional change in here.  Assuming that a terminal does
support more than SHRT_MAX colours, but for some reason GDB is
compiled with a version of the curses library that doesn't support
init_extended_color, then it is possible that in `get_color` the value
of NEXT could end up above SHRT_MAX, in which case the `init_color`
call will truncate the value of NEXT to a short and we will end up
redefining an earlier color index.  To avoid this unlikely case I've
added a compare against SHRT_MAX.

The init_extended_color path doesn't have this risk as COLORS is an
`int` and NEXT is passed as an `int` on this path so there is no risk
of truncation.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/config.in
gdb/configure
gdb/configure.ac
gdb/tui/tui-io.c

index 1ef5dc5c2f4ee9eb513cfd8d8d863302fc939296..74745665d4d42945d029e062f1ba87ca713175fa 100644 (file)
 /* Define to 1 if you have the `iconvlist' function. */
 #undef HAVE_ICONVLIST
 
+/* Define to 1 if you have the `init_extended_color' function. */
+#undef HAVE_INIT_EXTENDED_COLOR
+
 /* Define to 1 if you have the `init_extended_pair' function. */
 #undef HAVE_INIT_EXTENDED_PAIR
 
index 633004d3f70dc0d70aa337db814a4d3a55061c3c..6107a9fe25a59ed65c88e0b7fc7ffa64541cb878 100755 (executable)
@@ -30238,6 +30238,7 @@ for ac_func in  \
   getrlimit \
   getuid \
   iconvlist \
+  init_extended_color \
   init_extended_pair \
   libiconvlist \
   posix_madvise \
index 943b2218a4165ce57073537149248cf0123a746a..4f870ba0bd82a38a6ab20b1099282276efaa1502 100644 (file)
@@ -1520,6 +1520,7 @@ AC_CHECK_FUNCS([ \
   getrlimit \
   getuid \
   iconvlist \
+  init_extended_color \
   init_extended_pair \
   libiconvlist \
   posix_madvise \
index 896d00f44c8dc6d0765dc1f5b59e93320148bb8e..b6afe08be3e69191d118c5861d5552f2137c08aa 100644 (file)
@@ -252,10 +252,27 @@ get_color (const ui_file_style::color &color, int *result)
          if (next >= COLORS)
            return false;
          rgb_color rgb = color.get_rgb ();
+
          /* We store RGB as 0..255, but curses wants 0..1000.  */
-         if (init_color (next, rgb[0] * 1000 / 255, rgb[1] * 1000 / 255,
-                         rgb[2] * 1000 / 255) == ERR)
+         short r = rgb[0] * 1000 / 255;
+         short g = rgb[1] * 1000 / 255;
+         short b = rgb[2] * 1000 / 255;
+
+         /* If init_extended_pair is not available then we fallback to
+            using init_pair.  However, init_pair can only handle 'short'
+            color indices so there is no point using init_extended_color
+            to allow for the generation of longer 'int' color indices.  */
+#if defined HAVE_INIT_EXTENDED_COLOR && defined HAVE_INIT_EXTENDED_PAIR
+         if (init_extended_color (next, r, g, b) == ERR)
+           return false;
+#else
+         /* NEXT is an int, but is passed as a short.  If COLORS is
+            more than SHRT_MAX then NEXT will be truncated and end up
+            redefining a color entry that we don't expect.  */
+         if (next > SHRT_MAX
+             || init_color (next, r, g, b) == ERR)
            return false;
+#endif
          color_map[color] = next;
          *result = next;
        }