lkml.org 
[lkml]   [2016]   [Mar]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [RFC 7/7] USB: usbatm: avoid fragile and inefficient snprintf use
From
Date
On Tue, 2016-03-08 at 21:40 +0100, Rasmus Villemoes wrote:
> Passing overlapping source and destination is fragile, and in this
> case we can even simplify the code and avoid the huge stack buffer by
> using the %p extension for printing a small hex dump.
[]
> diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c
[]
> @@ -1331,15 +1331,12 @@ MODULE_VERSION(DRIVER_VERSION);
>  static int usbatm_print_packet(struct usbatm_data *instance,
>          const unsigned char *data, int len)
>  {
> - unsigned char buffer[256];
> - int i = 0, j = 0;
> + int i, j;
>  
>   for (i = 0; i < len;) {
> - buffer[0] = '\0';
> - sprintf(buffer, "%.3d :", i);
> - for (j = 0; (j < 16) && (i < len); j++, i++)
> - sprintf(buffer, "%s %2.2x", buffer, data[i]);
> - dev_dbg(&instance->usb_intf->dev, "%s", buffer);
> + j = min(16, len-i);
> + dev_dbg(&instance->usb_intf->dev, "%.3d : %*ph", i, j, data + i);
> + i += j;
>   }
>   return i;
>  }

Maybe use print_dump_hex_debug()

\
 
 \ /
  Last update: 2016-03-08 22:41    [W:0.260 / U:0.384 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site