Message ID | 1491224951-22437-1-git-send-email-nicolas.le.bayon@st.com |
---|---|
State | Accepted |
Commit | b1f2a17c780ce9b1d7666bcc6eef82ce9bb4b27f |
Delegated to: | Ćukasz Majewski |
Headers | show |
-----Original Message----- From: Marek Vasut [mailto:marex@denx.de] Sent: lundi 3 avril 2017 15:25 To: Nicolas LE BAYON <nicolas.le.bayon@st.com>; u-boot@lists.denx.de; lukma@denx.de Cc: nlebayon@gmail.com; Patrice CHOTARD <patrice.chotard@st.com>; Jean-philippe ROMAIN <jean-philippe.romain@st.com> Subject: Re: [U-Boot][PATCH RESEND v6] usb: gadget: avoid variable name clipping in cb_getvar On 04/03/2017 03:09 PM, nicolas.le.bayon@st.com wrote: > From: Nicolas Le Bayon <nlebayon@gmail.com> > > Instead of using a fixed-size array to store variable name, preferring > a dynamic allocation treats correctly all variable name lengths. > Variable names are growing through releases and features. By this way, > name clipping is prevented. > > Signed-off-by: Nicolas Le Bayon <nlebayon@gmail.com> > Reviewed-by: Marek Vasut <marex@denx.de> > Acked-by: Lukasz Majewski <lukma@denx.de> So I received two patches which are the same, why? Because I switched to my pro e-mail and I wasn't registered yet, so I received an automatic message with non-member reason. So I registered my pro address and re-sent the same patch. Please consider only one. > --- > > Changes in v2: > - instead of using a bigger fixed size, use malloc to fit with size > needs Changes in v3: > - v2 was an error (intermediate version), so propose a complete one > Changes in v4: > - be more explicit and detailed in label and description fields > - remove intermediate variable only used one time > - be more explicit in error message > - fix indent issue > Changes in v5: > - drop an unuseful error() call > Changes in v6: > - add Marek review approval > > drivers/usb/gadget/f_fastboot.c | 12 ++++++++++-- > 1 file changed, 10 insertions(+), 2 deletions(-) > > diff --git a/drivers/usb/gadget/f_fastboot.c > b/drivers/usb/gadget/f_fastboot.c index 2160b1c..7cd6d24 100644 > --- a/drivers/usb/gadget/f_fastboot.c > +++ b/drivers/usb/gadget/f_fastboot.c > @@ -432,9 +432,15 @@ static void cb_getvar(struct usb_ep *ep, struct usb_request *req) > else > strcpy(response, "FAILValue not set"); > } else { > - char envstr[32]; > + char *envstr; > > - snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd); > + envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1); > + if (!envstr) { > + fastboot_tx_write_str("FAILmalloc error"); > + return; > + } > + > + sprintf(envstr, "fastboot.%s", cmd); > s = getenv(envstr); > if (s) { > strncat(response, s, chars_left); > @@ -442,6 +448,8 @@ static void cb_getvar(struct usb_ep *ep, struct usb_request *req) > printf("WARNING: unknown variable: %s\n", cmd); > strcpy(response, "FAILVariable not implemented"); > } > + > + free(envstr); > } > fastboot_tx_write_str(response); > } > -- Best regards, Marek Vasut
diff --git a/drivers/usb/gadget/f_fastboot.c b/drivers/usb/gadget/f_fastboot.c index 2160b1c..7cd6d24 100644 --- a/drivers/usb/gadget/f_fastboot.c +++ b/drivers/usb/gadget/f_fastboot.c @@ -432,9 +432,15 @@ static void cb_getvar(struct usb_ep *ep, struct usb_request *req) else strcpy(response, "FAILValue not set"); } else { - char envstr[32]; + char *envstr; - snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd); + envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1); + if (!envstr) { + fastboot_tx_write_str("FAILmalloc error"); + return; + } + + sprintf(envstr, "fastboot.%s", cmd); s = getenv(envstr); if (s) { strncat(response, s, chars_left); @@ -442,6 +448,8 @@ static void cb_getvar(struct usb_ep *ep, struct usb_request *req) printf("WARNING: unknown variable: %s\n", cmd); strcpy(response, "FAILVariable not implemented"); } + + free(envstr); } fastboot_tx_write_str(response); }