Skip to contentSkip to conversation

openprom: Use struct_size() helper

PATCH In review Gustavo A. R. Silva 2 participants 2 messages Jun 22, 2023 – Jun 29, 2023 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 openprom: Use struct_size() helper

Prefer struct_size() over open-coded versions.

---
 drivers/sbus/char/openprom.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/sbus/char/openprom.c +6 -2
index 30b9751aad30..de56c8fec373 100644
@@ -76,7 +76,9 @@ static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
 	if (bufsize > OPROMMAXPARAM)
 		bufsize = OPROMMAXPARAM;
 
-	if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
+	*opp_p = kzalloc(struct_size(*opp_p, oprom_array, bufsize + 1),
+			 GFP_KERNEL);
+	if (!*opp_p)
 		return -ENOMEM;
 
 	if (copy_from_user(&(*opp_p)->oprom_array,
@@ -95,7 +97,9 @@ static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
 	if (!info || !opp_p)
 		return -EFAULT;
 
-	if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
+	*opp_p = kzalloc(struct_size(*opp_p, oprom_array, OPROMMAXPARAM + 1),
+			 GFP_KERNEL);
+	if (!*opp_p)
 		return -ENOMEM;
 
 	(*opp_p)->oprom_size = 0;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Gustavo A. R. Silva

Prefer struct_size() over open-coded versions.

This commit log could be more descriptive since the replacement below isn't in the standard "sizeof(struct) + sizeof(element) * count" format. :) It took me a moment to figure out what was going on:

struct openpromio
{
unsigned int oprom_size; /* Actual size of the oprom_array. */
char oprom_array[]; /* Holds property names and values. */
};

sizeof(struct openpromio) == sizeof(int) :P

Quoted 23 lines
---
 drivers/sbus/char/openprom.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/sbus/char/openprom.c +3 -1
index 30b9751aad30..de56c8fec373 100644
@@ -76,7 +76,9 @@ static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
 	if (bufsize > OPROMMAXPARAM)
 		bufsize = OPROMMAXPARAM;
 
-	if (!(*opp_p = kzalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
+	*opp_p = kzalloc(struct_size(*opp_p, oprom_array, bufsize + 1),
+			 GFP_KERNEL);
+	if (!*opp_p)
 		return -ENOMEM;
 
 	if (copy_from_user(&(*opp_p)->oprom_array,

drivers/sbus/char/openprom.c static int getstrings(struct openpromio __user *info, struct openpromio **opp_p) · line 103

Quoted 8 lines
drivers/sbus/char/openprom.c +3 -1
@@ -95,7 +97,9 @@ static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
 	if (!info || !opp_p)
 		return -EFAULT;
 
-	if (!(*opp_p = kzalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
+	*opp_p = kzalloc(struct_size(*opp_p, oprom_array, OPROMMAXPARAM + 1),
+			 GFP_KERNEL);
+	if (!*opp_p)
 		return -ENOMEM;

With that understood, yeah, these look good to me. :)

Signature · 1 line
Kees Cook