Skip site navigation (1) Skip section navigation (2)

Re: fix memcpy() overlap

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>,Neil Conway <neilc(at)samurai(dot)com>,PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: fix memcpy() overlap
Date: 2004-02-03 16:26:34
Message-ID: 200402031626.i13GQYS12079@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-patches
Tom Lane wrote:
> Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com> writes:
> > On Mon, 2 Feb 2004, Tom Lane wrote:
> >> This isn't a bug, and I see no reason to clutter the code just to shut
> >> up valgrind.
> 
> > Isn't memcpy on overlapping (even entirely overlapping) buffers undefined
> > behavior unless the count is 0?
> 
> The reason that the spec describes overlapped memcpy as undefined is
> that it does not want to restrict which direction the copy occurs in
> (proceeding from lower to higher memory addresses or vice versa).
> memmove is constrained to do the copy in the direction that will avoid
> failure when the source and destination partially overlap.  But memcpy
> is expected to do whichever is fastest on the particular architecture,
> without concern for possible overlap.  (Offhand I've never heard of a
> machine where memcpy doesn't work lower-to-higher, but maybe there is
> one.)
> 
> However, when the source and destination buffers are the same, it does
> not matter which direction you copy in; you are picking up and putting
> down the same bytes at the same addresses no matter what order you
> process the bytes in.  There is no implementation in existence that will
> produce an unwanted result.
> 
> If you want to argue about dependencies on implementation details that
> are theoretically undefined according to the ANSI C spec, we have tons
> more beyond this one.  F'r instance, depending on twos-complement
> arithmetic is illegal per spec also ...

Isn't memmove() for overlaping regions?  That's what my BSD manual page
says.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman(at)candle(dot)pha(dot)pa(dot)us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

In response to

Responses

pgsql-patches by date

Next:From: Andrew DunstanDate: 2004-02-03 16:29:05
Subject: Re: [PATCHES] log session end - again
Previous:From: Bruce MomjianDate: 2004-02-03 16:14:34
Subject: Re: [PATCHES] log session end - again

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group