Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752444AbZKLKhy (ORCPT ); Thu, 12 Nov 2009 05:37:54 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1751649AbZKLKhx (ORCPT ); Thu, 12 Nov 2009 05:37:53 -0500 Received: from mgw2.diku.dk ([130.225.96.92]:33938 "EHLO mgw2.diku.dk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751508AbZKLKhw (ORCPT ); Thu, 12 Nov 2009 05:37:52 -0500 Date: Thu, 12 Nov 2009 11:37:55 +0100 (CET) From: Julia Lawall To: Bernd Petrovitsch Cc: al@alarsen.net, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: sizeof vs strlen (was Re: [PATCH 4/4] fs/qnx4: decrement sizeof size in strncmp) In-Reply-To: <1258021335.3689.41.camel@tara.firmix.at> Message-ID: References: <1258021335.3689.41.camel@tara.firmix.at> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1149 Lines: 30 On Thu, 12 Nov 2009, Bernd Petrovitsch wrote: > On Thu, 2009-11-12 at 08:49 +0100, Julia Lawall wrote: > > From: Julia Lawall > > > > As observed by Joe Perches, sizeof of a constant string includes the > > trailing 0. If what is wanted is to check the initial characters of > > another string, this trailing 0 should not be taken into account. If an > > exact match is wanted, strcmp should be used instead. > [...] > > strncmp(foo, abc, > > - sizeof(abc) > > + sizeof(abc)-1 > > ) > > // > Am I the only one who find "strlen()" instead of "sizeof()-1" here much > more readable (and to the point). > > As for run-time, it shouldn't make a difference for static/constant > strings as gcc should be able calculate the length at compile time. And > if the string is not constant, sizeof() is probably wrong anyways. The rule specifies that the string is a constant. julia -- 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/