From 7ecb9e528eba34c23fb9e64cf1c73f0386faade3 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Mon, 10 Mar 2025 17:28:33 +0100
Subject: [PATCH] quotearg: Avoid undefined behaviour.

Reported by Kirill Furman <kfurman@astralinux.ru> in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-03/msg00037.html>.

* lib/quotearg.c (set_char_quoting): Use 'unsigned int', not 'int', for
doing bit mask operations.
---
 ChangeLog      | 8 ++++++++
 lib/quotearg.c | 4 ++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3f80b9a2b4..329f4e424e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-03-10  Bruno Haible  <bruno@clisp.org>
+
+	quotearg: Avoid undefined behaviour.
+	Reported by Kirill Furman <kfurman@astralinux.ru> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2025-03/msg00037.html>.
+	* lib/quotearg.c (set_char_quoting): Use 'unsigned int', not 'int', for
+	doing bit mask operations.
+
 2025-03-09  Bruno Haible  <bruno@clisp.org>
 
 	getlogin_r: Work around musl bug.
diff --git a/lib/quotearg.c b/lib/quotearg.c
index d7b549e478..6bf0f5a905 100644
--- a/lib/quotearg.c
+++ b/lib/quotearg.c
@@ -147,8 +147,8 @@ set_char_quoting (struct quoting_options *o, char c, int i)
   unsigned int *p =
     (o ? o : &default_quoting_options)->quote_these_too + uc / INT_BITS;
   int shift = uc % INT_BITS;
-  int r = (*p >> shift) & 1;
-  *p ^= ((i & 1) ^ r) << shift;
+  unsigned int r = (*p >> shift) & 1;
+  *p ^= ((i & 1U) ^ r) << shift;
   return r;
 }
 
-- 
2.39.5