9front - general discussion about 9front
 help / color / mirror / Atom feed
From: nicolagi@sdf.org
To: 9front@9front.org
Subject: MOVE implementation in games/mix
Date: Sat, 21 Nov 2020 15:39:21 +0000	[thread overview]
Message-ID: <98B0A90C39B4474EE7DEEFD6C04B56B9@sdf.org> (raw)

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;



                 reply	other threads:[~2020-11-21 15:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98B0A90C39B4474EE7DEEFD6C04B56B9@sdf.org \
    --to=nicolagi@sdf.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).