In a testdir of all crypto modules:

      CC       gc-gnulib.o
    gc-gnulib.c: In function ‘gc_cipher_setkey’:
    gc-gnulib.c:293:45: warning: format ‘%x’ expects argument of type ‘unsigned 
int’, but argument 3 has type ‘int’ [-Wformat=]
      293 |           sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
          |                                          ~~~^   ~~~~~~~~~~~~~
          |                                             |          |
          |                                             |          int
          |                                             unsigned int
          |                                          %02x
    gc-gnulib.c: In function ‘gc_cipher_setiv’:
    gc-gnulib.c:351:48: warning: format ‘%x’ expects argument of type ‘unsigned 
int’, but argument 3 has type ‘int’ [-Wformat=]
      351 |               sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
          |                                             ~~~^   ~~~~~~~~~~~~
          |                                                |         |
          |                                                |         int
          |                                                unsigned int
          |                                             %02x

This is harmless, but I like having -Wformat enabled sometimes.
Therefore I have pushed the attached patch to add casts to silence this.

Collin

>From f02f52cac5699af5e996034bcc40b1e5944ecf94 Mon Sep 17 00:00:00 2001
Message-ID: <f02f52cac5699af5e996034bcc40b1e5944ecf94.1748284664.git.collin.fu...@gmail.com>
From: Collin Funk <collin.fu...@gmail.com>
Date: Mon, 26 May 2025 11:27:34 -0700
Subject: [PATCH] crypto/gc: Pacify -Wformat warnings.

* lib/gc-gnulib.c (gc_cipher_setkey, gc_cipher_setiv): Cast the argument
since "%02x" expects the argument to be unsigned.
---
 ChangeLog       | 6 ++++++
 lib/gc-gnulib.c | 6 ++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bf5b782640..f90d007c30 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-05-26  Collin Funk  <collin.fu...@gmail.com>
+
+	crypto/gc: Pacify -Wformat warnings.
+	* lib/gc-gnulib.c (gc_cipher_setkey, gc_cipher_setiv): Cast the argument
+	since "%02x" expects the argument to be unsigned.
+
 2025-05-25  Bruno Haible  <br...@clisp.org>
 
 	stddef-h tests: Add more C++ tests.
diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c
index 4eaf9a7656..85b7a62b74 100644
--- a/lib/gc-gnulib.c
+++ b/lib/gc-gnulib.c
@@ -290,7 +290,8 @@ gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
         char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
 
         for (i = 0; i < keylen; i++)
-          sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
+          sprintf (&keyMaterial[2 * i], "%02x",
+                   (unsigned int) (key[i] & 0xFF));
 
         rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
                               keylen * 8, keyMaterial);
@@ -348,7 +349,8 @@ gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
             char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
 
             for (i = 0; i < ivlen; i++)
-              sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
+              sprintf (&ivMaterial[2 * i], "%02x",
+                       (unsigned int) (iv[i] & 0xFF));
 
             rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
                                      ivMaterial);
-- 
2.49.0

Reply via email to