Skip to contentSkip to conversation

xfs: use struct_size() helper

PATCH Darrick J. Wong 2 participants 3 messages Apr 12, 2019 1 message not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

[fixing linux-xfs cc]

Quoted 10 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes, in particular in the context in which this code is being used.

So, replace code of the following form:

sizeof(*alist) + context->count * sizeof(alist->al_offset[0]

with:

Quoted 7 lines

struct_size(alist, al_offset, context->count)

and remove unnecessary variable arraytop.

This code was detected with the help of Coccinelle.

Has this been run through xfstests?

--D

Quoted 30 lines
 fs/xfs/xfs_attr_list.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
fs/xfs/xfs_attr_list.c +1 -4
index 3d213a7394c5..05e03348553e 100644
@@ -553,7 +553,6 @@ xfs_attr_put_listent(
 {
 	struct attrlist *alist = (struct attrlist *)context->alist;
 	attrlist_ent_t *aep;
-	int arraytop;
 
 	ASSERT(!context->seen_enough);
 	ASSERT(!(context->flags & ATTR_KERNOVAL));
@@ -572,10 +571,8 @@ xfs_attr_put_listent(
 	    ((flags & XFS_ATTR_ROOT) == 0))
 		return;
 
-	arraytop = sizeof(*alist) +
-			context->count * sizeof(alist->al_offset[0]);
 	context->firstu -= ATTR_ENTSIZE(namelen);
-	if (context->firstu < arraytop) {
+	if (context->firstu < struct_size(alist, al_offset, context->count)) {
 		trace_xfs_attr_list_full(context);
 		alist->al_more = 1;
 		context->seen_enough = 1;
Signature · 1 line
2.21.0
#2 lore raw
Quoted 2 lines · Darrick J. Wong

[fixing linux-xfs cc]

Thanks for this.

Quoted 13 lines · Gustavo A. R. Silva
Quoted 12 lines

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes, in particular in the context in which this code is being used.

So, replace code of the following form:

sizeof(*alist) + context->count * sizeof(alist->al_offset[0]

with:

struct_size(alist, al_offset, context->count)

Quoted 8 lines
Quoted 5 lines

and remove unnecessary variable arraytop.

This code was detected with the help of Coccinelle.

Has this been run through xfstests?

No. Is this a requirement?

Thanks
--
Gustavo

Quoted 32 lines

--D

Quoted 30 lines
 fs/xfs/xfs_attr_list.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
fs/xfs/xfs_attr_list.c +1 -4
index 3d213a7394c5..05e03348553e 100644
@@ -553,7 +553,6 @@ xfs_attr_put_listent(
 {
 	struct attrlist *alist = (struct attrlist *)context->alist;
 	attrlist_ent_t *aep;
-	int arraytop;
 
 	ASSERT(!context->seen_enough);
 	ASSERT(!(context->flags & ATTR_KERNOVAL));
@@ -572,10 +571,8 @@ xfs_attr_put_listent(
 	    ((flags & XFS_ATTR_ROOT) == 0))
 		return;
 
-	arraytop = sizeof(*alist) +
-			context->count * sizeof(alist->al_offset[0]);
 	context->firstu -= ATTR_ENTSIZE(namelen);
-	if (context->firstu < arraytop) {
+	if (context->firstu < struct_size(alist, al_offset, context->count)) {
 		trace_xfs_attr_list_full(context);
 		alist->al_more = 1;
 		context->seen_enough = 1;
Signature · 1 line
2.21.0
#3 lore raw
Quoted 23 lines · Gustavo A. R. Silva
Quoted 2 lines · Darrick J. Wong

[fixing linux-xfs cc]

Thanks for this.

Quoted 15 lines · Gustavo A. R. Silva
Quoted 14 lines

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes, in particular in the context in which this code is being used.

So, replace code of the following form:

sizeof(*alist) + context->count * sizeof(alist->al_offset[0]

with:

struct_size(alist, al_offset, context->count)

and remove unnecessary variable arraytop.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

This code was detected with the help of Coccinelle.

Has this been run through xfstests?

No. Is this a requirement?

Yes, since this is a change to core functionality (and my eyes aren't so good at digging through preprocessor macros...)

--D

Quoted 36 lines

Thanks
--
Gustavo

Quoted 32 lines

--D

Quoted 30 lines
 fs/xfs/xfs_attr_list.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
fs/xfs/xfs_attr_list.c +1 -4
index 3d213a7394c5..05e03348553e 100644
@@ -553,7 +553,6 @@ xfs_attr_put_listent(
 {
 	struct attrlist *alist = (struct attrlist *)context->alist;
 	attrlist_ent_t *aep;
-	int arraytop;
 
 	ASSERT(!context->seen_enough);
 	ASSERT(!(context->flags & ATTR_KERNOVAL));
@@ -572,10 +571,8 @@ xfs_attr_put_listent(
 	    ((flags & XFS_ATTR_ROOT) == 0))
 		return;
 
-	arraytop = sizeof(*alist) +
-			context->count * sizeof(alist->al_offset[0]);
 	context->firstu -= ATTR_ENTSIZE(namelen);
-	if (context->firstu < arraytop) {
+	if (context->firstu < struct_size(alist, al_offset, context->count)) {
 		trace_xfs_attr_list_full(context);
 		alist->al_more = 1;
 		context->seen_enough = 1;
Signature · 1 line
2.21.0