9
0
Fork 0

usb: gadget: properly release f_multi_opts

The usbgadget commands uses statically allocated f_multi_opts and passes
this to usb_multi_register(). These f_multi_opts are of course no
longer valid when we leave the usbgadget command. Luckily we do not use
the data after we left the usbgadget command, so this never has been a
problem. However, f_multi_opts has some allocated members which we can
not free anymore on gadget unregistration because we no longer have the
pointer to them.

Fix this by adding a release function to struct f_multi_opts. This way
we can allocate all memory dynamically and properly free it when not
used anymore.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2017-01-20 10:03:45 +01:00
parent 25fd64ed22
commit 2b9bcff79a
3 changed files with 31 additions and 8 deletions

View File

@ -31,10 +31,10 @@
static int do_usbgadget(int argc, char *argv[])
{
int opt;
int opt, ret;
int acm = 1, create_serial = 0;
char *fastboot_opts = NULL, *dfu_opts = NULL;
struct f_multi_opts opts = {};
struct f_multi_opts *opts;
while ((opt = getopt(argc, argv, "asdA:D:")) > 0) {
switch (opt) {
@ -73,19 +73,26 @@ static int do_usbgadget(int argc, char *argv[])
return -EINVAL;
}
opts = xzalloc(sizeof(*opts));
opts->release = usb_multi_opts_release;
if (fastboot_opts) {
opts.fastboot_opts.files = file_list_parse(fastboot_opts);
opts->fastboot_opts.files = file_list_parse(fastboot_opts);
}
if (dfu_opts) {
opts.dfu_opts.files = file_list_parse(dfu_opts);
opts->dfu_opts.files = file_list_parse(dfu_opts);
}
if (create_serial) {
opts.create_acm = acm;
opts->create_acm = acm;
}
return usb_multi_register(&opts);
ret = usb_multi_register(opts);
if (ret)
usb_multi_opts_release(opts);
return ret;
}
BAREBOX_CMD_HELP_START(usbgadget)

View File

@ -246,8 +246,22 @@ int usb_multi_register(struct f_multi_opts *opts)
void usb_multi_unregister(void)
{
if (gadget_multi_opts)
usb_composite_unregister(&multi_driver);
if (!gadget_multi_opts)
return;
usb_composite_unregister(&multi_driver);
if (gadget_multi_opts->release)
gadget_multi_opts->release(gadget_multi_opts);
gadget_multi_opts = NULL;
}
void usb_multi_opts_release(struct f_multi_opts *opts)
{
if (opts->fastboot_opts.files)
file_list_free(opts->fastboot_opts.files);
if (opts->dfu_opts.files)
file_list_free(opts->dfu_opts.files);
free(opts);
}

View File

@ -9,9 +9,11 @@ struct f_multi_opts {
struct f_fastboot_opts fastboot_opts;
struct f_dfu_opts dfu_opts;
int create_acm;
void (*release)(struct f_multi_opts *opts);
};
int usb_multi_register(struct f_multi_opts *opts);
void usb_multi_unregister(void);
void usb_multi_opts_release(struct f_multi_opts *opts);
#endif /* __USB_GADGET_MULTI_H */