lkml.org 
[lkml]   [2009]   [Oct]   [18]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: [PATCH 1/7] usb: trivial cleanups
From
On Mon, Oct 19, 2009 at 1:54 AM, Felipe Contreras
<felipe.contreras@gmail.com> wrote:
> It seems 'min_t' was used before, and looks cleaner, plus white-space
> stuff.
>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
>  drivers/usb/core/hcd.c |    7 +++----
>  1 files changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 34de475..e6d754e 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -396,8 +396,7 @@ rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
>        case 0:
>                /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
>                /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
> -               if (len > 4)
> -                       len = 4;
> +               len = min_t(unsigned, len, sizeof(langids));
>                memcpy(data, langids, len);
>                return len;
>        case 1:

I still have an unfixed warning:
drivers/usb/core/hcd.c: In function ‘rh_string’:
/data/public/src/linux/arch/x86/include/asm/string_32.h:74: warning:
array subscript is above array bounds

Apparently there's a problem with the optimized memcpy for x86 with this code:
static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
len = min_t(unsigned, len, sizeof(langids));
memcpy(data, langids, len);
return len;

gcc 4.4 is trying to optimize the memcpy, but it's not able to realize
that 'len' will always be <= 4 and the memcpy will not exceed langids.
One way to solve this is by replacing len with the min_t expression:
memcpy(data, langids, min_t(unsigned, len, sizeof(langids)));

However, that looks ugly and we need the expression again for the
return. Another way is to remove 'const' from langids.

AFAIK the code is perfectly correct as it is, I think the fact that
gcc 4.4 complains is a bug on gcc side.

Cheers.

--
Felipe Contreras
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2009-10-19 01:11    [W:0.089 / U:0.188 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site