Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Jul 2010 10:21:20 -0400
From:      Lowell Gilbert <freebsd-chat-local@be-well.ilk.org>
To:        deeptech71@gmail.com,  freebsd-chat@freebsd.org
Subject:   Re: is strlen()'s read-4-bytes-ahead a standard?
Message-ID:  <44mxtr33tr.fsf@be-well.ilk.org>
In-Reply-To: <868w5bkg45.fsf@ds4.des.no> ("Dag-Erling =?iso-8859-1?Q?Sm=F8?= =?iso-8859-1?Q?rgrav=22's?= message of "Fri, 16 Jul 2010 10:03:54 %2B0200")
References:  <AANLkTima5e5pXRrLkOP-3vzHrXWawhDOyP25xgHBPqw_@mail.gmail.com> <4C3F8F2E.9080705@delphij.net> <868w5bkg45.fsf@ds4.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
Dag-Erling Sm=F8rgrav <des@des.no> writes:

> Xin LI <delphij@delphij.net> writes:
>> deeptech71@gmail.com writes:
>> > Some C implementations use the read-4-bytes-ahead technique to speed
>> > up strlen(). Does the C standard state anything about strlen() being
>> > allowed to read past the terminating zero?
>> It's not 4-bytes-ahead, but read a whole (aligned) word at one time.
>> I think C standard does not dictate in this detail.
>
> My guess is that it invokes undefined behavior, but it doesn't matter in
> practice, because as long as you only read one aligned word at a time,
> and as long as the pointer you got is valid and points to a properly
> terminated string, you might read trash (which is expected), but you
> will never read unmapped memory.

strlen() is part of the implementation, and doesn't have to worry about
undefined behaviour.  Any conforming *program* will work properly, and
that's all the standard library needs to ensure.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?44mxtr33tr.fsf>