Module Name:    src
Committed By:   rillig
Date:           Sat May 21 19:02:15 UTC 2022

Modified Files:
        src/games/gomoku: main.c stoc.c

Log Message:
gomoku: fix error handling when reading moves from a file

The columns of the board are labeled from A to H and J to T, which makes
I5 or i5 an invalid coordinate.  Previously, reading this invalid
coordinate from a file resulted in the string "<6" appearing in the move
log.

The 'i' was converted into the nonexistent column 20, and PT(20, 5) got
an out-of-bounds argument, resulting in spot 120.  Converting this spot
back into coordinates resulted in PT(0, 6).  The '<' comes from
'letters[0]'.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/games/gomoku/main.c
cvs rdiff -u -r1.19 -r1.20 src/games/gomoku/stoc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.48 src/games/gomoku/main.c:1.49
--- src/games/gomoku/main.c:1.48	Sat May 21 17:19:10 2022
+++ src/games/gomoku/main.c	Sat May 21 19:02:14 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.48 2022/05/21 17:19:10 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.49 2022/05/21 19:02:14 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -36,7 +36,7 @@
 __COPYRIGHT("@(#) Copyright (c) 1994\
  The Regents of the University of California.  All rights reserved.");
 /*	@(#)main.c	8.4 (Berkeley) 5/4/95	*/
-__RCSID("$NetBSD: main.c,v 1.48 2022/05/21 17:19:10 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.49 2022/05/21 19:02:14 rillig Exp $");
 
 #include <sys/stat.h>
 #include <curses.h>
@@ -235,7 +235,7 @@ again:
 		switch (input[color]) {
 		case INPUTF: /* input comes from a file */
 			curmove = readinput(inputfp);
-			if (curmove != ILLEGAL)
+			if (curmove != EOF)
 				break;
 			/* Switch to another input source. */
 			switch (test) {
@@ -305,7 +305,7 @@ again:
 			curmove = pickmove(color);
 			break;
 		}
-		if (interactive) {
+		if (interactive && curmove != ILLEGAL) {
 			misclog("%3d%*s%-6s", movenum,
 			    color == BLACK ? 2 : 9, "", stoc(curmove));
 		}
@@ -372,7 +372,7 @@ readinput(FILE *fp)
 	while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
 		buf[pos++] = c;
 	buf[pos] = '\0';
-	return ctos(buf);
+	return c == EOF ? EOF : ctos(buf);
 }
 
 #ifdef DEBUG

Index: src/games/gomoku/stoc.c
diff -u src/games/gomoku/stoc.c:1.19 src/games/gomoku/stoc.c:1.20
--- src/games/gomoku/stoc.c:1.19	Sat May 21 17:19:10 2022
+++ src/games/gomoku/stoc.c	Sat May 21 19:02:14 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: stoc.c,v 1.19 2022/05/21 17:19:10 rillig Exp $	*/
+/*	$NetBSD: stoc.c,v 1.20 2022/05/21 19:02:14 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*	@(#)stoc.c	8.1 (Berkeley) 7/24/94	*/
-__RCSID("$NetBSD: stoc.c,v 1.19 2022/05/21 17:19:10 rillig Exp $");
+__RCSID("$NetBSD: stoc.c,v 1.20 2022/05/21 19:02:14 rillig Exp $");
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -43,19 +43,6 @@ __RCSID("$NetBSD: stoc.c,v 1.19 2022/05/
 
 const char	letters[]	= "<ABCDEFGHJKLMNOPQRST>";
 
-struct mvstr {
-	int	m_code;
-	const char	*m_text;
-};
-static	const struct	mvstr	mv[] = {
-	{ RESIGN,	"resign" },
-	{ RESIGN,	"quit" },
-	{ SAVE,		"save" },
-	{ -1,		0 }
-};
-
-static int lton(int);
-
 
 /*
  * Turn the spot number form of a move into the character form.
@@ -65,9 +52,10 @@ stoc(int s)
 {
 	static char buf[32];
 
-	for (int i = 0; mv[i].m_code >= 0; i++)
-		if (s == mv[i].m_code)
-			return mv[i].m_text;
+	if (s == RESIGN)
+		return "resign";
+	if (s == SAVE)
+		return "save";
 	snprintf(buf, sizeof(buf), "%c%d",
 	    letters[s % (BSZ + 1)], s / (BSZ + 1));
 	return buf;
@@ -80,28 +68,21 @@ int
 ctos(const char *mp)
 {
 
-	for (int i = 0; mv[i].m_code >= 0; i++)
-		if (strcmp(mp, mv[i].m_text) == 0)
-			return mv[i].m_code;
-	if (!isalpha((unsigned char)mp[0]))
+	if (strcmp(mp, "resign") == 0 || strcmp(mp, "quit") == 0)
+		return RESIGN;
+	if (strcmp(mp, "save") == 0)
+		return SAVE;
+
+	int letter = toupper((unsigned char)mp[0]);
+	int x = 1;
+	while (x <= BSZ && letters[x] != letter)
+		x++;
+	if (x > BSZ)
 		return ILLEGAL;
-	int i = atoi(&mp[1]);
-	if (i < 1 || i > 19)
-		return ILLEGAL;
-	return PT(lton((unsigned char)mp[0]), i);
-}
 
-/*
- * Turn a letter into a number.
- */
-static int
-lton(int c)
-{
-	int i;
+	int y = atoi(&mp[1]);
+	if (y < 1 || y > 19)
+		return ILLEGAL;
 
-	if (islower(c))
-		c = toupper(c);
-	for (i = 1; i <= BSZ && letters[i] != c; i++)
-		;
-	return i;
+	return PT(x, y);
 }

Reply via email to