Hi Bruno,

After the recent changes to mbrtowc and mbrtoc32 I noticed some warnings
when building coreutils. Here are the steps to reproduce it in a
testdir:

    $ gnulib-tool --create-testdir --dir testdir1 mbrtowc mbrtoc32
    $ cd testdir1
    $ sed -i '/gl_INIT/aAC_DEFINE([GNULIB_WCHAR_SINGLE_LOCALE], [1], [a])' 
configure.ac
    $ autoreconf -fvi
    $ ./configure CFLAGS='-Wshadow'
    $ make
    [...]
    gcc -DHAVE_CONFIG_H -I. -I..  -DGNULIB_STRICT_CHECKING=1   -Wshadow -MT 
mbrtoc32.o -MD -MP -MF .deps/mbrtoc32.Tpo -c -o mbrtoc32.o mbrtoc32.c
    mbrtoc32.c: In function ‘rpl_mbrtoc32’:
    mbrtoc32.c:228:23: warning: declaration of ‘c’ shadows a previous local 
[-Wshadow]
      228 |         unsigned char c = (unsigned char) p[0];
          |                       ^
    In file included from mbrtoc32.c:214:
    mbrtowc-impl-utf8.h:23:23: note: shadowed declaration is here
       23 |         unsigned char c = (unsigned char) p[0];
          |                       ^
    mv -f .deps/mbrtoc32.Tpo .deps/mbrtoc32.Po
    gcc -DHAVE_CONFIG_H -I. -I..  -DGNULIB_STRICT_CHECKING=1   -Wshadow -MT 
mbrtowc.o -MD -MP -MF .deps/mbrtowc.Tpo -c -o mbrtowc.o mbrtowc.c
    mbrtowc.c: In function ‘rpl_mbrtowc’:
    mbrtowc.c:213:23: warning: declaration of ‘c’ shadows a previous local 
[-Wshadow]
      213 |         unsigned char c = (unsigned char) p[0];
          |                       ^
    In file included from mbrtowc.c:199:
    mbrtowc-impl-utf8.h:23:23: note: shadowed declaration is here
       23 |         unsigned char c = (unsigned char) p[0];
          |                       ^
    mv -f .deps/mbrtowc.Tpo .deps/mbrtowc.Po


The code is fine as-is, but I like to avoid shadowing since it can
sometimes cause confusion.

Instinctively, I renamed the variable the "c1" instead of "c" which
works. However, I realized afterwards we can just enclose the file in
its own scope, which seems more defensive against future changes causing
similar warnings to this.

What do you think? Proposed patch attached.

Collin

>From e6219bcdce5f4aee58f332ec1485af334e5ee6e1 Mon Sep 17 00:00:00 2001
Message-ID: <e6219bcdce5f4aee58f332ec1485af334e5ee6e1.1775453699.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Sun, 5 Apr 2026 22:19:37 -0700
Subject: [PATCH] mbrtowc, mbrtoc32: Silence -Wshadow warnings (regr.
 2026-04-02).

* lib/mbrtowc-impl-utf8.h: Add brackets around the code so variables are
not added to the scope where this file is included.
---
 ChangeLog               | 6 ++++++
 lib/mbrtowc-impl-utf8.h | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 0decc21186..177762bfc8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2026-04-05  Collin Funk  <[email protected]>
+
+	mbrtowc, mbrtoc32: Silence -Wshadow warnings (regr. 2026-04-02).
+	* lib/mbrtowc-impl-utf8.h: Add brackets around the code so variables are
+	not added to the scope where this file is included.
+
 2026-04-05  Paul Eggert  <[email protected]>
 
 	term-style-control: use pthread_sigmask
diff --git a/lib/mbrtowc-impl-utf8.h b/lib/mbrtowc-impl-utf8.h
index ad96ca84d9..4efd0ad76b 100644
--- a/lib/mbrtowc-impl-utf8.h
+++ b/lib/mbrtowc-impl-utf8.h
@@ -19,6 +19,7 @@
 /* This file contains the part of the body of the mbrtowc and mbrtoc32 functions
    that handles the special case of the UTF-8 encoding.  */
 
+      {
         /* Cf. unistr/u8-mbtouc.c.  */
         unsigned char c = (unsigned char) p[0];
 
@@ -136,3 +137,4 @@
               }
           }
         goto invalid;
+      }
-- 
2.53.0

Reply via email to