This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bfroehle
Recipients bfroehle
Date 2013-02-24.17:32:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1361727162.6.0.330340271384.issue17289@psf.upfronthosting.co.za>
In-reply-to
Content
The `readline.set_completer_delims` doesn't play well with others because
it assumes that only it ever allocates or modifies the
rl_completer_word_break_characters buffer.  If other programs modify this
value, for example changing it from heap allocated space to stack
allocated space, the results can be catastrophic.

To remind you, the function essentially works as:

    set_completer_delims(PyObject *self, PyObject *args)
    {
        // ...
        free((void*) rl_completer_word_break_characters;
        rl_completer_word_break_characters = strdup(break_chars);
        // ...
    }

where `break_chars` is the user provided string.

Take, for example, R as another programs which changes the readline
completer strings.  When an embedded R instance is initialized (say, using
`r2py`) something similar to the following takes place::

    static void
    set_rl_completer_word_break_characters(const char *new)
    {
        static char[201] buffer;
        strncpy(buffer, new, 200);
        rl_completer_word_break_characters = buffer;
    }

    static void
    initialize_embedded_R(...)
    {
        // ...
        set_rl_completer_word_break_characters(...);
    }

As you might expect the next trip through `readline.set_completer_delims`
after initializing R will be catastrophic when we attempt to free a stack
allocate buffer.

I think we should consider modifying the `readline.set_completer_delims`
to store the allocated buffers in the module state::

    set_completer_delims(PyObject *self, PyObject *args)
    {
        // ...
        free(_readlinestate_global->break_chars);
        rl_completer_word_break_characters = strdup(break_chars);
        _readlinestate_global->break_chars = rl_completer_word_break_characters;
        // ...
    }

This would prevent the segfault and memory leaks, and would render weird
hacks (like https://bitbucket.org/lgautier/rpy2/commits/408bae913653 in
the r2py code) unnecessary.
History
Date User Action Args
2013-02-24 17:32:42bfroehlesetrecipients: + bfroehle
2013-02-24 17:32:42bfroehlesetmessageid: <1361727162.6.0.330340271384.issue17289@psf.upfronthosting.co.za>
2013-02-24 17:32:42bfroehlelinkissue17289 messages
2013-02-24 17:32:42bfroehlecreate