* [PATCH] usbip: usbip_host_common: Use struct_size() in realloc()
@ 2019-05-23 14:40 Gustavo A. R. Silva
2019-05-23 14:52 ` Gustavo A. R. Silva
0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2019-05-23 14:40 UTC (permalink / raw)
To: Valentina Manea, Shuah Khan; +Cc: linux-usb, linux-kernel, Gustavo A. R. Silva
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:
struct foo {
int stuff;
struct boo entry[];
};
size = sizeof(struct foo) + count * sizeof(struct boo);
instance = realloc(instance, size);
Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:
size = struct_size(instance, entry, count);
or
instance = realloc(instance, struct_size(instance, entry, count));
Notice that, in this case, variable size is not necessary,
hence it is removed.
This code was detected with the help of Coccinelle.
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
Notice that checkpatch reports the following warning:
WARNING: line over 80 characters
#57: FILE: tools/usb/usbip/libsrc/usbip_host_common.c:90:
+ edev = realloc(edev, struct_size(edev, uinf, edev->udev.bNumInterfaces));
The line above is 81-character long. So, I think we should be fine
with that, instead of split it into two lines like:
edev = realloc(edev,
struct_size(edev, uinf, edev->udev.bNumInterfaces));
Thanks
--
Gustavo
tools/usb/usbip/libsrc/usbip_host_common.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/tools/usb/usbip/libsrc/usbip_host_common.c b/tools/usb/usbip/libsrc/usbip_host_common.c
index 2813aa821c82..1645d02a52af 100644
--- a/tools/usb/usbip/libsrc/usbip_host_common.c
+++ b/tools/usb/usbip/libsrc/usbip_host_common.c
@@ -67,7 +67,6 @@ struct usbip_exported_device *usbip_exported_device_new(
{
struct usbip_exported_device *edev = NULL;
struct usbip_exported_device *edev_old;
- size_t size;
int i;
edev = calloc(1, sizeof(struct usbip_exported_device));
@@ -87,11 +86,8 @@ struct usbip_exported_device *usbip_exported_device_new(
goto err;
/* reallocate buffer to include usb interface data */
- size = sizeof(struct usbip_exported_device) +
- edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
-
edev_old = edev;
- edev = realloc(edev, size);
+ edev = realloc(edev, struct_size(edev, uinf, edev->udev.bNumInterfaces));
if (!edev) {
edev = edev_old;
dbg("realloc failed");
--
2.21.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] usbip: usbip_host_common: Use struct_size() in realloc()
2019-05-23 14:40 [PATCH] usbip: usbip_host_common: Use struct_size() in realloc() Gustavo A. R. Silva
@ 2019-05-23 14:52 ` Gustavo A. R. Silva
0 siblings, 0 replies; 2+ messages in thread
From: Gustavo A. R. Silva @ 2019-05-23 14:52 UTC (permalink / raw)
To: Valentina Manea, Shuah Khan; +Cc: linux-usb, linux-kernel
Hi all,
Sorry, please, drop this patch.
Thanks
--
Gustavo
On 5/23/19 9:40 AM, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
>
> struct foo {
> int stuff;
> struct boo entry[];
> };
>
> size = sizeof(struct foo) + count * sizeof(struct boo);
> instance = realloc(instance, size);
>
> Instead of leaving these open-coded and prone to type mistakes, we can
> now use the new struct_size() helper:
>
> size = struct_size(instance, entry, count);
>
> or
>
> instance = realloc(instance, struct_size(instance, entry, count));
>
> Notice that, in this case, variable size is not necessary,
> hence it is removed.
>
> This code was detected with the help of Coccinelle.
>
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>
> Notice that checkpatch reports the following warning:
>
> WARNING: line over 80 characters
> #57: FILE: tools/usb/usbip/libsrc/usbip_host_common.c:90:
> + edev = realloc(edev, struct_size(edev, uinf, edev->udev.bNumInterfaces));
>
> The line above is 81-character long. So, I think we should be fine
> with that, instead of split it into two lines like:
>
> edev = realloc(edev,
> struct_size(edev, uinf, edev->udev.bNumInterfaces));
>
> Thanks
> --
> Gustavo
>
> tools/usb/usbip/libsrc/usbip_host_common.c | 6 +-----
> 1 file changed, 1 insertion(+), 5 deletions(-)
>
> diff --git a/tools/usb/usbip/libsrc/usbip_host_common.c b/tools/usb/usbip/libsrc/usbip_host_common.c
> index 2813aa821c82..1645d02a52af 100644
> --- a/tools/usb/usbip/libsrc/usbip_host_common.c
> +++ b/tools/usb/usbip/libsrc/usbip_host_common.c
> @@ -67,7 +67,6 @@ struct usbip_exported_device *usbip_exported_device_new(
> {
> struct usbip_exported_device *edev = NULL;
> struct usbip_exported_device *edev_old;
> - size_t size;
> int i;
>
> edev = calloc(1, sizeof(struct usbip_exported_device));
> @@ -87,11 +86,8 @@ struct usbip_exported_device *usbip_exported_device_new(
> goto err;
>
> /* reallocate buffer to include usb interface data */
> - size = sizeof(struct usbip_exported_device) +
> - edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
> -
> edev_old = edev;
> - edev = realloc(edev, size);
> + edev = realloc(edev, struct_size(edev, uinf, edev->udev.bNumInterfaces));
> if (!edev) {
> edev = edev_old;
> dbg("realloc failed");
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-05-23 15:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-05-23 14:40 [PATCH] usbip: usbip_host_common: Use struct_size() in realloc() Gustavo A. R. Silva
2019-05-23 14:52 ` Gustavo A. R. Silva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox