usb: replace hardcoded maximum usb string length by definition
Subsystems (2)
PATCH 1/1 usb: replace hardcoded maximum usb string length by definition
Replace hardcoded maximum usb string length (126 bytes) by definition "MAX_USB_STRING_LEN".
--- drivers/usb/gadget/composite.c | 4 ++-- drivers/usb/gadget/configfs.c | 3 ++- drivers/usb/gadget/usbstring.c | 5 +++-- include/linux/usb.h | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-)
drivers/usb/gadget/composite.c +2 -2
@@ -1041,7 +1041,7 @@ static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)while (*sp) { s = *sp; language =cpu_to_le16 (s->language); -for (tmp = buf; *tmp && tmp < &buf[126 ]; tmp++) { +for (tmp = buf; *tmp && tmp < &buf[MAX_USB_STRING_LEN ]; tmp++) {if (*tmp == language) goto repeat; }
@@ -1116,7 +1116,7 @@ static int get_string(struct usb_composite_dev *cdev,collect_langs (sp, s->wData); } -for (len =0 ; len <=126 && s->wData[len]; len++) +for (len =0 ; len <=MAX_USB_STRING_LEN && s->wData[len]; len++)continue ;if (!len)return -EINVAL ;
drivers/usb/gadget/configfs.c +2 -1
@@ -4,6 +4,7 @@#include <linux/slab.h> #include <linux/device.h> #include <linux/nls.h> +#include <linux/usb.h> #include <linux/usb/composite.h> #include <linux/usb/gadget_configfs.h> #include "configfs.h"
@@ -115,7 +116,7 @@ static int usb_string_copy(const char *s, char **s_copy)char *str;char *copy = *s_copy; ret =strlen (s); -if (ret >126 ) +if (ret >MAX_USB_STRING_LEN )return -EOVERFLOW ; str =kstrdup (s,GFP_KERNEL );
drivers/usb/gadget/usbstring.c +3 -2
@@ -11,6 +11,7 @@#include <linux/device.h> #include <linux/nls.h> +#include <linux/usb.h> #include <linux/usb/ch9.h> #include <linux/usb/gadget.h>
@@ -55,9 +56,9 @@return -EINVAL ;/* string descriptors have length, tag, then UTF16-LE text */ - len =min ((size_t )126 ,strlen (s->s)); + len =min ((size_t )MAX_USB_STRING_LEN ,strlen (s->s)); len =utf8s_to_utf16s (s->s, len,UTF16_LITTLE_ENDIAN , - (wchar_t *) &buf[2 ],126 ); + (wchar_t *) &buf[2 ],MAX_USB_STRING_LEN );if (len <0 )return -EINVAL ; buf [0 ] = (len +1 ) *2 ;
include/linux/usb.h +2 -0
@@ -1815,6 +1815,8 @@ static inline int usb_get_ptm_status(struct usb_device *dev, void *data) 0, data); } +/* USB String descriptors can contain at most 126 characters. */ +#define MAX_USB_STRING_LEN 126extern int usb_string (struct usb_device *dev,int index,char *buf,size_t size);
Signature · 1 line
1.7.9.5
Quoted 4 lines · Macpaul Lin
Replace hardcoded maximum usb string length (126 bytes) by definition "
MAX_USB_STRING_LEN".Signed-off-by Macpaul Lin <[email protected]>
...
include/linux/usb.h static inline int usb_get_ptm_status(struct usb_device *dev, void *data) · line 1819
Quoted 10 lines
include/linux/usb.h +2
-0@@ -1815,6 +1815,8 @@ static inline int usb_get_ptm_status(struct usb_device *dev, void *data) 0, data); } +/* USB String descriptors can contain at most 126 characters. */ +#define MAX_USB_STRING_LEN 126
This definition belongs in include/uapi/linux/usb/ch9.h (near the definition of struct usb_string_descriptor) because it is part of the USB standard rather than specific to the Linux USB stack.
Alan Stern
PATCH v2 1/1 usb: replace hardcode maximum usb string length by definition
In reply to Macpaul LinReplace hardcode maximum usb string length (126 bytes) by definition "MAX_USB_STRING_LEN".
---
Changes for v2:
- Add definition "MAX_USB_STRING_LEN" in ch9.h instead of in usb.h. Thanks for Alan's suggestion.
drivers/usb/gadget/composite.c | 4 ++-- drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/usbstring.c | 4 ++-- include/uapi/linux/usb/ch9.h | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-)
drivers/usb/gadget/composite.c +2 -2
@@ -1041,7 +1041,7 @@ static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)while (*sp) { s = *sp; language =cpu_to_le16 (s->language); -for (tmp = buf; *tmp && tmp < &buf[126 ]; tmp++) { +for (tmp = buf; *tmp && tmp < &buf[MAX_USB_STRING_LEN ]; tmp++) {if (*tmp == language) goto repeat; }
@@ -1116,7 +1116,7 @@ static int get_string(struct usb_composite_dev *cdev,collect_langs (sp, s->wData); } -for (len =0 ; len <=126 && s->wData[len]; len++) +for (len =0 ; len <=MAX_USB_STRING_LEN && s->wData[len]; len++)continue ;if (!len)return -EINVAL ;
drivers/usb/gadget/configfs.c +1 -1
@@ -115,7 +115,7 @@ static int usb_string_copy(const char *s, char **s_copy)char *str;char *copy = *s_copy; ret =strlen (s); -if (ret >126 ) +if (ret >MAX_USB_STRING_LEN )return -EOVERFLOW ; str =kstrdup (s,GFP_KERNEL );
drivers/usb/gadget/usbstring.c +2 -2
@@ -55,9 +55,9 @@return -EINVAL ;/* string descriptors have length, tag, then UTF16-LE text */ - len =min ((size_t )126 ,strlen (s->s)); + len =min ((size_t )MAX_USB_STRING_LEN ,strlen (s->s)); len =utf8s_to_utf16s (s->s, len,UTF16_LITTLE_ENDIAN , - (wchar_t *) &buf[2 ],126 ); + (wchar_t *) &buf[2 ],MAX_USB_STRING_LEN );if (len <0 )return -EINVAL ; buf [0 ] = (len +1 ) *2 ;
include/uapi/linux/usb/ch9.h +3 -0
@@ -364,6 +364,9 @@ struct usb_config_descriptor {/*-------------------------------------------------------------------------*/ +/* USB String descriptors can contain at most 126 characters. */ +#define MAX_USB_STRING_LEN 126 +/* USB_DT_STRING: String descriptor */ struct usb_string_descriptor {__u8 bLength;
Signature · 1 line
1.7.9.5
Re: [PATCH v2] usb: replace hardcode maximum usb string length by definition
Quoted 8 lines · Macpaul Lin
Replace hardcode maximum usb string length (126 bytes) by definition "
MAX_USB_STRING_LEN".Signed-off-by Macpaul Lin <[email protected]>---
Changes for v2:
- Add definition "MAX_USB_STRING_LEN" inch9.hinstead of inusb.h. Thanks for Alan's suggestion.
Greg Kroah-Hartman
maintainerRe: [PATCH v2] usb: replace hardcode maximum usb string length by definition
In reply to Macpaul LinQuoted 9 lines · Macpaul Lin
Replace hardcode maximum usb string length (126 bytes) by definition "
MAX_USB_STRING_LEN".Signed-off-by Macpaul Lin <[email protected]>---
Changes for v2:
- Add definition "MAX_USB_STRING_LEN" inch9.hinstead of inusb.h. Thanks for Alan's suggestion.
Quoted 5 lines
drivers/usb/gadget/composite.c | 4 ++-- drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/usbstring.c | 4 ++-- include/uapi/linux/usb/ch9.h | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-)
This patch fails to apply to my tree (or to 5.8-rc1). Please rebase it and resend.
thanks,
greg k-h
PATCH v3 1/1 usb: replace hardcode maximum usb string length by definition
In reply to Macpaul LinReplace hardcode maximum usb string length (126 bytes) by definition "MAX_USB_STRING_LEN".
---
Changes for v2:
- Add definition "MAX_USB_STRING_LEN" in ch9.h instead of in usb.h. Thanks for Alan's suggestion.
Changes for v3:
- Rebase to 5.8-rc1 and resolve conflict.
drivers/usb/gadget/composite.c | 4 ++-- drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/usbstring.c | 4 ++-- include/uapi/linux/usb/ch9.h | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-)
drivers/usb/gadget/composite.c +2 -2
@@ -1041,7 +1041,7 @@ static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)while (*sp) { s = *sp; language =cpu_to_le16 (s->language); -for (tmp = buf; *tmp && tmp < &buf[126 ]; tmp++) { +for (tmp = buf; *tmp && tmp < &buf[MAX_USB_STRING_LEN ]; tmp++) {if (*tmp == language) goto repeat; }
@@ -1116,7 +1116,7 @@ static int get_string(struct usb_composite_dev *cdev,collect_langs (sp, s->wData); } -for (len =0 ; len <=126 && s->wData[len]; len++) +for (len =0 ; len <=MAX_USB_STRING_LEN && s->wData[len]; len++)continue ;if (!len)return -EINVAL ;
drivers/usb/gadget/configfs.c +1 -1
@@ -115,7 +115,7 @@ static int usb_string_copy(const char *s, char **s_copy)char *str;char *copy = *s_copy; ret =strlen (s); -if (ret >126 ) +if (ret >MAX_USB_STRING_LEN )return -EOVERFLOW ; str =kstrdup (s,GFP_KERNEL );
drivers/usb/gadget/usbstring.c +2 -2
@@ -55,9 +55,9 @@return -EINVAL ;/* string descriptors have length, tag, then UTF16-LE text */ - len =min ((size_t )126 ,strlen (s->s)); + len =min ((size_t )MAX_USB_STRING_LEN ,strlen (s->s)); len =utf8s_to_utf16s (s->s, len,UTF16_LITTLE_ENDIAN , - (wchar_t *) &buf[2 ],126 ); + (wchar_t *) &buf[2 ],MAX_USB_STRING_LEN );if (len <0 )return -EINVAL ; buf [0 ] = (len +1 ) *2 ;
include/uapi/linux/usb/ch9.h +3 -0
@@ -364,6 +364,9 @@ struct usb_config_descriptor {/*-------------------------------------------------------------------------*/ +/* USB String descriptors can contain at most 126 characters. */ +#define MAX_USB_STRING_LEN 126 +/* USB_DT_STRING: String descriptor */ struct usb_string_descriptor {__u8 bLength;
Signature · 1 line
1.7.9.5
Greg Kroah-Hartman
maintainerRe: [PATCH v3] usb: replace hardcode maximum usb string length by definition
Quoted 74 lines · Macpaul Lin
Replace hardcode maximum usb string length (126 bytes) by definition "
MAX_USB_STRING_LEN".Signed-off-by Macpaul Lin <[email protected]>Acked-by Alan Stern <[email protected]>---
Changes for v2:
- Add definition "MAX_USB_STRING_LEN" inch9.hinstead of inusb.h. Thanks for Alan's suggestion.
Changes for v3:
- Rebase to 5.8-rc1 and resolve conflict.drivers/usb/gadget/composite.c | 4 ++-- drivers/usb/gadget/configfs.c | 2 +- drivers/usb/gadget/usbstring.c | 4 ++-- include/uapi/linux/usb/ch9.h | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-)drivers/usb/gadget/composite.c +2
-2@@ -1041,7 +1041,7 @@ static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)while (*sp) { s = *sp; language =cpu_to_le16 (s->language); -for (tmp = buf; *tmp && tmp < &buf[126 ]; tmp++) { +for (tmp = buf; *tmp && tmp < &buf[MAX_USB_STRING_LEN ]; tmp++) {if (*tmp == language) goto repeat; }@@ -1116,7 +1116,7 @@ static int get_string(struct usb_composite_dev *cdev,collect_langs (sp, s->wData); } -for (len =0 ; len <=126 && s->wData[len]; len++) +for (len =0 ; len <=MAX_USB_STRING_LEN && s->wData[len]; len++)continue ;if (!len)return -EINVAL ;drivers/usb/gadget/configfs.c +1
-1@@ -115,7 +115,7 @@ static int usb_string_copy(const char *s, char **s_copy)char *str;char *copy = *s_copy; ret =strlen (s); -if (ret >126 ) +if (ret >MAX_USB_STRING_LEN )return -EOVERFLOW ; str =kstrdup (s,GFP_KERNEL );drivers/usb/gadget/usbstring.c +2
-2@@ -55,9 +55,9 @@return -EINVAL ;/* string descriptors have length, tag, then UTF16-LE text */ - len =min ((size_t )126 ,strlen (s->s)); + len =min ((size_t )MAX_USB_STRING_LEN ,strlen (s->s)); len =utf8s_to_utf16s (s->s, len,UTF16_LITTLE_ENDIAN , - (wchar_t *) &buf[2 ],126 ); + (wchar_t *) &buf[2 ],MAX_USB_STRING_LEN );if (len <0 )return -EINVAL ; buf [0 ] = (len +1 ) *2 ;include/uapi/linux/usb/ch9.h +0
-0
include/uapi/linux/usb/ch9.h struct usb_config_descriptor { · line 368
Quoted 5 lines
include/uapi/linux/usb/ch9.h +2
-0@@ -364,6 +364,9 @@ struct usb_config_descriptor {/*-------------------------------------------------------------------------*/ +/* USB String descriptors can contain at most 126 characters. */ +#define MAX_USB_STRING_LEN 126
Nit, as this is part of the userspace api, we should make this "USB_MAX_STRING_LEN" as we should be using "USB_" for all exports here.
That seems to be the case already except for one really odd set of "TEST_" defines, which I'll go fix up after this...
I will just hand-edit this patch to make that change so you don't have to respin it again...
thanks,
greg k-h