kpdyer / fteproxy

programmable proxy for censorship circumvention
https://fteproxy.org/
Apache License 2.0
149 stars 21 forks source link

tp_init function in fte.cDFA python module failed on PyArg_ParseTuple #94

Closed kpdyer closed 10 years ago

kpdyer commented 10 years ago

Our function `fte.cDFA.DFA.__init__ takes a string and int as arguments. Both arguments are required. However, using the standard python pattern:

static int
DFA_init(DFAObject *self, PyObject *args, PyObject *kwds)
{
    const char* regex;
    uint32_t len;
    uint32_t max_len;
    if (!PyArg_ParseTuple(args, "s#i", &regex, &len, &max_len))
        return -1;

results in len being 0 and regex does not contain the actual string passed as input.

Environment:

kpdyer commented 10 years ago

I couldn't get this to occur on any other platform. Also, unable to track down the root of this issue.

I solved the issue by manually getting and validating each input parameter. For example

    PyObject *arg0 = PyTuple_GetItem(args, 0);
    if (!PyString_Check(arg0)) {
        PyErr_SetString(PyExc_RuntimeError, "First argument must be a string");
        return 0;
    }
    const char* regex = PyString_AsString(arg0);