Portability of some overlapping strcpy or memcpy calls

Samuel C Kendall kendall at wjh12.harvard.edu
Thu Mar 9 14:24:33 AEST 1989


Consider the following function call:

	memcpy(p, p + M, N)

where p is a char*, M is nonnegative, N is positive, and M < N.  This
is an overlapping copy, where the bytes are being copied to the left (M
> 0) or onto themselves (M == 0).  I am interested in finding out if
this call to memcpy, and similar calls to memccpy, strcpy, and strncpy,
are portable.

I'm interested in practical portability, not theoretical.  This call is
obviously illegal, since overlapping copies are forbidden by ANSI and
by the Unix man pages I've seen.  However, the copy succeeds in all the
implementations I've tested (Sun, Vax), and it makes sense -- you have
to go out of your way to make this kind of overlap fail.  If someone
knows of an implementation where it does fail, please let me know which
implementation it is and how it fails BY MAIL.  I'll summarize to the
net if anyone shows interest.

Details: how could this call fail to copy correctly?  The
implementations I know copy left-to-right.  An implementation that
copied right-to-left would definitely fail.  Also, an implementation
that copied in larger-than-byte units, and which couldn't handle
overlapping units, would fail for small M.  Finally, an implementation
with run-time checking for this condition would fail.

Our latest version of Saber-C (a C environment with lots of run-time
checking) checks for overlapping copies to the right, but purposely
omits checks for the above cases.  I'm trying to figure out if a
"portability mode" should be more stringent.

	Sam Kendall	  kendall%saber at harvard.harvard.edu
	Saber Software, Inc.	      harvard!saber!kendall



More information about the Comp.lang.c mailing list