openprom: Use struct_size() helper
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
@@ -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
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@@ -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. :)
Reviewed-by Kees Cook <[email protected]>
Signature · 1 line
Kees Cook