[PATCH 5/6] Cygwin: Fix warnings about narrowing conversions of socket ioctls

Jon Turney jon.turney@dronecode.org.uk
Tue Aug 6 18:58:54 GMT 2024


On 05/08/2024 11:22, Corinna Vinschen wrote:
> On Aug  4 22:48, Jon Turney wrote:
>> Fix gcc 12 warnings about narrowing conversions of socket ioctl constants
>> when used as case labels, e.g:
>>
>>> ../../../../src/winsup/cygwin/net.cc: In function ‘int get_ifconf(ifconf*, int)’:
>>> ../../../../src/winsup/cygwin/net.cc:1940:18: error: narrowing conversion of ‘2152756069’ from ‘long int’ to ‘int’ [-Wnarrowing]
>> ---
>>   winsup/cygwin/net.cc | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
>> index 08c584fe5..b76af2d19 100644
>> --- a/winsup/cygwin/net.cc
>> +++ b/winsup/cygwin/net.cc
>> @@ -1935,7 +1935,7 @@ get_ifconf (struct ifconf *ifc, int what)
>>   	{
>>   	  ++cnt;
>>   	  strcpy (ifr->ifr_name, ifp->ifa_name);
>> -	  switch (what)
>> +	  switch ((long int)what)
>>   	    {
>>   	    case SIOCGIFFLAGS:
>>   	      ifr->ifr_flags = ifp->ifa_ifa.ifa_flags;
>> -- 
>> 2.45.1
> 
> The only caller, fhandler_socket::ioctl, passes an unsigned int
> value to get_ifconf. Given how the value is defined, it would be
> more straightforward to convert get_ifconf to
> 
>    get_ifconf (struct ifconf *ifc, unsigned int what);
> 
> wouldn't it?

Yeah, I'm not sure why I didn't do that.  I think I got confused about 
where this is used from.

(These constants are long int though, for whatever reason, so it's not 
immediately obvious that they all can be converted to unsigned int 
without loss, but it seems they can)

Revised patch attached.
-------------- next part --------------
From df8b1ffd6ef7a9808a2d5eebf4b6416d45841e36 Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Sun, 4 Aug 2024 17:02:00 +0100
Subject: [PATCH] Cygwin: Fix warnings about narrowing conversions of socket
 ioctls
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix gcc 12 warnings about narrowing conversions of socket ioctl constants
when used as case labels, e.g:

> ../../../../src/winsup/cygwin/net.cc: In function ‘int get_ifconf(ifconf*, int)’:
> ../../../../src/winsup/cygwin/net.cc:1940:18: error: narrowing conversion of ‘2152756069’ from ‘long int’ to ‘int’ [-Wnarrowing]

Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
---
 winsup/cygwin/fhandler/socket.cc | 2 +-
 winsup/cygwin/net.cc             | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/socket.cc b/winsup/cygwin/fhandler/socket.cc
index f7c5ff629..c0cef7d3e 100644
--- a/winsup/cygwin/fhandler/socket.cc
+++ b/winsup/cygwin/fhandler/socket.cc
@@ -86,7 +86,7 @@ struct __old_ifreq {
 int
 fhandler_socket::ioctl (unsigned int cmd, void *p)
 {
-  extern int get_ifconf (struct ifconf *ifc, int what); /* net.cc */
+  extern int get_ifconf (struct ifconf *ifc, unsigned int what); /* net.cc */
   int res;
   struct ifconf ifc, *ifcp;
   struct ifreq *ifrp;
diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
index 08c584fe5..737e494f8 100644
--- a/winsup/cygwin/net.cc
+++ b/winsup/cygwin/net.cc
@@ -1912,7 +1912,7 @@ freeifaddrs (struct ifaddrs *ifp)
 }
 
 int
-get_ifconf (struct ifconf *ifc, int what)
+get_ifconf (struct ifconf *ifc, unsigned int what)
 {
   __try
     {
-- 
2.45.1



More information about the Cygwin-patches mailing list