bug-gnu-chess
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

misc patch


From: David Holland
Subject: misc patch
Date: Fri, 4 Jan 2002 16:12:19 -0500 (EST)

I was trying to build gnuchess (5.03 beta from ftp.gnu.org) for an odd
platform today, and discovered a bunch of issues. This patch:

  (1) removes a bunch of unused variables;
  (2) corrects some function prototypes and standard header inclusions;
  (3) corrects some dubious declarations;
  (4) fixes some incorrect printf arguments;
  (5) removes unnecessary usage of struct timezone;
  (6) permits a clean build with gcc -Wall -W (at least with an oldish gcc).

In a couple of places I spotted problems that I wasn't immediately
sure how to fix properly; these are marked with "XXX". Please don't
apply this patch without at least looking at those.

Also, two prototypes that weren't in common.h but should have been
were moved there into a category of "stuff that wasn't here but should be".
Please categorize them more properly...

This patch is also not heavily tested, though it appears to cause no
problems.

One issue I discovered that this patch does *not* address is the usage

      sscanf (inputstr, "%s %[^\n]", cmd, inputstr);

which produces (AFAIK) undefined behavior in C. This usage is found in
both cmd.c and epd.c, and really ought to be cleaned up - but since
doing so would require reasonably extensive changes I thought I'd
mention it first before sending a diff.

Finally, it appears to me that there's no need for either the autoconf
test for sqrt or the -lm listed in the libraries line - the patch
doesn't address this either though.

diff -ru ../chess-5.03beta/src/book.c ./book.c
--- ../chess-5.03beta/src/book.c        Mon Dec 17 07:35:43 2001
+++ ./book.c    Fri Jan  4 15:39:48 2002
@@ -41,11 +41,8 @@
 #define MAXMATCH 100
 #define MAXPEEK 10
                           
-static int bookcnt, bigbookcnt = 0, variations = 0, bigvariations = 0;
-static int lastbookcnt;
+static int bookcnt, bigbookcnt = 0;
 static int runningbookcnt = 0;
-static char linebuf[MAXVAR][MAXLINE];  
-static char smallbuf[MAXLINE/2];
 HashType posshash[MAXMOVES];
 struct hashtype {
   short wins;
@@ -54,11 +51,10 @@
   HashType key;
 } bookpos[MAXBOOK];
 
-int compare(leaf *, leaf *);
-int compare(a, b)
-leaf *a;
-leaf *b;
+static int compare(const void *aa, const void *bb)
 {
+    const leaf *a = (const leaf *)aa;
+    const leaf *b = (const leaf *)bb;
     if (b->score > a->score) return(1);
     else if (b->score < a->score) return(-1);
     else return(0);
@@ -74,8 +70,8 @@
 void BookBuilder(short depth, int score, short result, short side)
 {
   FILE *wfp,*rfp;
-  register i;
-  int targetslot, found = 0, storeit = 0, tot;
+  register int i;
+  int targetslot, found = 0, storeit = 0;
   if (depth == -1 && score == -1) {
     if ((rfp = fopen(BOOKRUN,"r+b")) != NULL) {
       printf("Opened existing book!\n");
@@ -88,7 +84,7 @@
        return;
       }
     }
-    fread(&bookpos,sizeof(struct hashtype),runningbookcnt,wfp);
+    /*fread(&bookpos,sizeof(struct hashtype),runningbookcnt,wfp);*/ /*XXX ? */
     runningbookcnt = fread(&bookpos,sizeof(struct hashtype),MAXBOOK,rfp);
     fclose(rfp);
     printf("Read %d book positions\n",runningbookcnt);
@@ -236,7 +232,7 @@
          SANMove(m[matches[i]].move,1);
           tot = r[matches[i]].wins+r[matches[i]].draws+r[matches[i]].losses;
          fprintf(ofp,"%s(%2.0f/%d/%d/%d) ",SANmv,
-               100.0*(r[matches[i]].wins+(r[matches[i]].draws/2))/tot,
+               (100.0*(r[matches[i]].wins+(r[matches[i]].draws/2)))/tot,
                r[matches[i]].wins,
                r[matches[i]].losses,
                r[matches[i]].draws);
@@ -256,7 +252,7 @@
         tot = r[matches[k]].wins+r[matches[k]].draws+r[matches[k]].losses;
         if (tot != 0)
           printf("B p=%2.0f\n",
-          100*(r[matches[k]].wins+r[matches[k]].draws)/tot);
+          (100.0*(r[matches[k]].wins+r[matches[k]].draws))/tot);
         else
           printf("p=NO EXPERIENCES\n");
       }
