public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: dwc2: gadget: Fix a warning when compiling with W=1
@ 2023-09-23 10:54 Christophe JAILLET
  2023-09-26 19:49 ` kernel test robot
  2023-10-02 11:45 ` Greg Kroah-Hartman
  0 siblings, 2 replies; 4+ messages in thread
From: Christophe JAILLET @ 2023-09-23 10:54 UTC (permalink / raw)
  To: Minas Harutyunyan, Greg Kroah-Hartman, Ben Dooks
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET,
	Greg Kroah-Hartman, linux-usb

In order to teach the compiler that 'hs_ep->name' will never be truncated,
we need to tell it that 'epnum' is not negative.

'epnum' comes from in a 'for' loop in dwc2_gadget_init(), starting at 0
and ending at 255. (hsotg->num_of_eps is a char)

When building with W=1, this fixes the following warnings:

  drivers/usb/dwc2/gadget.c: In function ‘dwc2_hsotg_initep’:
  drivers/usb/dwc2/gadget.c:4804:55: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 8 [-Werror=format-truncation=]
   4804 |         snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
        |                                                       ^~
  drivers/usb/dwc2/gadget.c:4804:52: note: directive argument in the range [-2147483645, 255]
   4804 |         snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
        |                                                    ^~~~~~~~
  drivers/usb/dwc2/gadget.c:4804:9: note: ‘snprintf’ output between 6 and 17 bytes into a destination of size 10
   4804 |         snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fixes: 5b7d70c6dbf2 ("USB: Gadget driver for Samsung HS/OtG block")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
Only changing:
  -	int epnum;
  +	unsigned int epnum;
is enought to fix the build warning.

But changing the prototype of dwc2_hsotg_initep() and the printf() format
as well, to make obvious that epnum is >= 0, looks more logical to me.
---
 drivers/usb/dwc2/gadget.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index b517a7216de2..102b2dd8113e 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -4786,8 +4786,8 @@ static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
  */
 static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
 			      struct dwc2_hsotg_ep *hs_ep,
-				       int epnum,
-				       bool dir_in)
+			      unsigned int epnum,
+			      bool dir_in)
 {
 	char *dir;
 
@@ -4801,7 +4801,7 @@ static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
 	hs_ep->dir_in = dir_in;
 	hs_ep->index = epnum;
 
-	snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
+	snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%u%s", epnum, dir);
 
 	INIT_LIST_HEAD(&hs_ep->queue);
 	INIT_LIST_HEAD(&hs_ep->ep.ep_list);
@@ -4965,7 +4965,7 @@ static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
 int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
 {
 	struct device *dev = hsotg->dev;
-	int epnum;
+	unsigned int epnum;
 	int ret;
 
 	/* Dump fifo information */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-03 20:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-23 10:54 [PATCH] usb: dwc2: gadget: Fix a warning when compiling with W=1 Christophe JAILLET
2023-09-26 19:49 ` kernel test robot
2023-10-02 11:45 ` Greg Kroah-Hartman
2023-10-03 20:37   ` Christophe JAILLET

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox