On Mon, Sep 6, 2010 at 6:35 PM, John E. / TDM <[email protected]> wrote:
>  On 9/6/2010 6:14 AM, Ozkan Sezer wrote:
>>
>> strnlen doesn't exist in msvcrt.dll from x86-winxp
>>
>> we can implement it roughly like:
>>
>> size_t __cdecl strnlen (const char *s, size_t maxlen)
>> {
>>   size_t siz = __builtin_strlen(s);
>>   if (siz>  maxlen) siz = maxlen;
>>   return siz;
>> }
>
> This is not a good implementation of strnlen. The purpose of using strnlen
> is to avoid reading past the end of a buffer of known size --
>
> Quote from
> <http://msdn.microsoft.com/en-us/library/z50ty2zh%28VS.80%29.aspx>:
> *
>>
>> *strnlen* is not a replacement for *strlen*; *strnlen* is only intended to
>> be used to calculate the size of incoming untrusted data in a buffer of
>> known size (such as a network packet). *strnlen* will calculate the length
>> but not walk past the end of your buffer if the string is unterminated.
>
> *
> Here is a correct (albeit slightly less performant than assembly) strnlen
> implementation:
>
> size_t strnlen(const char* s, size_t maxlen)
> {
>  const char* s2 = s;
>  while (s2 - s < maxlen && *s2)
>    ++s2;
>  return s2 - s;
> }
>
> Note that this is in accordance with MS' implementation:
>
>> However, *strnlen* and *strnlen_l* interpret the string as a single-byte
>> character string, so its return value is always equal to the number of
>> bytes, even if the string contains multibyte characters.
>
> -John E. / TDM
>

Kai, OK I guess?

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to