diff mbox series

[V2,3/4] xfs: Use variable-size array for nameval in xfs_attr_sf_entry

Message ID 20200902144059.284726-4-cmaiolino@redhat.com (mailing list archive)
State Superseded
Headers show
Series Clean up xfs_attr_sf_entry | expand

Commit Message

Carlos Maiolino Sept. 2, 2020, 2:40 p.m. UTC
nameval is a variable-size array, so, define it as it, and remove all
the -1 magic number subtractions

Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
---
Changelog:

	V2:
	 - Drop wrong change to XFS_ATTR_SF_ENTSIZE_MAX

 fs/xfs/libxfs/xfs_attr_leaf.c | 6 ++----
 fs/xfs/libxfs/xfs_attr_sf.h   | 4 ++--
 fs/xfs/libxfs/xfs_da_format.h | 2 +-
 3 files changed, 5 insertions(+), 7 deletions(-)

Comments

Darrick J. Wong Sept. 2, 2020, 5:52 p.m. UTC | #1
On Wed, Sep 02, 2020 at 04:40:58PM +0200, Carlos Maiolino wrote:
> nameval is a variable-size array, so, define it as it, and remove all
> the -1 magic number subtractions
> 
> Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
> ---
> Changelog:
> 
> 	V2:
> 	 - Drop wrong change to XFS_ATTR_SF_ENTSIZE_MAX
> 
>  fs/xfs/libxfs/xfs_attr_leaf.c | 6 ++----
>  fs/xfs/libxfs/xfs_attr_sf.h   | 4 ++--
>  fs/xfs/libxfs/xfs_da_format.h | 2 +-
>  3 files changed, 5 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/xfs/libxfs/xfs_attr_leaf.c b/fs/xfs/libxfs/xfs_attr_leaf.c
> index d920183b08a99..89193871e6a7f 100644
> --- a/fs/xfs/libxfs/xfs_attr_leaf.c
> +++ b/fs/xfs/libxfs/xfs_attr_leaf.c
> @@ -992,7 +992,7 @@ xfs_attr_shortform_allfit(
>  			return 0;
>  		if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
>  			return 0;
> -		bytes += sizeof(struct xfs_attr_sf_entry) - 1
> +		bytes += sizeof(struct xfs_attr_sf_entry)
>  				+ name_loc->namelen
>  				+ be16_to_cpu(name_loc->valuelen);

Isn't this an open-coded:

		bytes += XFS_ATTR_SF_ENTSIZE_BYNAME(name_loc->namelen,
					be16_to_cpu(name_loc->valuelen));

Otherwise this patch looks ok to me.

--D

>  	}
> @@ -1036,10 +1036,8 @@ xfs_attr_shortform_verify(
>  		 * struct xfs_attr_sf_entry has a variable length.
>  		 * Check the fixed-offset parts of the structure are
>  		 * within the data buffer.
> -		 * xfs_attr_sf_entry is defined with a 1-byte variable
> -		 * array at the end, so we must subtract that off.
>  		 */
> -		if (((char *)sfep + sizeof(*sfep) - 1) >= endp)
> +		if (((char *)sfep + sizeof(*sfep)) >= endp)
>  			return __this_address;
>  
>  		/* Don't allow names with known bad length. */
> diff --git a/fs/xfs/libxfs/xfs_attr_sf.h b/fs/xfs/libxfs/xfs_attr_sf.h
> index c4afb33079184..f608a2966d7f8 100644
> --- a/fs/xfs/libxfs/xfs_attr_sf.h
> +++ b/fs/xfs/libxfs/xfs_attr_sf.h
> @@ -27,11 +27,11 @@ typedef struct xfs_attr_sf_sort {
>  } xfs_attr_sf_sort_t;
>  
>  #define XFS_ATTR_SF_ENTSIZE_BYNAME(nlen,vlen)	/* space name/value uses */ \
> -	(((int)sizeof(struct xfs_attr_sf_entry)-1 + (nlen)+(vlen)))
> +	(((int)sizeof(struct xfs_attr_sf_entry) + (nlen)+(vlen)))
>  #define XFS_ATTR_SF_ENTSIZE_MAX			/* max space for name&value */ \
>  	((1 << (NBBY*(int)sizeof(uint8_t))) - 1)
>  #define XFS_ATTR_SF_ENTSIZE(sfep)		/* space an entry uses */ \
> -	((int)sizeof(struct xfs_attr_sf_entry)-1 + \
> +	((int)sizeof(struct xfs_attr_sf_entry) + \
>  		(sfep)->namelen+(sfep)->valuelen)
>  #define XFS_ATTR_SF_NEXTENTRY(sfep)		/* next entry in struct */ \
>  	((struct xfs_attr_sf_entry *)((char *)(sfep) + \
> diff --git a/fs/xfs/libxfs/xfs_da_format.h b/fs/xfs/libxfs/xfs_da_format.h
> index e708b714bf99d..b40a4e80f5ee6 100644
> --- a/fs/xfs/libxfs/xfs_da_format.h
> +++ b/fs/xfs/libxfs/xfs_da_format.h
> @@ -589,7 +589,7 @@ struct xfs_attr_shortform {
>  		uint8_t namelen;	/* actual length of name (no NULL) */
>  		uint8_t valuelen;	/* actual length of value (no NULL) */
>  		uint8_t flags;	/* flags bits (see xfs_attr_leaf.h) */
> -		uint8_t nameval[1];	/* name & value bytes concatenated */
> +		uint8_t nameval[];	/* name & value bytes concatenated */
>  	} list[1];			/* variable sized array */
>  };
>  
> -- 
> 2.26.2
>
Christoph Hellwig Sept. 3, 2020, 9:11 a.m. UTC | #2
On Wed, Sep 02, 2020 at 04:40:58PM +0200, Carlos Maiolino wrote:
> nameval is a variable-size array, so, define it as it, and remove all
> the -1 magic number subtractions

Looks good, but two little style nitpicks below.

Reviewed-by: Christoph Hellwig <hch@lst.de>

>  		if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
>  			return 0;
> -		bytes += sizeof(struct xfs_attr_sf_entry) - 1
> +		bytes += sizeof(struct xfs_attr_sf_entry)
>  				+ name_loc->namelen
>  				+ be16_to_cpu(name_loc->valuelen);

This can be condensed to:

		bytes += sizeof(struct xfs_attr_sf_entry) + name_loc->namelen +
 				be16_to_cpu(name_loc->valuelen);

> index c4afb33079184..f608a2966d7f8 100644
> --- a/fs/xfs/libxfs/xfs_attr_sf.h
> +++ b/fs/xfs/libxfs/xfs_attr_sf.h
> @@ -27,11 +27,11 @@ typedef struct xfs_attr_sf_sort {
>  } xfs_attr_sf_sort_t;
>  
>  #define XFS_ATTR_SF_ENTSIZE_BYNAME(nlen,vlen)	/* space name/value uses */ \
> -	(((int)sizeof(struct xfs_attr_sf_entry)-1 + (nlen)+(vlen)))
> +	(((int)sizeof(struct xfs_attr_sf_entry) + (nlen)+(vlen)))

We can drop the int cast, but more importantly please add whitespaces
before and after the + operator.
diff mbox series

Patch

diff --git a/fs/xfs/libxfs/xfs_attr_leaf.c b/fs/xfs/libxfs/xfs_attr_leaf.c
index d920183b08a99..89193871e6a7f 100644
--- a/fs/xfs/libxfs/xfs_attr_leaf.c
+++ b/fs/xfs/libxfs/xfs_attr_leaf.c
@@ -992,7 +992,7 @@  xfs_attr_shortform_allfit(
 			return 0;
 		if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
 			return 0;
-		bytes += sizeof(struct xfs_attr_sf_entry) - 1
+		bytes += sizeof(struct xfs_attr_sf_entry)
 				+ name_loc->namelen
 				+ be16_to_cpu(name_loc->valuelen);
 	}
@@ -1036,10 +1036,8 @@  xfs_attr_shortform_verify(
 		 * struct xfs_attr_sf_entry has a variable length.
 		 * Check the fixed-offset parts of the structure are
 		 * within the data buffer.
-		 * xfs_attr_sf_entry is defined with a 1-byte variable
-		 * array at the end, so we must subtract that off.
 		 */
-		if (((char *)sfep + sizeof(*sfep) - 1) >= endp)
+		if (((char *)sfep + sizeof(*sfep)) >= endp)
 			return __this_address;
 
 		/* Don't allow names with known bad length. */
diff --git a/fs/xfs/libxfs/xfs_attr_sf.h b/fs/xfs/libxfs/xfs_attr_sf.h
index c4afb33079184..f608a2966d7f8 100644
--- a/fs/xfs/libxfs/xfs_attr_sf.h
+++ b/fs/xfs/libxfs/xfs_attr_sf.h
@@ -27,11 +27,11 @@  typedef struct xfs_attr_sf_sort {
 } xfs_attr_sf_sort_t;
 
 #define XFS_ATTR_SF_ENTSIZE_BYNAME(nlen,vlen)	/* space name/value uses */ \
-	(((int)sizeof(struct xfs_attr_sf_entry)-1 + (nlen)+(vlen)))
+	(((int)sizeof(struct xfs_attr_sf_entry) + (nlen)+(vlen)))
 #define XFS_ATTR_SF_ENTSIZE_MAX			/* max space for name&value */ \
 	((1 << (NBBY*(int)sizeof(uint8_t))) - 1)
 #define XFS_ATTR_SF_ENTSIZE(sfep)		/* space an entry uses */ \
-	((int)sizeof(struct xfs_attr_sf_entry)-1 + \
+	((int)sizeof(struct xfs_attr_sf_entry) + \
 		(sfep)->namelen+(sfep)->valuelen)
 #define XFS_ATTR_SF_NEXTENTRY(sfep)		/* next entry in struct */ \
 	((struct xfs_attr_sf_entry *)((char *)(sfep) + \
diff --git a/fs/xfs/libxfs/xfs_da_format.h b/fs/xfs/libxfs/xfs_da_format.h
index e708b714bf99d..b40a4e80f5ee6 100644
--- a/fs/xfs/libxfs/xfs_da_format.h
+++ b/fs/xfs/libxfs/xfs_da_format.h
@@ -589,7 +589,7 @@  struct xfs_attr_shortform {
 		uint8_t namelen;	/* actual length of name (no NULL) */
 		uint8_t valuelen;	/* actual length of value (no NULL) */
 		uint8_t flags;	/* flags bits (see xfs_attr_leaf.h) */
-		uint8_t nameval[1];	/* name & value bytes concatenated */
+		uint8_t nameval[];	/* name & value bytes concatenated */
 	} list[1];			/* variable sized array */
 };