9front - general discussion about 9front
 help / color / mirror / Atom feed
* MOVE implementation in games/mix
@ 2020-11-21 15:39 nicolagi
  0 siblings, 0 replies; only message in thread
From: nicolagi @ 2020-11-21 15:39 UTC (permalink / raw)
  To: 9front

Largely unimportant but I'll share anyway...

I was toying with games/mix to do some exercises from TACP.  It seems
to me MOVE shouldn't be implemented with memcpy() but with an explicit
for loop, in order to surely comply with the description of the
behavior in case of overlapping memory regions at page 135.  It seems
to me that this is expected to be exploited in exercise 16.  I've
changed my copy of games/mix with the below; probably not idiomatic C
but seems to do the trick.  Am I totally wrong?

diff -r 551ecd2fd50b sys/src/games/mix/mix.c
--- a/sys/src/games/mix/mix.c	Thu Nov 19 23:05:26 2020 -0800
+++ b/sys/src/games/mix/mix.c	Sat Nov 21 15:36:38 2020 +0000
@@ -736,7 +736,10 @@
 	d = mval(ri[1], 0, MASK2);
 	if(d < 0 || d > 4000)
 		vmerror("Bad address MOVE %d", d);
-	memcpy(cells+d, cells+s, f*sizeof(u32int));
+	u32int *p = cells + d, *q = cells + s;
+	while(p < cells+d+f)
+		*p++ = *q++;
+	/* memcpy(cells+d, cells+s, f*sizeof(u32int)); */
 	d += f;
 	d &= MASK2;
 	ri[1] = d < 0 ? -d|SIGNB : d;



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-11-21 15:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-21 15:39 MOVE implementation in games/mix nicolagi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).