Readline library crash on custom completion

Bug #128727 reported by Bogdan Harjoc
4
Affects Status Importance Assigned to Milestone
readline5 (Ubuntu)
Triaged
Undecided
Unassigned

Bug Description

Binary package hint: libreadline5

Description
=======

Using up-to-date feisty.

I am using libreadline to provide custom completion for an application.

Using custom completion involves setting a global C symbol
(rl_attempted_completion_function) to a function that returns with a list
of suitable matches:

char ** match_generator(char * prefix, ...);

The char ** result ends with a (char *)NULL. If no matches are found, this
returns the NULL on the first position which confuses readline (see patch).

Steps to reproduce:
============

/* compile: gcc -Wall -O -o rltest rltest.c -lreadline */

#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>

char ** match_func(const char * text, int start, int end)
{
        char ** ret = (char **)malloc(sizeof(char *));
        ret[0] = 0;
        return ret;
}

int main()
{
        rl_attempted_completion_over = 1;
        rl_attempted_completion_function = match_func;

        readline("hit TAB> ");
        return 0;
}

Fixes
====

Existing applications can simply do "if !matches[0] then matches=0".

To get expected (IMHO) behaviour from the library:

--- readline-5.2/complete.c 2006-07-28 17:35:49.000000000 +0200
+++ readline-5.2-fixed/complete.c 2007-07-27 16:57:02.000000000 +0200
@@ -961,6 +961,7 @@
   if (rl_attempted_completion_function)
     {
       matches = (*rl_attempted_completion_function) (text, start, end);
+ if (matches && ! matches[0]) matches = 0;

       if (matches || rl_attempted_completion_over)
        {

====

Cheers!

Tags: patch
Revision history for this message
Bogdan Harjoc (harjoc) wrote :

Actually 'matches' should also be free()'d.
Attached patch for this.

Revision history for this message
Daniel T Chen (crimsun) wrote :

Confirmed on current 8.10.

Changed in readline5:
status: New → Triaged
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.