[I've no idea about this, adding Eric to CC for attention]

On Wed, Feb 15, 2023 at 03:11:42PM +0100, Laszlo Ersek wrote:
> F_SETFD takes an "int", so it stands to reason that FD_CLOEXEC expands to
> an "int". In turn, it's bad hygiene to manipulate the sign bit of (signed)
> integers with bit operations:
> 
>   ~FD_CLOEXEC
> 
> Convert FD_CLOEXEC to "unsigned int" for the bitwise complement operator:
> 
>   ~(unsigned)FD_CLOEXEC
> 
> The bitwise complement then results in an "unsigned int". "Flags" (of type
> "int", and having, per POSIX, a non-negative value returned by
> fcntl(F_GETFD)) will be automatically converted to "unsigned int" by the
> usual arithmetic conversions for the bitwise AND operator:
> 
>   flags & ~(unsigned)FD_CLOEXEC
> 
> We could pass the result of *that* to fcntl(), due to (a) the value being
> in range for "signed int" ("flags" is a non-negative "int", and we're only
> clearing a value bit), and (b) non-negative "int" values being represented
> identically by "unsigned int" (C99 6.2.5 p9). But, for clarity, let's cast
> the result to "int" explicitly:
> 
>   (int)(flags & ~(unsigned)FD_CLOEXEC)
> 
> Signed-off-by: Laszlo Ersek <ler...@redhat.com>
> ---
> 
> Notes:
>     context:-U5
> 
>  generator/states-connect-socket-activation.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/generator/states-connect-socket-activation.c 
> b/generator/states-connect-socket-activation.c
> index b5e146539cc8..729f37d897fb 100644
> --- a/generator/states-connect-socket-activation.c
> +++ b/generator/states-connect-socket-activation.c
> @@ -181,11 +181,11 @@  CONNECT_SA.START:
>        int flags = fcntl (s, F_GETFD, 0);
>        if (flags == -1) {
>          nbd_internal_fork_safe_perror ("fcntl: F_GETFD");
>          _exit (126);
>        }
> -      if (fcntl (s, F_SETFD, flags & ~FD_CLOEXEC) == -1) {
> +      if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) {
>          nbd_internal_fork_safe_perror ("fcntl: F_SETFD");
>          _exit (126);
>        }
>      }
>  
> 
> _______________________________________________
> Libguestfs mailing list
> Libguestfs@redhat.com
> https://listman.redhat.com/mailman/listinfo/libguestfs

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
_______________________________________________
Libguestfs mailing list
Libguestfs@redhat.com
https://listman.redhat.com/mailman/listinfo/libguestfs

Reply via email to