strncmp issue

Chuck Swiger cswiger at mac.com
Tue Apr 28 17:26:04 UTC 2009


Hi, Channa--

On Apr 28, 2009, at 2:52 AM, Channa wrote:
> I am using the freebsd implementation of strncmp for ARM which is an
> assembly implementation.
> I have a small doubt, when i tested the strncmp by passing the third  
> argument:
> 'n' as -1 the return values is  '0' instead it should '-1'.
> When the third argument  to strncmp is as below:
>
> ret = strncmp("a","b",-1)

Thanks for the thought, but strncmp() is defined to take a size_t as  
the third argument, which is unsigned (ie, uint32_t or uint64_t).

Presumably when you tell it to compare with length of -1, that will be  
converted to UINT_MAX or ULONG_MAX, and strncmp() will run until it  
finds a null somewhere in one the strings and then return the  
comparison result.  (Or get a segfault, perhaps, if you run off the  
end into unallocated address space.)

Regards,
-- 
-Chuck



More information about the freebsd-current mailing list