Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Apr 2009 10:25:43 -0700
From:      Chuck Swiger <cswiger@mac.com>
To:        Channa <channa.kad@gmail.com>
Cc:        freebsd-arm@FreeBSD.org, FreeBSD Current <freebsd-current@freebsd.org>
Subject:   Re: strncmp issue
Message-ID:  <FBA2BA91-D13F-41B2-95B6-21DE8E020B6F@mac.com>
In-Reply-To: <515c64960904280252sc9fe2afy24e8db8ab13b13e4@mail.gmail.com>
References:  <515c64960904280252sc9fe2afy24e8db8ab13b13e4@mail.gmail.com>

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?FBA2BA91-D13F-41B2-95B6-21DE8E020B6F>