From owner-freebsd-hackers@FreeBSD.ORG Mon May 27 10:31:02 2013 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id B24E88FD for ; Mon, 27 May 2013 10:31:02 +0000 (UTC) (envelope-from nowakpl@platinum.linux.pl) Received: from platinum.linux.pl (platinum.edu.pl [81.161.192.4]) by mx1.freebsd.org (Postfix) with ESMTP id 771732B7 for ; Mon, 27 May 2013 10:31:01 +0000 (UTC) Received: by platinum.linux.pl (Postfix, from userid 87) id 5087D5FD07; Mon, 27 May 2013 12:25:39 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on platinum.linux.pl X-Spam-Level: X-Spam-Status: No, score=-1.3 required=3.0 tests=ALL_TRUSTED,AWL autolearn=disabled version=3.3.2 Received: from [10.255.0.2] (unknown [83.151.38.73]) by platinum.linux.pl (Postfix) with ESMTPA id 17BD15FD05 for ; Mon, 27 May 2013 12:25:39 +0200 (CEST) Message-ID: <51A33493.9010702@platinum.linux.pl> Date: Mon, 27 May 2013 12:25:23 +0200 From: Adam Nowacki User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: freebsd-hackers@freebsd.org Subject: Re: Performance improvement to strnlen(). References: <2adc4d8e7c55e92d935f61efb4c9d723@lthomas.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 May 2013 10:31:02 -0000 On 2013-05-27 10:37, Václav Zeman wrote: > On 26 May 2013 21:01, Lee Thomas wrote: >> On 2013-05-26 08:00, Václav Zeman wrote: >>> >>> On 05/25/2013 10:27 PM, Lee Thomas wrote: >>>> >>>> + lp = (const unsigned long *)((uintptr_t)str & ~LONGPTR_MASK); >>>> + va = (*lp - mask01); >>>> + vb = ((~*lp) & mask80); >>> >>> I do not think that this correct C. This is type punning violating the >>> rules of the language. >> >> >> Hello Václav, >> >> The aliasing here is safe, because there are no writes through either of the >> pointers, and the reads are correctly aligned. > I disagree. IANALL but AFAIK, this is simply not allowed by the > language => UB => even though it seems to work in this instance, you > are just lucky the UB is actually doing what you expect. It is not possible to tell if the result would be undefined by looking at the strnlen function alone. Internally it doesn't break any aliasing rules as char and long are allowed to alias. UB can only happen when the input string was created with incompatible type (not char and not long) and the strnlen function got inlined. Preventing inlining would be sufficient to guarantee correctness in any case.