diff -ru ../chess-5.03beta/src/cmd.c ./cmd.c
--- ../chess-5.03beta/src/cmd.c Mon Dec 17 07:35:27 2001
+++ ./cmd.c     Fri Jan  4 15:42:04 2002
@@ -40,9 +40,9 @@
 
 #define prompt ':'
 
-extern short stage, InChkDummy, terminal;
+extern short stage;
 static char inputstr[128], cmd[128], file[128], s[128], logfile[128], 
-            gamefile[128],resultstr[128],userinput[128];
+            gamefile[128],userinput[128];
 
 char subcmd[128],setting[128],subsetting[128];
 
@@ -230,7 +230,7 @@
          *x = '\000';
          break;
        }
-        *x++;
+       x++;
       }
       suffix = 0;
       for (;;) {
@@ -254,7 +254,7 @@
      }
    }   
    else if (strcmp (cmd, "rating") == 0) {
-      sscanf(inputstr,"%d %d",&myrating,&opprating); 
+      sscanf(inputstr,"%hd %hd",&myrating,&opprating); 
       fprintf(ofp,"my rating = %d, opponent rating = 
%d\n",myrating,opprating); 
       /* Change randomness of book based on opponent rating. */
       /* Basically we play narrower book the higher the opponent */
@@ -380,7 +380,7 @@
         if (!(flags & XBOARD)) {
           printf ("Time Control: %d moves in %.2f secs\n", 
                  MoveLimit[white], TimeLimit[white]);
-          printf("Fischer increment of %.2f seconds\n",TCinc);
+          printf("Fischer increment of %.2f seconds\n",(double)TCinc);
         }
       }
    }
