[PATCH 3 of 6 V2] rust: exposing in parsers module

Yuya Nishihara yuya at tcha.org
Fri Oct 12 01:05:51 EDT 2018


On Tue, 09 Oct 2018 17:22:47 +0200, Georges Racinet wrote:
> # HG changeset patch
> # User Georges Racinet <gracinet at anybox.fr>
> # Date 1538060175 -7200
> #      Thu Sep 27 16:56:15 2018 +0200
> # Node ID cf5c799e65a1225538fa1246887e2efd94c09acc
> # Parent  81b8781de6fad514634713fa2cb9f10c320d1af3
> # EXP-Topic rustancestors-rfc
> rust: exposing in parsers module

Note that we use tabs in C source.

> +static int rustla_init(rustlazyancestorsObject *self,
> +                       PyObject *args) {
> +  PyObject *initrevsarg = NULL;
> +  PyObject *inclusivearg = NULL;
> +  long stoprev = 0;
> +  long *initrevs = NULL;
> +  int inclusive = 0;
> +  Py_ssize_t i;
> +
> +  indexObject *index;
> +  if (!PyArg_ParseTuple(args, "O!O!lO!",
> +                        &indexType, &index,
> +                        &PyList_Type, &initrevsarg,
> +                        &stoprev,
> +                        &PyBool_Type, &inclusivearg))
> +    return -1;
> +
> +  Py_INCREF(index);
> +  self->index = index;
> +
> +  if (inclusivearg == Py_True)
> +    inclusive = 1;
> +
> +  Py_ssize_t linit = PyList_GET_SIZE(initrevsarg);
> +
> +  initrevs = (long*)malloc(linit * sizeof(long));

linit * sizeof(long) can overflow. Using calloc() would be probably safer,
and should be okay here as initrevs are small.

> +
> +  if (initrevs == NULL) {
> +    PyErr_NoMemory();
> +    goto bail;
> +  }
> +
> +  for (i=0; i<linit; i++) {
> +    initrevs[i] = PyInt_AsLong(PyList_GET_ITEM(initrevsarg, i));

PyInt_AsLong() may fail if a list item isn't an integer.

> +  }
> +  self->iter = rustlazyancestors_init(index,
> +                                      index_get_parents,
> +                                      linit, initrevs,
> +                                      stoprev, inclusive);
> +  if (self->iter == NULL)
> +    /* if this is because of GraphError::ParentOutOfRange
> +       index_get_parents() has already set the proper ValueError */
> +    goto bail;
> +
> +  free(initrevs);
> +  return 0;
> +
> +bail:
> +  Py_XDECREF(index);

Perhaps self->index shouldn't be decrefed here since it's still alive and
decrefed again at rustla_dealloc().

> +  free(initrevs);
> +  return -1;
> +};
> +
> +static void rustla_dealloc(rustlazyancestorsObject *self)
> +{
> +  Py_XDECREF(self->index);
> +  if (self->iter != NULL) { /* can happen if rustla_init failed */
> +    rustlazyancestors_drop(self->iter);
> +  }
> +  PyObject_Del(self);
> +}


More information about the Mercurial-devel mailing list