From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1c5RpR-00055y-N8 for mharc-grub-devel@gnu.org; Sat, 12 Nov 2016 01:30:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58739) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c5RpO-00055c-S2 for grub-devel@gnu.org; Sat, 12 Nov 2016 01:30:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c5RpL-0002iF-NY for grub-devel@gnu.org; Sat, 12 Nov 2016 01:30:14 -0500 Received: from [2600:3c01::f03c:91ff:fe93:7077] (port=36441 helo=juniper.fatooh.org) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c5RpL-0002YQ-BJ for grub-devel@gnu.org; Sat, 12 Nov 2016 01:30:11 -0500 Received: from juniper.fatooh.org (juniper.fatooh.org [127.0.0.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by juniper.fatooh.org (Postfix) with ESMTPS id CAC74414CE for ; Fri, 11 Nov 2016 22:29:42 -0800 (PST) Received: from juniper.fatooh.org (juniper.fatooh.org [127.0.0.1]) by juniper.fatooh.org (Postfix) with ESMTP id AD1F7418E4 for ; Fri, 11 Nov 2016 22:29:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha1; c=simple; d=fatooh.org; h=subject:to :references:from:message-id:date:mime-version:in-reply-to :content-type:content-transfer-encoding; s=dkim; bh=WSABG7C7Nj4M kCD7NRondHx3/j4=; b=kK6SZL/CPp4vLL85xZXMBJUclesU52fnCkR5ezQD7Vtv WQPLgJNl0EDlxqaqmqVlqA/MD/+0kQK85k3hwhUFAk7IuJfMCRkS9KZUQ59rTmPs 71kYyz2kJNGjDQJE8aoG7OiEbyH0GtmFLz0Hhwqn/UnTgfn73zj2DvPwvkFFVcg= DomainKey-Signature: a=rsa-sha1; c=simple; d=fatooh.org; h=subject:to :references:from:message-id:date:mime-version:in-reply-to :content-type:content-transfer-encoding; q=dns; s=dkim; b=cvWAxG ukOcIjOZZ/NH0BXFUd7SrN0s/2TzOg1oIXPG99DAOmVCxXxA/3fXoaqpQ6VMfEaj JC/WYvxj6zRmsJuXd10HluY0gdZm2ofm53PyJfA3xbhdNKVD/S2xpKfjGEY9TXvh 8ssULMKHpAVknmydjDDy5Dov5JUJ3MOE7P8ko= Received: from [198.18.0.3] (173-164-227-83-SFBA.hfc.comcastbusiness.net [173.164.227.83]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by juniper.fatooh.org (Postfix) with ESMTPSA id 98FF7414CE for ; Fri, 11 Nov 2016 22:29:42 -0800 (PST) Subject: Re: [PATCH] fix detection of non-LUKS CRYPT To: The development of GNU GRUB References: <43313b6e-80c2-fafb-286b-47db8b180f6a@fatooh.org> <08fec14a-a356-04c9-b135-80da3005efc3@gmail.com> <3abef268-adaf-f324-94aa-6f97f7517ec0@fatooh.org> <603e109a-e60a-0d2c-c246-4adaa022caef@gmail.com> <150130c6-5eb9-66cc-462c-5b253fb63700@fatooh.org> From: Corey Hickey Message-ID: <498d735a-8496-16ba-cf42-7b0230336034@fatooh.org> Date: Fri, 11 Nov 2016 22:29:41 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2600:3c01::f03c:91ff:fe93:7077 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 12 Nov 2016 06:30:16 -0000 On 2016-11-07 03:04, Andrei Borzenkov wrote: >> My initial idea matched your earlier example: >> >> #define STARTS_WITH(s1, s2) (strncmp((s1), (s2), sizeof(s2) - 1) == 0) >> >> Am I failing to see a use case where that breaks? >> > > char *prefix = "CRYPT-LUKS1-"; > sizeof (prefix) == 4 or 8 depending on platform. Not 13 in any case. Yes, the second argument would always have to be a string literal. Not ideal, but I don't know a better way. Enforcing that the argument be a string literal would work, but I can't find a way to do that. The best I have come up with so far is this: #define STARTS_WITH(s1, s2) (strncmp((s1), (s2 ""), sizeof(s2) - 1) == 0) The string concatenation is intended to require s2 to be a string literal and fail at compilation time otherwise. Unfortunately, it is possible to defeat--s2 only has to _end_ with a string literal. It breaks with something like this: char *eight = "12345678" char *nine = "123456789" STARTS_WITH(eight, 1 ? nine : "foo")) >> The one bad thing I can see is that it can fail poorly if somebody reverses >> the arguments by mistake. In that case, sizeof(s2) returns the pointer size >> rather than the length of the string literal. I don't know how to enforce >> that a macro argument be a string literal. An alternate idea is: >> >> #define STARTS_WITH(s1, s2) (strncmp((s1), (s2), strlen(s2)) == 0) >> > > This adds additional overhead of function call instead of constant > expression; unless we are sure gcc/clang are smart enough to reduce it > for constant arguments. > >> That should work with arguments in either order (though of course s2 is >> expected to be smaller). I don't know of a drawback other than strlen >> presumably being a bit slower, but I didn't get the impression any of the >> code in question was performance-sensitive. >> > > For user space it probably does not matter much; but for boot time > code it also increases code size and better be avoided. > > Even for user space it is called often so if we can reduce overhead, > let's do it. I'm open to ideas. -Corey