Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/games/gomoku gomoku: fix error handling when reading moves f...
details: https://anonhg.NetBSD.org/src/rev/6c0c977baec3
branches: trunk
changeset: 366284:6c0c977baec3
user: rillig <rillig%NetBSD.org@localhost>
date: Sat May 21 19:02:14 2022 +0000
description:
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]'.
diffstat:
games/gomoku/main.c | 10 ++++----
games/gomoku/stoc.c | 61 ++++++++++++++++++----------------------------------
2 files changed, 26 insertions(+), 45 deletions(-)
diffs (141 lines):
diff -r d778c9bff905 -r 6c0c977baec3 games/gomoku/main.c
--- a/games/gomoku/main.c Sat May 21 17:50:21 2022 +0000
+++ b/games/gomoku/main.c Sat May 21 19:02:14 2022 +0000
@@ -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 @@
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 @@
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 @@
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
diff -r d778c9bff905 -r 6c0c977baec3 games/gomoku/stoc.c
--- a/games/gomoku/stoc.c Sat May 21 17:50:21 2022 +0000
+++ b/games/gomoku/stoc.c Sat May 21 19:02:14 2022 +0000
@@ -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 @@
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 @@
{
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 @@
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]))
- return ILLEGAL;
- int i = atoi(&mp[1]);
- if (i < 1 || i > 19)
- return ILLEGAL;
- return PT(lton((unsigned char)mp[0]), i);
-}
+ if (strcmp(mp, "resign") == 0 || strcmp(mp, "quit") == 0)
+ return RESIGN;
+ if (strcmp(mp, "save") == 0)
+ return SAVE;
-/*
- * Turn a letter into a number.
- */
-static int
-lton(int c)
-{
- int i;
+ int letter = toupper((unsigned char)mp[0]);
+ int x = 1;
+ while (x <= BSZ && letters[x] != letter)
+ x++;
+ if (x > BSZ)
+ return ILLEGAL;
- if (islower(c))
- c = toupper(c);
- for (i = 1; i <= BSZ && letters[i] != c; i++)
- ;
- return i;
+ int y = atoi(&mp[1]);
+ if (y < 1 || y > 19)
+ return ILLEGAL;
+
+ return PT(x, y);
}
Home |
Main Index |
Thread Index |
Old Index