Subject: bug#78659: [PATCH] Pacify Clang's -Wformat-signedness.

From: Collin Funk <[email-masked]>
Date: Sat, 31 May 2025 22:16:15 -0700

Hi,

When compiling with './autogen.sh && ./configure CC=clang && make':

    [...]
    xfns.c:2041:6: warning: format specifies type 'unsigned int' but the argument has type 'int' [-Wformat]
     2040 |           sprintf (css, "scrollbar slider { background-color: #%02x%02x%02x; }",
          |                                                                ~~~~
          |                                                                %02x
     2041 |                    color.red >> 8, color.green >> 8, color.blue >> 8);
    [...]

This warning is harmless, but interestingly 'gcc -Wformat-signedness'
does not warn about it, but 'clang -Wformat-signedness' does.

Since the color fields of struct XColor are 'unsigned short' and the
constant is an 'int', the expression should be an 'int'.

Anyways, the attached patch silences the warnings.

Collin

>From 1c34ded32a2826d009ee531c1f0d695b8144b39d Mon Sep 17 00:00:00 2001
Message-ID: <1c34[email-masked]>
From: Collin Funk <[email-masked]>
Date: Sat, 31 May 2025 21:54:54 -0700
Subject: [PATCH] Pacify Clang's -Wformat-signedness.

* src/gtkutil.c (xg_set_widget_bg): Cast the expression passed to the
"%x" specifier to unsigned int.
* src/xfns.c (x_set_scroll_bar_foreground, x_set_scroll_bar_background):
Likewise.
---
 src/gtkutil.c | 3 ++-
 src/xfns.c    | 8 ++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/gtkutil.c b/src/gtkutil.c
index c192102730c..c8b0943fb2f 100644
--- a/src/gtkutil.c
+++ b/src/gtkutil.c
@@ -1434,7 +1434,8 @@ xg_set_widget_bg (struct frame *f, GtkWidget *w, unsigned long pixel)
     {
       static char const format[] = "* { background-color: #%02x%02x%02x; }";
       char buffer[sizeof format + 3 * INT_STRLEN_BOUND (xbg.red)];
-      sprintf (buffer, format, xbg.red >> 8, xbg.green >> 8, xbg.blue >> 8);
+      sprintf (buffer, format, (unsigned int) xbg.red >> 8,
+	       (unsigned int) xbg.green >> 8, (unsigned int) xbg.blue >> 8);
       GtkCssProvider *provider = gtk_css_provider_new ();
       gtk_css_provider_load_from_data (provider, buffer, -1, NULL);
       gtk_style_context_add_provider (gtk_widget_get_style_context(w),
diff --git a/src/xfns.c b/src/xfns.c
index 4f7be935000..5f0cb0e16a9 100644
--- a/src/xfns.c
+++ b/src/xfns.c
@@ -2038,7 +2038,9 @@ x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object old
 		       &color);
 
 	  sprintf (css, "scrollbar slider { background-color: #%02x%02x%02x; }",
-		   color.red >> 8, color.green >> 8, color.blue >> 8);
+		   (unsigned int) color.red >> 8,
+		   (unsigned int) color.green >> 8,
+		   (unsigned int) color.blue >> 8);
 	  gtk_css_provider_load_from_data (FRAME_X_OUTPUT (f)->scrollbar_foreground_css_provider,
 					   css, -1, NULL);
 	}
@@ -2111,7 +2113,9 @@ x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object old
 			 &color);
 
 	    sprintf (css, "scrollbar trough { background-color: #%02x%02x%02x; }",
-		     color.red >> 8, color.green >> 8, color.blue >> 8);
+		     (unsigned int) color.red >> 8,
+		     (unsigned int) color.green >> 8,
+		     (unsigned int) color.blue >> 8);
 	    gtk_css_provider_load_from_data (FRAME_X_OUTPUT (f)->scrollbar_background_css_provider,
 					     css, -1, NULL);
 	  }
-- 
2.49.0