[libvirt] [RFC] virfile: fix cast-align error

Marc Hartmayer posted 1 patch 5 years, 6 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20181008184124.28352-1-mhartmay@linux.ibm.com
Test syntax-check passed
There is a newer version of this series
src/util/virfile.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[libvirt] [RFC] virfile: fix cast-align error
Posted by Marc Hartmayer 5 years, 6 months ago
Use the correct type in order to fix the following error on s390x:

In function 'virFileIsSharedFSType':
../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
         virFileIsSharedFixFUSE(path, (long *) &sb.f_type);

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
---
 src/util/virfile.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 2a7e87102a25..832d832696d5 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -3466,7 +3466,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
 
 static int
 virFileIsSharedFixFUSE(const char *path,
-                       long *f_type)
+                       unsigned int *f_type)
 {
     char *dirpath = NULL;
     const char **mounts = NULL;
@@ -3575,7 +3575,7 @@ virFileIsSharedFSType(const char *path,
 
     if (sb.f_type == FUSE_SUPER_MAGIC) {
         VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
-        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
+        virFileIsSharedFixFUSE(path, &sb.f_type);
     }
 
     VIR_DEBUG("Check if path %s with FS magic %lld is shared",
-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC] virfile: fix cast-align error
Posted by Bjoern Walk 5 years, 6 months ago
Marc Hartmayer <mhartmay@linux.ibm.com> [2018-10-08, 08:41PM +0200]:
> Use the correct type in order to fix the following error on s390x:
> 
> In function 'virFileIsSharedFSType':
> ../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
>          virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> 
> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> ---
>  src/util/virfile.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 2a7e87102a25..832d832696d5 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -3466,7 +3466,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
>  
>  static int
>  virFileIsSharedFixFUSE(const char *path,
> -                       long *f_type)
> +                       unsigned int *f_type)
>  {
>      char *dirpath = NULL;
>      const char **mounts = NULL;
> @@ -3575,7 +3575,7 @@ virFileIsSharedFSType(const char *path,
>  
>      if (sb.f_type == FUSE_SUPER_MAGIC) {
>          VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
> -        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> +        virFileIsSharedFixFUSE(path, &sb.f_type);
>      }

Using an unsigned int is fine as per statfs(2):

    NOTES
       The __fsword_t type used for various fields in the statfs structure defini‐
       tion is a glibc internal type, not intended for public  use.   This  leaves
       the programmer in a bit of a conundrum when trying to copy or compare these
       fields to local variables in a program.  Using unsigned int for such  vari‐
       ables suffices on most systems.

I would prefer an explicit cast though.

>  
>      VIR_DEBUG("Check if path %s with FS magic %lld is shared",
>                path, (long long int)sb.f_type);

Should we also fix this cast?

Reviewed-by: Bjoern Walk <bwalk@linux.ibm.com>

-- 
IBM Systems
Linux on Z & Virtualization Development
--------------------------------------------------
IBM Deutschland Research & Development GmbH
Schönaicher Str. 220, 71032 Böblingen
Phone: +49 7031 16 1819
--------------------------------------------------
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294 
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC] virfile: fix cast-align error
Posted by Michal Privoznik 5 years, 6 months ago
On 10/08/2018 08:41 PM, Marc Hartmayer wrote:
> Use the correct type in order to fix the following error on s390x:
> 
> In function 'virFileIsSharedFSType':
> ../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
>          virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> 
> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> ---
>  src/util/virfile.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 2a7e87102a25..832d832696d5 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -3466,7 +3466,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
>  
>  static int
>  virFileIsSharedFixFUSE(const char *path,
> -                       long *f_type)
> +                       unsigned int *f_type)
>  {
>      char *dirpath = NULL;
>      const char **mounts = NULL;
> @@ -3575,7 +3575,7 @@ virFileIsSharedFSType(const char *path,
>  
>      if (sb.f_type == FUSE_SUPER_MAGIC) {
>          VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
> -        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> +        virFileIsSharedFixFUSE(path, &sb.f_type);

This won't fly on x86_64 where f_type is long. I think we can use
__fsword_t directly.

>      }
>  
>      VIR_DEBUG("Check if path %s with FS magic %lld is shared",
> 

Does that work for you?

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC] virfile: fix cast-align error
Posted by Bjoern Walk 5 years, 6 months ago
Michal Privoznik <mprivozn@redhat.com> [2018-10-09, 03:45PM +0200]:
> On 10/08/2018 08:41 PM, Marc Hartmayer wrote:
> > Use the correct type in order to fix the following error on s390x:
> > 
> > In function 'virFileIsSharedFSType':
> > ../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
> >          virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> > 
> > Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> > ---
> >  src/util/virfile.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/src/util/virfile.c b/src/util/virfile.c
> > index 2a7e87102a25..832d832696d5 100644
> > --- a/src/util/virfile.c
> > +++ b/src/util/virfile.c
> > @@ -3466,7 +3466,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
> >  
> >  static int
> >  virFileIsSharedFixFUSE(const char *path,
> > -                       long *f_type)
> > +                       unsigned int *f_type)
> >  {
> >      char *dirpath = NULL;
> >      const char **mounts = NULL;
> > @@ -3575,7 +3575,7 @@ virFileIsSharedFSType(const char *path,
> >  
> >      if (sb.f_type == FUSE_SUPER_MAGIC) {
> >          VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
> > -        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> > +        virFileIsSharedFixFUSE(path, &sb.f_type);
> 
> This won't fly on x86_64 where f_type is long. I think we can use
> __fsword_t directly.
> 
> >      }
> >  
> >      VIR_DEBUG("Check if path %s with FS magic %lld is shared",
> > 
> 
> Does that work for you?

Hmm, __fsword_t is still a long int but the f_type member is defined as
unsigned int in the userspace header. Also, I am against exposing any
internal data types.

virFileIsSharedFixFUSE could just return the value, the return value
right now is not checked anyways.

> 
> Michal
> 
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
> 

-- 
IBM Systems
Linux on Z & Virtualization Development
--------------------------------------------------
IBM Deutschland Research & Development GmbH
Schönaicher Str. 220, 71032 Böblingen
Phone: +49 7031 16 1819
--------------------------------------------------
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294 
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC] virfile: fix cast-align error
Posted by Michal Privoznik 5 years, 6 months ago
On 10/09/2018 04:39 PM, Bjoern Walk wrote:
> Michal Privoznik <mprivozn@redhat.com> [2018-10-09, 03:45PM +0200]:
>> On 10/08/2018 08:41 PM, Marc Hartmayer wrote:
>>> Use the correct type in order to fix the following error on s390x:
>>>
>>> In function 'virFileIsSharedFSType':
>>> ../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
>>>          virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
>>>
>>> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
>>> ---
>>>  src/util/virfile.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/src/util/virfile.c b/src/util/virfile.c
>>> index 2a7e87102a25..832d832696d5 100644
>>> --- a/src/util/virfile.c
>>> +++ b/src/util/virfile.c
>>> @@ -3466,7 +3466,7 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
>>>  
>>>  static int
>>>  virFileIsSharedFixFUSE(const char *path,
>>> -                       long *f_type)
>>> +                       unsigned int *f_type)
>>>  {
>>>      char *dirpath = NULL;
>>>      const char **mounts = NULL;
>>> @@ -3575,7 +3575,7 @@ virFileIsSharedFSType(const char *path,
>>>  
>>>      if (sb.f_type == FUSE_SUPER_MAGIC) {
>>>          VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
>>> -        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
>>> +        virFileIsSharedFixFUSE(path, &sb.f_type);
>>
>> This won't fly on x86_64 where f_type is long. I think we can use
>> __fsword_t directly.
>>
>>>      }
>>>  
>>>      VIR_DEBUG("Check if path %s with FS magic %lld is shared",
>>>
>>
>> Does that work for you?
> 
> Hmm, __fsword_t is still a long int but the f_type member is defined as
> unsigned int in the userspace header.

What header file are you looking at? From /usr/include/bits/statfs.h:

struct statfs
  {
    __fsword_t f_type;
    __fsword_t f_bsize;

/* ..... */

    __fsid_t f_fsid;
    __fsword_t f_namelen;
    __fsword_t f_frsize;
    __fsword_t f_flags;
    __fsword_t f_spare[4];
  };


> Also, I am against exposing any
> internal data types.

It's not internal if it is exposed in a public header file.

> 
> virFileIsSharedFixFUSE could just return the value, the return value
> right now is not checked anyways.

Sure, but since you claim the f_type member is an unsigned type, then we
would hit the sign warning anyway.
Also, not sure. If virFileIsSharedFixFUSE() is unable to tell the
filesystem it should not touch the f_type at all.

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC] virfile: fix cast-align error
Posted by Bjoern Walk 5 years, 6 months ago
Michal Privoznik <mprivozn@redhat.com> [2018-10-09, 04:52PM +0200]:
> What header file are you looking at? From /usr/include/bits/statfs.h:

It's s390x-specific hard-coded as an unsigned int.

> > Also, I am against exposing any
> > internal data types.
> 
> It's not internal if it is exposed in a public header file.
> 
> > 
> > virFileIsSharedFixFUSE could just return the value, the return value
> > right now is not checked anyways.
> 
> Sure, but since you claim the f_type member is an unsigned type, then we
> would hit the sign warning anyway.
> Also, not sure. If virFileIsSharedFixFUSE() is unable to tell the
> filesystem it should not touch the f_type at all.

Ok, then I guess Daniel's solution is the most simple one. I'll have
Marc send a v3 :)

-- 
IBM Systems
Linux on Z & Virtualization Development
--------------------------------------------------
IBM Deutschland Research & Development GmbH
Schönaicher Str. 220, 71032 Böblingen
Phone: +49 7031 16 1819
--------------------------------------------------
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294 
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [RFC v2] virfile: fix cast-align error
Posted by Marc Hartmayer 5 years, 6 months ago
On s390x the struct member f_type of statsfs is hard coded to
'unsigned int'. So let's try to determine the type by means of the GNU
extension typeof.

This fixes the following error:
../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
         virFileIsSharedFixFUSE(path, (long *) &sb.f_type);

Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
---
 src/util/virfile.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 2a7e87102a25..6cde4ab6c23b 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -3464,9 +3464,15 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
 
 # define PROC_MOUNTS "/proc/mounts"
 
+# ifdef __GNUC__
+typedef typeof(((struct statfs*)0)->f_type) f_type_type;
+# else
+typedef long f_type_type;
+# endif
+
 static int
 virFileIsSharedFixFUSE(const char *path,
-                       long *f_type)
+                       f_type_type *f_type)
 {
     char *dirpath = NULL;
     const char **mounts = NULL;
@@ -3575,7 +3581,7 @@ virFileIsSharedFSType(const char *path,
 
     if (sb.f_type == FUSE_SUPER_MAGIC) {
         VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
-        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
+        virFileIsSharedFixFUSE(path, &sb.f_type);
     }
 
     VIR_DEBUG("Check if path %s with FS magic %lld is shared",
-- 
2.17.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [RFC v2] virfile: fix cast-align error
Posted by Daniel P. Berrangé 5 years, 6 months ago
On Tue, Oct 09, 2018 at 05:55:29PM +0200, Marc Hartmayer wrote:
> On s390x the struct member f_type of statsfs is hard coded to
> 'unsigned int'. So let's try to determine the type by means of the GNU
> extension typeof.
> 
> This fixes the following error:
> ../../src/util/virfile.c:3578:38: error: cast increases required alignment of target type [-Werror=cast-align]
>          virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> 
> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> ---
>  src/util/virfile.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 2a7e87102a25..6cde4ab6c23b 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -3464,9 +3464,15 @@ int virFilePrintf(FILE *fp, const char *msg, ...)
>  
>  # define PROC_MOUNTS "/proc/mounts"
>  
> +# ifdef __GNUC__
> +typedef typeof(((struct statfs*)0)->f_type) f_type_type;
> +# else
> +typedef long f_type_type;
> +# endif

These games are rather overkill IMHO.

> +
>  static int
>  virFileIsSharedFixFUSE(const char *path,
> -                       long *f_type)
> +                       f_type_type *f_type)

Make this  'long long'

>  {
>      char *dirpath = NULL;
>      const char **mounts = NULL;
> @@ -3575,7 +3581,7 @@ virFileIsSharedFSType(const char *path,

Add a local variable

   long long f_type;

and then after the stat() call

   f_type = sb.f_type;

and change everything that follows to use the local variable

>      if (sb.f_type == FUSE_SUPER_MAGIC) {
>          VIR_DEBUG("Found FUSE mount for path=%s. Trying to fix it", path);
> -        virFileIsSharedFixFUSE(path, (long *) &sb.f_type);
> +        virFileIsSharedFixFUSE(path, &sb.f_type);
>      }
>  
>      VIR_DEBUG("Check if path %s with FS magic %lld is shared",

This also removes the need to cast to long long in this VIR_DEBUG
call


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list