Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 May 2013 12:25:23 +0200
From:      Adam Nowacki <nowakpl@platinum.linux.pl>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Performance improvement to strnlen().
Message-ID:  <51A33493.9010702@platinum.linux.pl>
In-Reply-To: <CAKw7uVi2oofoy=yJQiHR927oGfy74UuYFq68r=C=gUVXBXZuKw@mail.gmail.com>
References:  <2adc4d8e7c55e92d935f61efb4c9d723@lthomas.net> <CAKw7uVi2oofoy=yJQiHR927oGfy74UuYFq68r=C=gUVXBXZuKw@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?51A33493.9010702>