From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752147AbcCHVBU (ORCPT ); Tue, 8 Mar 2016 16:01:20 -0500 Received: from smtprelay0064.hostedemail.com ([216.40.44.64]:56899 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751647AbcCHVBO (ORCPT ); Tue, 8 Mar 2016 16:01:14 -0500 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::,RULES_HIT:41:355:379:541:599:960:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2393:2559:2562:2828:3138:3139:3140:3141:3142:3352:3622:3865:3868:3870:3871:4321:5007:6120:6261:7904:8531:10004:10400:10848:11026:11232:11658:11783:11914:12043:12048:12296:12438:12517:12519:12740:13069:13311:13357:13439:13894:14659:14721:21080:30012:30054:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:1,LUA_SUMMARY:none X-HE-Tag: nest01_7ebe15dcd125a X-Filterd-Recvd-Size: 2160 Message-ID: <1457470869.10628.3.camel@perches.com> Subject: Re: [RFC 7/7] USB: usbatm: avoid fragile and inefficient snprintf use From: Joe Perches To: Rasmus Villemoes , Kees Cook , Andrew Morton , Duncan Sands , Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Date: Tue, 08 Mar 2016 13:01:09 -0800 In-Reply-To: <1457469654-17059-8-git-send-email-linux@rasmusvillemoes.dk> References: <1457469654-17059-1-git-send-email-linux@rasmusvillemoes.dk> <1457469654-17059-8-git-send-email-linux@rasmusvillemoes.dk> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.1-1ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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()