This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC] Do not call memmove when memcpy suffices.


On Sun, Aug 18, 2013 at 07:55:46PM +0000, Joseph S. Myers wrote:
> On Tue, 13 Aug 2013, Ondrej Bilka wrote:
> 
> > #define memmove(_dest, _src, _n)                                              \
> >   ({                                                                          \
> >      char *dest = (_dest);                                                    \
> >      char *src = (_src);                                                      \
> >      size_t n = (_n);                                                         \
> >      if (__glibc_likely ((size_t)((src - n) - dest) >= 2 * n))                \
> >        return memcpy (dest, src, n);                                          \
> >      else                                                                     \
> >        return memmove (dest, src, n);                                         \
> >    })
> 
> I don't see why this is safe.  Say the arguments overlap with src < dest, 
> ((src - n) - dest) negative and becoming large and positive when cast to 
> size_t,

And did you compute conditions how small dest must be for wraparound to
occur?

>  so the test passes and memcpy is called - but the glibc ABI 
> guarantee for memcpy requires that there is no overlap in either direction 
> (except maybe in the case where src == dest).  Do you mean ((src + n) - 
> dest)?
> 
> -- 
> Joseph S. Myers
> joseph@codesourcery.com

-- 

Sand fleas eating the Internet cables


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]