shithub: puzzles

Download patch

ref: 75225284c0db99aa14f72a630cc11d3b91ed79cf
parent: fda72b0f1edeedb684bdd845b43d7bbed4607d26
author: Simon Tatham <anakin@pobox.com>
date: Thu Jun 23 19:11:59 EDT 2005

Array overflow fix from James Harvey.

[originally from svn r6005]

--- a/guess.c
+++ b/guess.c
@@ -221,12 +221,9 @@
 
 static pegrow dup_pegrow(pegrow pegs)
 {
-    pegrow newpegs = snew(struct pegrow);
+    pegrow newpegs = new_pegrow(pegs->npegs);
 
-    newpegs->npegs = pegs->npegs;
-    newpegs->pegs = snewn(newpegs->npegs, int);
     memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
-    newpegs->feedback = snewn(newpegs->npegs, int);
     memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
 
     return newpegs;
@@ -325,6 +322,7 @@
     int i;
 
     *ret = *state;
+
     ret->guesses = snewn(state->params.nguesses, pegrow);
     for (i = 0; i < state->params.nguesses; i++)
 	ret->guesses[i] = dup_pegrow(state->guesses[i]);
@@ -463,8 +461,9 @@
     nrequired = params->allow_blank ? 1 : params->npegs;
 
     for (i = 0; i < params->npegs; i++) {
-        if (pegs->pegs[i] > 0) {
-            colcount->pegs[pegs->pegs[i]]++;
+        int c = pegs->pegs[i];
+        if (c > 0) {
+            colcount->pegs[c-1]++;
             nset++;
         }
     }