From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 500161150 for ; Fri, 3 Feb 2017 13:26:26 +0100 (CET) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP; 03 Feb 2017 04:26:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,328,1477983600"; d="scan'208";a="220931512" Received: from irsmsx108.ger.corp.intel.com ([163.33.3.3]) by fmsmga004.fm.intel.com with ESMTP; 03 Feb 2017 04:26:24 -0800 Received: from irsmsx103.ger.corp.intel.com ([169.254.3.77]) by IRSMSX108.ger.corp.intel.com ([169.254.11.173]) with mapi id 14.03.0248.002; Fri, 3 Feb 2017 12:26:23 +0000 From: "Mrozowicz, SlawomirX" To: Stephen Hemminger , "Doherty, Declan" CC: "dev@dpdk.org" , "De Lara Guarch, Pablo" Thread-Topic: bugs and glitches in rte_cryptodev_devices_get Thread-Index: AQHSfKvNzGDS4PCdLEmABsE9qVpYEaFXNwbg Date: Fri, 3 Feb 2017 12:26:23 +0000 Message-ID: <158888A50F43E34AAE179517F56C9745652A9B@IRSMSX103.ger.corp.intel.com> References: <20170201085321.7168505a@xeon-e3> In-Reply-To: <20170201085321.7168505a@xeon-e3> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [163.33.239.180] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] bugs and glitches in rte_cryptodev_devices_get X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Feb 2017 12:26:27 -0000 >-----Original Message----- >From: Stephen Hemminger [mailto:stephen@networkplumber.org] >Sent: Wednesday, February 1, 2017 5:54 PM >To: Mrozowicz, SlawomirX ; Doherty, >Declan >Cc: dev@dpdk.org >Subject: bugs and glitches in rte_cryptodev_devices_get > >The function rte_cryptodev_devices_get has several issues. I was just goin= g >to fix it, but think it need to be explained. > >One potentially serious one (reported by coverity) is: > >*** CID 141067: (BAD_COMPARE) >/lib/librte_cryptodev/rte_cryptodev.c: 503 in rte_cryptodev_devices_get() >497 && (*devs + i)->attached =3D=3D >498 RTE_CRYPTODEV_ATTACHED) >{ >499 >500 dev =3D (*devs + i)->device; >501 >502 if (dev) >>>> CID 141067: (BAD_COMPARE) >>>> Truncating the result of "strncmp" to "unsigned char" may cause it= to be >misinterpreted as 0. Note that "strncmp" may return an integer besides -1,= 0, >or 1. >503 cmp =3D strncmp(dev->driver->name, >504 dev_name, >505 strlen(dev_name)); >506 else >507 cmp =3D strncmp((*devs + i)->data->name, >508 dev_name, >/lib/librte_cryptodev/rte_cryptodev.c: 507 in rte_cryptodev_devices_get() >501 >502 if (dev) >503 cmp =3D strncmp(dev->driver->name, >504 dev_name, >505 strlen(dev_name)); >506 else >>>> CID 141067: (BAD_COMPARE) >>>> Truncating the result of "strncmp" to "unsigned char" may cause it= to be >misinterpreted as 0. Note that "strncmp" may return an integer besides -1,= 0, >or 1. >507 cmp =3D strncmp((*devs + i)->data->name, >508 dev_name, >509 strlen(dev_name)); >510 >511 if (cmp =3D=3D 0) >512 devices[count++] =3D (*devs + i)->data->dev_id; > > >But also: > >1. Incorrect function signature: > * function returns int but never a negative value. should be unsigned. > * devices argument is not modified should be const. > >2. Original ABI seems short sighted with limit of 256 cryptodevs > * this seems like 8 bit mindset, should really use unsigned int inste= ad > of uint8_t for number of devices. > >3. Wacky indention of the if statement. > >4. Make variables local to the block they are used (cmp, dev) > >5. Use array instead of pointer: > ie. instead of *devs + i use devs[i] > We reconsider your suggestions and we addressed all your changes except add= the const of the devices argument, since in our opinion it is not necessar= y. > >The overall code in question is: > > >int >rte_cryptodev_devices_get(const char *dev_name, uint8_t *devices, > uint8_t nb_devices) >{ > uint8_t i, cmp, count =3D 0; > struct rte_cryptodev **devs =3D &rte_cryptodev_globals->devs; > struct rte_device *dev; > > for (i =3D 0; i < rte_cryptodev_globals->max_devs && count < >nb_devices; > i++) { > > if ((*devs + i) > && (*devs + i)->attached =3D=3D > RTE_CRYPTODEV_ATTACHED) >{ > > dev =3D (*devs + i)->device; > > if (dev) > cmp =3D strncmp(dev->driver->name, > dev_name, > strlen(dev_name)); > else > cmp =3D strncmp((*devs + i)->data->name, > dev_name, > strlen(dev_name)); > > if (cmp =3D=3D 0) > devices[count++] =3D (*devs + i)->data->dev_id; > } > } > > return count; >} > >Please fix it. >