Skip to contentSkip to conversation

RDMA/ucma: Use struct_size() helper

PATCH Applied Gustavo A. R. Silva 4 participants 4 messages Jun 4, 2019 – Jun 14, 2019 InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RDMA/ucma: Use struct_size() helper

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 the following form:

sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))

with:

struct_size(resp, path_data, i)

This code was detected with the help of Coccinelle.

---
 drivers/infiniband/core/ucma.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/infiniband/core/ucma.c +1 -2
index 140a338a135f..cbe460076611 100644
@@ -951,8 +951,7 @@ static ssize_t ucma_query_path(struct ucma_context *ctx,
 		}
 	}
 
-	if (copy_to_user(response, resp,
-			 sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
+	if (copy_to_user(response, resp, struct_size(resp, path_data, i)))
 		ret = -EFAULT;
 
 	kfree(resp);
Signature · 1 line
2.21.0
#1 lore raw
Quoted 3 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.

What does "in particular in the context in which this code is being used" mean?

Quoted 8 lines

So, replace the following form:

sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))

with:

struct_size(resp, path_data, i)

It is already written inside commit itself.

Quoted 25 lines

This code was detected with the help of Coccinelle.

---
 drivers/infiniband/core/ucma.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/infiniband/core/ucma.c +1 -3
index 140a338a135f..cbe460076611 100644
@@ -951,8 +951,7 @@ static ssize_t ucma_query_path(struct ucma_context *ctx,
 		}
 	}
 
-	if (copy_to_user(response, resp,
-			 sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
+	if (copy_to_user(response, resp, struct_size(resp, path_data, i)))
 		ret = -EFAULT;
 
 	kfree(resp);
--

2.21.0

#2 lore raw
In reply to Gustavo A. R. Silva
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 the following form:

sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))

with:

Quoted 8 lines

struct_size(resp, path_data, i)

This code was detected with the help of Coccinelle.

---
 drivers/infiniband/core/ucma.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Applied to for-next, thanks

Jason

#3 lore raw
In reply to Gustavo A. R. Silva

On 04.06.19 17:42, Gustavo A. R. Silva wrote:

Hi,

<snip>

drivers/infiniband/core/ucma.c static ssize_t ucma_query_path(struct ucma_context *ctx, · line 955

Quoted 12 lines
drivers/infiniband/core/ucma.c +1 -2
index 140a338a135f..cbe460076611 100644
@@ -951,8 +951,7 @@ static ssize_t ucma_query_path(struct ucma_context *ctx,
 		}
 	}
 
-	if (copy_to_user(response, resp,
-			 sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
+	if (copy_to_user(response, resp, struct_size(resp, path_data, i)))
 		ret = -EFAULT;

have you already considered further reducing the boilerplate by putting this into it's own helper macro, so it finally would look like this ?

+ if (copy_to_user_structs(response, resp, resp, path_data, i)))

drivers/infiniband/core/ucma.c static ssize_t ucma_query_path(struct ucma_context *ctx, · line 955

Quoted 1 line
drivers/infiniband/core/ucma.c +0 -0
 		ret = -EFAULT;

You've posted similar patches that also affected things like kzalloc(). Maybe for those it would be better candidates for putting everything into its own helper macro ? (I've already got that on my 2do list, but not sure whether maintainers really like to be bothered with those kind of patches ;-)).

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287