@@ -501,7 +501,6 @@
  ************************************************************************/
 {
    char cmd[10];
-   short i;
 
    sscanf (subcmd, "%s %[^\n]", cmd, subcmd);
    if (strcmp (cmd, "board") == 0)
@@ -551,7 +550,7 @@
    {
       int s, wp, bp, wk, bk;
       short r, c, sq;
-      BitBoard *b, x;
+      BitBoard *b;
 
       phase = PHASE;
       GenAtaks ();
@@ -637,7 +636,6 @@
  *
  *************************************************************************/
 {
-   int ncmds;
    char cmd[10];
 
    sscanf (subcmd, "%s %[^\n]", cmd, subcmd);
diff -ru ../chess-5.03beta/src/common.h ./common.h
--- ../chess-5.03beta/src/common.h      Tue Oct 23 12:03:56 2001
+++ ./common.h  Fri Jan  4 15:02:02 2002
@@ -574,3 +574,7 @@
 int DBSearchPlayer (char *player);
 void DBUpdatePlayer (char *player, char *resultstr);
 void DBTest (void);
+
+/* stuff that wasn't here but should be */
+void ShowGame (void);
+void ShowTime (void);
diff -ru ../chess-5.03beta/src/eval.c ./eval.c
--- ../chess-5.03beta/src/eval.c        Thu Dec 20 10:24:22 2001
+++ ./eval.c    Fri Jan  4 15:37:07 2002
@@ -436,7 +436,7 @@
  *
  ***************************************************************************/
 {
-   short s, s1, sq, xside, n;
+   short s, s1, sq, xside;
    short EnemyKing;
    BitBoard c, t;
 
@@ -821,7 +821,7 @@
  ***************************************************************************/
 {
    short s, sq, sq1, n, n1, n2, xside, file, fsq, rank;
-   BitBoard b, x, x1, x2;
+   BitBoard b, x;
 
    s = 0;
    xside = 1^side;
diff -ru ../chess-5.03beta/src/iterate.c ./iterate.c
--- ../chess-5.03beta/src/iterate.c     Tue Oct 23 12:03:56 2001
+++ ./iterate.c Fri Jan  4 15:43:13 2002
@@ -32,9 +32,7 @@
 #define WINDOW 75
 
 static struct timeval t1, t2;
-static struct timezone tz;
 extern short stage;
-short InChkDummy, terminal;
 
 void Iterate (void)
 /**************************************************************************
@@ -46,7 +44,6 @@
 {
    short side;
    int score, RootAlpha, RootBeta;
-   short terminal, InChkDummy;
 
    side = board.side;
 
@@ -95,7 +92,7 @@
       /* To prevent huge surplus build-up. */
       if (TCinc != 0)
         if (SearchTime < TCinc) {
-          printf("TimeLimit[%s] = %6.2f\n",TimeLimit[side]);
+          printf("TimeLimit[%s] = %6.2f\n","???",TimeLimit[side]); /* XXX */
           if (TimeLimit[side] > 30) {   /* Only if > 15 seconds left */
             SearchTime = TCinc;
           }
@@ -108,7 +105,7 @@
    GenMoves (1);
    FilterIllegalMoves (1); 
    SortRoot ();
-   gettimeofday (&t1, &tz);
+   gettimeofday (&t1, NULL);
    InChk[1] = SqAtakd (board.king[side], 1^side);
 
    /*  Are there any legal moves? */
@@ -357,7 +354,7 @@
 
 void GetElapsed (void)
 {
-   gettimeofday (&t2, &tz);
+   gettimeofday (&t2, NULL);
    et = (t2.tv_sec - t1.tv_sec) + (double) (t2.tv_usec - t1.tv_usec)/1000000.0;
 }
 
diff -ru ../chess-5.03beta/src/main.c ./main.c
--- ../chess-5.03beta/src/main.c        Wed Oct 17 19:25:29 2001
+++ ./main.c    Fri Jan  4 15:43:44 2002
@@ -288,10 +288,9 @@
 
 int main (int argc, char *argv[])
 {
-  BitBoard d;
   int compilebook = 0;
   time_t now; 
-  register i;
+  register int i;
 
 #ifdef UNIVERSAL
   char buf[10];
diff -ru ../chess-5.03beta/src/output.c ./output.c
--- ../chess-5.03beta/src/output.c      Mon Oct 22 21:01:16 2001
+++ ./output.c  Fri Jan  4 15:02:00 2002
@@ -30,7 +30,6 @@
 #include <stdio.h>
 #include "common.h"
 
-void ShowTime (void);
 void ShowTime ()
 /**************************************************************************
  *
@@ -264,7 +263,6 @@
    printf ("\n");
 }
    
-void ShowGame (void);
 void ShowGame (void)
 {
   short i;
diff -ru ../chess-5.03beta/src/pgn.c ./pgn.c
--- ../chess-5.03beta/src/pgn.c Wed Oct 17 19:25:29 2001
+++ ./pgn.c     Fri Jan  4 15:44:07 2002
@@ -173,10 +173,9 @@
    FILE *fp;
    char s[100], c, wmv[8], bmv[8];
    char header[2000];
-   int moveno, gnucolor, result, n, i, ngames = 0;
+   int moveno, result, n, i, ngames = 0;
    leaf *p;
    struct timeval t1, t2;
-   struct timezone tz;
    unsigned long et;
    short playercolor, examinecolor, playerfound;
 /* Only players in the table below are permitted into the opening book 
@@ -295,7 +294,7 @@
   };
 
    et = 0.0;
-   gettimeofday (&t1, &tz);
+   gettimeofday (&t1, NULL);
    result = -1;
    fp = fopen (file, "r");
    if (fp == NULL)
@@ -412,7 +411,7 @@
    fclose (fp);
    BookBuilder(-2,-2, -2, -2);
 
-   gettimeofday (&t2, &tz);
+   gettimeofday (&t2, NULL);
    et += (t2.tv_sec - t1.tv_sec);
    putchar('\n');
 
@@ -421,7 +420,7 @@
 
    printf ("Time = %ld\n", et);
    printf("Games compiled: %d\n",ngames);
-   printf("Games per second: %d\n",ngames/et);
+   printf("Games per second: %ld\n",ngames/et);
    printf("Positions scanned: %d\n",newpos+existpos);
    printf("New & unique added: %d positions\n",newpos);
    printf("Duplicates not added: %d positions\n",existpos);
diff -ru ../chess-5.03beta/src/players.c ./players.c
--- ../chess-5.03beta/src/players.c     Wed Oct 17 19:25:29 2001
+++ ./players.c Fri Jan  4 15:45:44 2002
@@ -25,6 +25,8 @@
      address@hidden, address@hidden, address@hidden
 */
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include "common.h"
 #include "book.h"
 
@@ -44,12 +46,10 @@
 playerentry playerdb[MAXPLAYERS];
 static char lname[100];
 
-int scorecompare(playerentry *, playerentry *);
-int rscorecompare(playerentry *, playerentry *);
-int namecompare(playerentry *, playerentry *);
-
-int rscorecompare(playerentry *a, playerentry *b)
+static int rscorecompare(const void *aa, const void *bb)
 {
+    const playerentry *a = (const playerentry *)aa;
+    const playerentry *b = (const playerentry *)bb;
     float ascore, bscore;
     /*int ascore, bscore; */
     ascore = (a->wins+(a->draws/2))/(a->wins+a->draws+a->losses);
@@ -63,8 +63,10 @@
     else return(0);
 }
 
-int scorecompare(playerentry *a, playerentry *b)
+static int scorecompare(const void *aa, const void *bb)
 {
+    const playerentry *a = (const playerentry *)aa;
+    const playerentry *b = (const playerentry *)bb;
     int ascore, bscore;
     ascore = 100*(a->wins+(a->draws/2))/(a->wins+a->draws+a->losses);
     bscore = 100*(b->wins+(b->draws/2))/(b->wins+b->draws+b->losses);
@@ -73,8 +75,10 @@
     else return(0);
 }
 
-int namecompare(playerentry *a, playerentry *b)
+static int namecompare(const void *aa, const void *bb)
 {
+    const playerentry *a = (const playerentry *)aa;
+    const playerentry *b = (const playerentry *)bb;
     if (strcmp(a->player,b->player) > 0) return(1);
     else if (strcmp(a->player,b->player) < 0) return(-1);
     else return(0);
@@ -93,7 +97,7 @@
 
 void DBListPlayer (char *style)
 {
-  register i;
+  register int i;
        
   DBReadPlayer ();
   DBSortPlayer (style);
@@ -111,7 +115,7 @@
 
 void DBWritePlayer (void)
 {
-   register i;
+   register int i;
    float result1;
    int result2;
    FILE *wfp;
@@ -160,7 +164,7 @@
 int DBSearchPlayer (char *player)
 {
   int index = -1;
-  register i;
+  register int i;
 
   for (i = 0; i < totalplayers; i++)
     if (strncmp(playerdb[i].player,player,strlen(playerdb[i].player)) == 0)
@@ -174,7 +178,7 @@
 void DBUpdatePlayer (char *player, char *resultstr)
 {
   char *p, *x;
-  int index,result;
+  int index,result=R_NORESULT;
   memset(lname,0,sizeof(lname));
   p = player;
   x = lname;
@@ -183,7 +187,7 @@
     if (*p != ' ') 
       *x++ = *p++;
     else
-       *p++;
+      p++;
   } while (*p != '\0');
   *x = '\000';
   memset(playerdb,0,sizeof(playerdb[MAXPLAYERS]));
diff -ru ../chess-5.03beta/src/quiesce.c ./quiesce.c
--- ../chess-5.03beta/src/quiesce.c     Mon Oct 22 21:11:00 2001
+++ ./quiesce.c Fri Jan  4 15:46:39 2002
@@ -38,7 +38,7 @@
  *
  **************************************************************************/
 {
-   short side, xside, i;
+   short side, xside;
    int best, delta, score, savealpha;
    leaf *p, *pbest;
 
diff -ru ../chess-5.03beta/src/search.c ./search.c
--- ../chess-5.03beta/src/search.c      Mon Dec 17 07:34:55 2001
+++ ./search.c  Fri Jan  4 15:47:21 2002
@@ -201,7 +201,7 @@
  **************************************************************************/
 {
    int best, score, nullscore, savealpha;
-   short side, xside, i, rc, t0, t1, firstmove;
+   short side, xside, rc, t0, t1, firstmove;
    short fcut, fdel, donull, savenode, nullthreatdone, extend;
    leaf *p, *pbest;
    int g0, g1;
@@ -642,6 +642,8 @@
 {
    short i, len;
    int pvar[MAXPLYDEPTH];
+
+   (void)move; /* unused */
 
    /* SMC */
    if (!(flags & POST))
diff -ru ../chess-5.03beta/src/test.c ./test.c
--- ../chess-5.03beta/src/test.c        Mon Oct  8 08:31:27 2001
+++ ./test.c    Fri Jan  4 15:47:46 2002
@@ -44,7 +44,6 @@
 {
    unsigned long i;
    struct timeval t1, t2;
-   struct timezone tz;
    unsigned long et;
    short side, xside;
 
@@ -54,7 +53,7 @@
    while (ReadEPDFile ("TEST/BK.epd", 0))
    {
 */
-      gettimeofday (&t1, &tz);
+      gettimeofday (&t1, NULL);
       side = board.side;
       xside = 1^side;
       for (i = 0; i < 2000000; i++)
@@ -62,7 +61,7 @@
         TreePtr[2] = TreePtr[1];
          GenMoves (1);
       }
-      gettimeofday (&t2, &tz);
+      gettimeofday (&t2, NULL);
       et += (t2.tv_sec - t1.tv_sec);
       printf ("Time = %ld\n", et);
 /*
@@ -88,20 +87,19 @@
 {
    unsigned long i;
    struct timeval t1, t2;
-   struct timezone tz;
    unsigned long et;
 
    GenCnt = 0;
    et = 0;
    while (ReadEPDFile ("TEST/BK.epd", 0))
    {
-      gettimeofday (&t1, &tz);
+      gettimeofday (&t1, NULL);
       for (i = 0; i < 100000; i++)
       {
         TreePtr[2] = TreePtr[1];
          GenNonCaptures (1);
       }
-      gettimeofday (&t2, &tz);
+      gettimeofday (&t2, NULL);
       et += (t2.tv_sec - t1.tv_sec);
       printf ("Time = %ld\n", et);
    }
@@ -124,20 +122,19 @@
 {
    unsigned long i;
    struct timeval t1, t2;
-   struct timezone tz;
    unsigned long et;
 
    GenCnt = 0;
    et = 0;
    while (ReadEPDFile ("TEST/BK.epd", 0))
    {
-      gettimeofday (&t1, &tz);
+      gettimeofday (&t1, NULL);
       for (i = 0; i < 200000; i++)
       {
         TreePtr[2] = TreePtr[1];
          GenCaptures (1);
       }
-      gettimeofday (&t2, &tz);
+      gettimeofday (&t2, NULL);
       et += (t2.tv_sec - t1.tv_sec);
       printf ("Time = %ld\n", et);
    }
@@ -223,18 +220,17 @@
 {
    unsigned long i;
    struct timeval t1, t2;
-   struct timezone tz; 
 
    et = 0;
    EvalCnt = 0;
    while (ReadEPDFile ("TEST/BK.epd", 0))
    {
-      gettimeofday (&t1, &tz);
+      gettimeofday (&t1, NULL);
       for (i = 0; i < NEVALS; i++)
       {
          (void) Evaluate (-INFINITY, INFINITY);
       }
-      gettimeofday (&t2, &tz);
+      gettimeofday (&t2, NULL);
       et += (t2.tv_sec - t1.tv_sec) + 
                (double) (t2.tv_usec - t1.tv_usec)/1000000.0;
       printf ("Time = %.1f\n", et);
diff -ru ../chess-5.03beta/src/ttable.c ./ttable.c
--- ../chess-5.03beta/src/ttable.c      Mon Oct 22 21:11:00 2001
+++ ./ttable.c  Fri Jan  4 15:48:19 2002
@@ -89,6 +89,9 @@
    HashSlot *t;
    KeyType Key;
 
+   (void)alpha;        /* unused */
+   (void)beta; /* unused */
+
    TotalGetHashCnt++;
    t = HashTab[side] + ((HashKey & TTHashMask) & -2);  
    Key = KEY(HashKey);
diff -ru ../chess-5.03beta/src/util.c ./util.c
--- ../chess-5.03beta/src/util.c        Mon Dec 17 07:25:04 2001
+++ ./util.c    Fri Jan  4 15:16:35 2002
@@ -172,6 +172,7 @@
  *
  ***************************************************************************/
 {
+   (void)sig;
    SET (flags, TIMEOUT);
    signal (SIGINT, EndSearch);
 }

-- 
   - David A. Holland             |    VINO project home page:
     address@hidden    | http://www.eecs.harvard.edu/vino



reply via email to

[Prev in Thread] Current Thread [Next in Thread]