Re: strncmp issue

From: Chuck Swiger <cswiger_at_mac.com>
Date: Tue, 28 Apr 2009 10:25:43 -0700
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
Received on Tue Apr 28 2009 - 15:26:04 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:46 UTC