jsommers / pytricia

A library for fast IP address lookup in Python.
GNU Lesser General Public License v3.0
214 stars 22 forks source link

IPv6 addresses :: through ::f cause spurious errors #34

Open andrewchi opened 4 years ago

andrewchi commented 4 years ago

Confirming what @lithammer reported in https://github.com/jsommers/pytricia/issues/8#issuecomment-492766164_ , the following exceptions are raised for the IPv6 addresses "::", "::1", "::2", ..., "::f", which should all be valid. For some reason, the errors do not occur with "::10".

>>> import pytricia
>>> t = pytricia.PyTricia(128)
>>> t['::'] = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: error return without exception set
>>> t['::/64'] = 0
>>> t['::']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid prefix.

It is not clear to me at what point that this bug is triggered, perhaps somewhere in _key_object_to_prefix()?

mutax commented 2 years ago

I just ran into the same issue. The SystemError might be result of returning a NULL from C to python without setting the error.

Scrolling through the Code I see at least one possible instance (picked randomly here) where this might be the case, compare how in the first return NULL is returned, in the second one the error message is set.

static PyObject *
pytricia_get(register PyTricia *obj, PyObject *args) {
    PyObject *key = NULL;
    PyObject *defvalue = NULL;

    if (!PyArg_ParseTuple(args, "O|O:get", &key, &defvalue)) {
        return NULL;
    }
    prefix_t *prefix = _key_object_to_prefix(key);
    if (!prefix) {
        PyErr_SetString(PyExc_ValueError, "Invalid prefix.");
        return NULL;
    }
    …

documenting it here in case other people stumble upon the same issue. Not sure if I will be able to submit a pull request due to time constraints.

Currently I add a '0' in the front and end of an address as a workaround like so:

    if the_ip.startswith('::'):
        the_ip = f'0{the_ip}'
    if the_ip.endswith('::'):
        the_ip = f'{the_ip}0'