Problems with Crafty's "pers load" command

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

maschmdt
Posts: 13
Joined: Sat Feb 23, 2008 8:07 pm

Problems with Crafty's "pers load" command

Post by maschmdt »

Hello!

I just wanted to experiment with crafty's personality feature and ran in subsequent crashes under Windows :(

It was reproducible with the following command sequence:

pers save t
pers load t

In option.c found this glitch:

Code: Select all

      Print(128, "Loading personality file %s\n", filename);
      if ((file = fopen(filename, "rw"))) {
maybe the mode "rw" is undefined for windows libs (should be "r+" to aquire read and write rights).

Anyway, I changed it to:

Code: Select all

      Print(128, "Loading personality file %s\n", filename);
      if ((file = fopen(filename, "r"))) {
This compiles and works fine under Windows (XP and Vista)

Michael.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Problems with Crafty's "pers load" command

Post by Sven »

maschmdt wrote:In option.c found this glitch:

Code: Select all

      Print(128, "Loading personality file %s\n", filename);
      if ((file = fopen(filename, "rw"))) {
maybe the mode "rw" is undefined for windows libs (should be "r+" to aquire read and write rights).
I would consider a call to fopen(..., "rw") to be invalid on any platform. POSIX at least does not allow "rw". But in theory, fopen() should not crash by itself, not even on Windows. In case of an invalid mode argument fopen() shall set errno to EINVAL and return a null pointer. Obviously this is not the case on Windows.

Btw I guess that fopen(..., "r") is fully sufficient here since there is no write access required IMO.

To be safe (on platforms where wrong mode does not crash), I would add error checking code like this, similar to the "save" case some 60 lines above but even slightly extended:

Code: Select all

// and don't forget to add these at the top for strerror() and errno:
// #include <string.h>
// #include <errno.h>

      if (&#40;file = fopen&#40;filename, "r"))) &#123;
        // ...
      &#125; else &#123;
        printf&#40;"ERROR.  Unable to open personality file %s for reading&#58; %s\n",
            args&#91;2&#93;, strerror&#40;errno&#41;);
        return &#40;1&#41;;
      &#125;
and probably I would also add the strerror(errno) stuff at some other places where system library functions may fail and the system's internal error message may help to analyze the problem. Of course it's a matter of style.

Sven
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Problems with Crafty's "pers load" command

Post by bob »

Sven Schüle wrote:
maschmdt wrote:In option.c found this glitch:

Code: Select all

      Print&#40;128, "Loading personality file %s\n", filename&#41;;
      if (&#40;file = fopen&#40;filename, "rw"))) &#123;
maybe the mode "rw" is undefined for windows libs (should be "r+" to aquire read and write rights).
I would consider a call to fopen(..., "rw") to be invalid on any platform. POSIX at least does not allow "rw". But in theory, fopen() should not crash by itself, not even on Windows. In case of an invalid mode argument fopen() shall set errno to EINVAL and return a null pointer. Obviously this is not the case on Windows.

Btw I guess that fopen(..., "r") is fully sufficient here since there is no write access required IMO.

To be safe (on platforms where wrong mode does not crash), I would add error checking code like this, similar to the "save" case some 60 lines above but even slightly extended:

Code: Select all

// and don't forget to add these at the top for strerror&#40;) and errno&#58;
// #include <string.h>
// #include <errno.h>

      if (&#40;file = fopen&#40;filename, "r"))) &#123;
        // ...
      &#125; else &#123;
        printf&#40;"ERROR.  Unable to open personality file %s for reading&#58; %s\n",
            args&#91;2&#93;, strerror&#40;errno&#41;);
        return &#40;1&#41;;
      &#125;
and probably I would also add the strerror(errno) stuff at some other places where system library functions may fail and the system's internal error message may help to analyze the problem. Of course it's a matter of style.

Sven
No idea how that crept in, but "r+" is correct rather than "rw". Although "r" will probably work just fine there since it is reading anyway.