Skip to content

Commit

Permalink
USB: replace hardcode maximum usb string length by definition
Browse files Browse the repository at this point in the history
Replace hardcoded maximum USB string length (126 bytes) by definition
"USB_MAX_STRING_LEN".

Signed-off-by: Macpaul Lin <macpaul.lin@mediatek.com>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Link: https://lore.kernel.org/r/1592471618-29428-1-git-send-email-macpaul.lin@mediatek.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
macpaul-lin-mtk authored and gregkh committed Jun 18, 2020
1 parent cda37db commit 81c7462
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions drivers/usb/gadget/composite.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,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[USB_MAX_STRING_LEN]; tmp++) {
if (*tmp == language)
goto repeat;
}
Expand Down Expand Up @@ -1160,7 +1160,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 <= USB_MAX_STRING_LEN && s->wData[len]; len++)
continue;
if (!len)
return -EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/gadget/configfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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 > USB_MAX_STRING_LEN)
return -EOVERFLOW;

str = kstrdup(s, GFP_KERNEL);
Expand Down
4 changes: 2 additions & 2 deletions drivers/usb/gadget/usbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ usb_gadget_get_string (const struct usb_gadget_strings *table, int id, u8 *buf)
return -EINVAL;

/* string descriptors have length, tag, then UTF16-LE text */
len = min ((size_t) 126, strlen (s->s));
len = min((size_t)USB_MAX_STRING_LEN, strlen(s->s));
len = utf8s_to_utf16s(s->s, len, UTF16_LITTLE_ENDIAN,
(wchar_t *) &buf[2], 126);
(wchar_t *) &buf[2], USB_MAX_STRING_LEN);
if (len < 0)
return -EINVAL;
buf [0] = (len + 1) * 2;
Expand Down
3 changes: 3 additions & 0 deletions include/uapi/linux/usb/ch9.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ struct usb_config_descriptor {

/*-------------------------------------------------------------------------*/

/* USB String descriptors can contain at most 126 characters. */
#define USB_MAX_STRING_LEN 126

/* USB_DT_STRING: String descriptor */
struct usb_string_descriptor {
__u8 bLength;
Expand Down

0 comments on commit 81c7462

Please sign in to comment.