I would like to propose a fix for cubeinfo() in the Python binding.

Currently, in PythonCubeInfo(), it is implemented as:

PyArg_ParseTuple(args, "|iiii(ii)iiii:cubeinfo",
                 &nCube, &fCubeOwner, &fMove, &nMatchTo,
                 &anScore[0], &anScore[1], &fCrawford, &bgv)

The format string is designed to receive four integers after the
score, but in reality, pointers are only passed for two items,
fCrawford and bgv.  As a result, if additional arguments are provided,
PyArg_ParseTuple() uses non-existent variable arguments as output
destinations, leading to undefined behavior. I have also been able to
crash gnubg through this code path on my end.

Meanwhile, PythonCubeInfo() already has

  int fJacobyRule = ms.fJacoby;
  int fBeavers = (ms.cBeavers > 0);
  bgvariation bgv = ms.bgv;

and passes them in the subsequent SetCubeInfo() in the order of:

  SetCubeInfo(..., fCrawford, fJacobyRule, fBeavers, bgv)

For this reason, I believe that the last four arguments of the format string 
were originally intended to be crawford, jacoby, beavers, bgv.


Past Guile bindings are also helpful. The general `cube-info` added in
2000 had the argument order:

  cube_info(..., sCrawford, sJacoby, sBeavers)

and passed them to SetCubeInfo() in the order of Crawford, Jacoby, and
Beavers. Also, cube-info-money followed the order of Jacoby and
Beavers.

Checking the initial PythonCubeInfo() from 2003 when the Python
bindings were added, the variables fCrawford, fJacoby, fBeavers, and
bgv already existed and were passed to SetCubeInfo() in this
order. However, the destination for PyArg_ParseTuple() lacked &fJacoby
and &fBeavers from the beginning.

Therefore, for example, modifying it as follows seems consistent with
the original intent and past Guile bindings.

if (!PyArg_ParseTuple(args, "|iiii(ii)iiii:cubeinfo",
                      &nCube, &fCubeOwner, &fMove, &nMatchTo,
                      &anScore[0], &anScore[1],
                      &fCrawford, &fJacobyRule, &fBeavers, &bgv))
    return NULL;

The argument order on the Python side is:

  cubeinfo(cube, cubeowner, move, matchto, score,
           crawford, jacoby, beavers, bgv)

Please note that in the current documentation, bgv is listed
immediately after Crawford, so this change will alter the meaning of
the 7th positional argument. However, considering the implementation's
format string, the argument order of SetCubeInfo(), and previous Guile
bindings, I believe aligning them in the order of crawford, jacoby,
beavers, and bgv is the most natural approach.

If there are no issues, I would like to create a patch in this order.

Best regards,
-- 
TAKAHASHI Kaoru

Reply via email to