Author: Dann Corbit
Date: 13:15:23 03/20/04
Go up one level in this thread
On March 20, 2004 at 08:16:03, Keafiwa wrote:
>//This code taken from part of TCSP main.c shows it's xboard procedure
>//code seems to be entering an infinite loop somewhere, on my machine(p4
>//windows xp) can anyone suggest wht might be wrong and how i can correct it
>
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>#include <signal.h>
>#include "defs.h"
>#include "data.h"
>#include "protos.h"
>void xboard()
>{
> int computer_side;
> char line[256], command[256];
> int m;
> int post = 0;
// If you don't have this stuff in main(), then you darn sure need it here:
/* No buffering, please... */
setbuf(stdout, NULL);
setbuf(stdin, NULL);
/* and I *really* mean it, too! */
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
fflush(NULL);
> signal(SIGINT, SIG_IGN);
> printf("\n");
> init_board();
> gen();
> computer_side = EMPTY;
> for (;;) {
> fflush(stdout);
> if (side == computer_side) {
> think(post);
> if (!pv[0][0].u) {
> computer_side = EMPTY;
> continue;
> }
> printf("move %s\n", move_str(pv[0][0].b));
> makemove(pv[0][0].b);
> ply = 0;
> gen();
> print_result();
> continue;
> }
> if (!fgets(line, 256, stdin))
> return;
> if (line[0] == '\n')
> continue;
> sscanf(line, "%s", command);
This is as bad as gets(). Don't ever do that. You open any computer that uses
your program to worm attacks. Imagine if the supplied string is 10,000
characters long and contains machine instructions to format your hard disk.
People [I use that term loosely here] deliberately exploit programs that use
reads of %s with no length specification and also that use gets(). Also, you
should always check the return of sscanf() and friends.
> if (!strcmp(command, "xboard"))
> continue;
> if (!strcmp(command, "new")) {
> init_board();
> gen();
> computer_side = DARK;
> continue;
> }
> if (!strcmp(command, "quit"))
> return;
> if (!strcmp(command, "force")) {
> computer_side = EMPTY;
> continue;
> }
> if (!strcmp(command, "white")) {
> side = LIGHT;
> xside = DARK;
> gen();
> computer_side = DARK;
> continue;
> }
> if (!strcmp(command, "black")) {
> side = DARK;
> xside = LIGHT;
> gen();
> computer_side = LIGHT;
> continue;
> }
> if (!strcmp(command, "st")) {
> sscanf(line, "st %d", &max_time);
Always check the return of sscanf() and friends.
> max_time *= 1000;
> max_depth = 32;
> continue;
> }
> if (!strcmp(command, "sd")) {
> sscanf(line, "sd %d", &max_depth);
Always check the return of sscanf() and friends.
> max_time = 1 << 25;
> continue;
> }
> if (!strcmp(command, "time")) {
> sscanf(line, "time %d", &max_time);
Always check the return of sscanf() and friends.
> max_time *= 10;
> max_time /= 30;
> max_depth = 32;
> continue;
> }
> if (!strcmp(command, "otim")) {
> continue;
> }
> if (!strcmp(command, "go")) {
> computer_side = side;
> continue;
> }
> if (!strcmp(command, "hint")) {
> think(0);
> if (!pv[0][0].u)
> continue;
> printf("Hint: %s\n", move_str(pv[0][0].b));
> continue;
> }
> if (!strcmp(command, "undo")) {
> if (!hply)
> continue;
> takeback();
> ply = 0;
> gen();
> continue;
> }
> if (!strcmp(command, "remove")) {
> if (hply < 2)
> continue;
> takeback();
> takeback();
> ply = 0;
> gen();
> continue;
> }
> if (!strcmp(command, "post")) {
> post = 2;
> continue;
> }
> if (!strcmp(command, "nopost")) {
> post = 0;
> continue;
> }
> m = parse_move(line);
> if (m == -1 || !makemove(gen_dat[m].m.b))
> printf("Error (unknown command): %s\n", command);
> else {
> ply = 0;
> gen();
> print_result();
> }
> }
>}
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.