From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alan Stern Subject: Re: [PATCH] ACPI: replace strlen("string") with sizeof("string") -1 Date: Mon, 6 Aug 2012 12:28:33 -0400 (EDT) Message-ID: References: <501FEA29.1030400@pavlinux.ru> Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <501FEA29.1030400@pavlinux.ru> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-pm-bounces@lists.linux-foundation.org Errors-To: linux-pm-bounces@lists.linux-foundation.org To: Pavel Vasilyev Cc: Len Brown , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-pm@lists.linux-foundation.org List-Id: linux-acpi@vger.kernel.org On Mon, 6 Aug 2012, Pavel Vasilyev wrote: > 06.08.2012 18:36, Alan Stern =D0=BF=D0=B8=D1=88=D0=B5=D1=82: > > On Mon, 6 Aug 2012, Pavel Machek wrote: > > > >> On Thu 2012-07-26 21:39:38, Len Brown wrote: > >>> ...both give the number of chars in the string > >>> without the '\0', as strncmp() wants, > >>> but sizeof() is compile-time. > >> > >> What about introducing something like streq() to do this > >> automatically? This is ugly.... > >> > >> #define streq(a, b) ... if (_buildin_constant(b)) ... > >> > >> ? > >> > >>> - if (!strncmp(val, "enable", strlen("enable"))) { > >>> + if (!strncmp(val, "enable", sizeof("enable") - 1)) { > > > > While you're at it, there's no point using strncmp when you know the > > length of one of the strings beforehand. Just use memcmp, and don't > > subtract 1 from the sizeof value. >=20 > http://www.gossamer-threads.com/lists/engine?do=3Dpost_attachment;posta= tt_id=3D41157;list=3Dlinux Interestingly, many (all?) of the changes in that patch are wrong=20 because they don't try to match the terminating '\0'. As a result,=20 they will match against extensions of the target string as well as the=20 target string itself. Alan Stern From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756692Ab2HFQ2f (ORCPT ); Mon, 6 Aug 2012 12:28:35 -0400 Received: from iolanthe.rowland.org ([192.131.102.54]:36621 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1752170Ab2HFQ2e (ORCPT ); Mon, 6 Aug 2012 12:28:34 -0400 Date: Mon, 6 Aug 2012 12:28:33 -0400 (EDT) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Pavel Vasilyev cc: Pavel Machek , Len Brown , , , , Len Brown Subject: Re: [linux-pm] [PATCH] ACPI: replace strlen("string") with sizeof("string") -1 In-Reply-To: <501FEA29.1030400@pavlinux.ru> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 6 Aug 2012, Pavel Vasilyev wrote: > 06.08.2012 18:36, Alan Stern пишет: > > On Mon, 6 Aug 2012, Pavel Machek wrote: > > > >> On Thu 2012-07-26 21:39:38, Len Brown wrote: > >>> ...both give the number of chars in the string > >>> without the '\0', as strncmp() wants, > >>> but sizeof() is compile-time. > >> > >> What about introducing something like streq() to do this > >> automatically? This is ugly.... > >> > >> #define streq(a, b) ... if (_buildin_constant(b)) ... > >> > >> ? > >> > >>> - if (!strncmp(val, "enable", strlen("enable"))) { > >>> + if (!strncmp(val, "enable", sizeof("enable") - 1)) { > > > > While you're at it, there's no point using strncmp when you know the > > length of one of the strings beforehand. Just use memcmp, and don't > > subtract 1 from the sizeof value. > > http://www.gossamer-threads.com/lists/engine?do=post_attachment;postatt_id=41157;list=linux Interestingly, many (all?) of the changes in that patch are wrong because they don't try to match the terminating '\0'. As a result, they will match against extensions of the target string as well as the target string itself. Alan Stern