chiark / gitweb /
Fix return value from newgame_undo_deserialise_read.
authorSimon Tatham <anakin@pobox.com>
Thu, 21 Jun 2018 18:02:21 +0000 (19:02 +0100)
committerSimon Tatham <anakin@pobox.com>
Thu, 21 Jun 2018 18:02:21 +0000 (19:02 +0100)
The read function used by midend_deserialise and friends is expected
never to perform a partial read (the main deserialisation code always
knows how many bytes it can expect to see), so it's specified to
return simply TRUE or FALSE for success/failure, rather than the
number of bytes read.

This probably wasn't breaking anything, since in the case of
deserialising from an internal memory buffer a short read could only
arise due to an outright bug constructing the buffer. But now I've
spotted it, I should fix it.

midend.c

index 1a08ef690df27fc2d036d440b83899c1054c361c..5a9c54863202731fabc97a11b3f1c8242cdb27c8 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -564,10 +564,12 @@ static int newgame_undo_deserialise_read(void *ctx, void *buf, int len)
 {
     struct newgame_undo_deserialise_read_ctx *const rctx = ctx;
 
-    int use = min(len, rctx->len - rctx->pos);
-    memcpy(buf, rctx->ser->buf + rctx->pos, use);
-    rctx->pos += use;
-    return use;
+    if (len > rctx->len - rctx->pos)
+        return FALSE;
+
+    memcpy(buf, rctx->ser->buf + rctx->pos, len);
+    rctx->pos += len;
+    return TRUE;
 }
 
 struct newgame_undo_deserialise_check_ctx {