libreadline7 segfault

Asked by Hinko Kocevar

Compiling a test program with libreadline7 and libhistory7 using bionic packages results in segfault.

Code:

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

/* A static variable for holding the line. */
static char *line_read = (char *)NULL;

/* Read a string, and return a pointer to it.
   Returns NULL on EOF. */
char * rl_gets ()
{
  /* If the buffer has already been allocated,
     return the memory to the free pool. */
  if (line_read)
    {
      free (line_read);
      line_read = (char *)NULL;
    }

  /* Get a line from the user. */
  line_read = readline ("");

  /* If the line has any text in it,
     save it on the history. */
  if (line_read && *line_read)
    add_history (line_read);

  return (line_read);
}

int main(int argc, char **argv)
{
  char *line = NULL;
  printf("hej..\n");

  using_history();

  while (1)
  {
    line = rl_gets();
    if (line) printf("You said: '%s'\n", line);
    else printf("You said nothing..\n");

    if (strncmp("q", line, 1) == 0) break;
  }

  printf("hej doo..\n");
  return 0;
}

Compile:

hinxx@obzen ~/Projects/ice40/tmp $ gcc -O0 -ggdb3 readline.c -o readline -lhistory -lreadline

Run:

hinxx@obzen ~/Projects/ice40/tmp $ ./readline
hej..
test
You said: 'test'
Segmentation fault

The 'Segmentation fault' happens after pressing key UP on the keyboard.

gdb output:

hinxx@obzen ~/Projects/ice40/tmp $ gdb ./readline
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./readline...done.
(gdb) r ./readline
Starting program: /home/hinxx/Projects/ice40/tmp/readline ./readline
hej..
test
You said: 'test'

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff79b352b in previous_history () at ./history.c:196
warning: Source file is more recent than executable.
(gdb) bt
#0 0x00007ffff79b352b in previous_history () at ./history.c:196
#1 0x00007ffff79b30e5 in rl_get_previous_history (count=<optimized out>, key=<optimized out>) at ./misc.c:609
#2 0x00007ffff799625f in _rl_dispatch_subseq (key=65, map=<optimized out>, got_subseq=<optimized out>) at ./readline.c:859
#3 0x00007ffff79963c6 in _rl_dispatch_subseq (key=91, map=0x7ffff7bc7420 <emacs_meta_keymap>, got_subseq=<optimized out>) at ./readline.c:984
#4 0x00007ffff79963c6 in _rl_dispatch_subseq (key=27, map=0x7ffff7bc8440 <emacs_standard_keymap>, got_subseq=<optimized out>) at ./readline.c:984
#5 0x00007ffff79966a1 in _rl_dispatch (key=<optimized out>, map=<optimized out>) at ./readline.c:802
#6 0x00007ffff7996786 in readline_internal_char () at ./readline.c:629
#7 0x00007ffff7996f25 in readline_internal_charloop () at ./readline.c:656
#8 readline_internal () at ./readline.c:670
#9 readline (prompt=<optimized out>) at ./readline.c:376
#10 0x00005555555548d0 in rl_gets () at readline.c:24
#11 0x000055555555493b in main (argc=2, argv=0x7fffffffde08) at readline.c:43

This does not happen if '-lhistory' in not used at compile time.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu readline Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Hinko Kocevar (hinkocevar) said :
#1

FYI, I've took the vanilla package readline-7.0.tar.gz and compiled that one, forced the test app to use the resulting libreadline and libhistory (with LD_LIBRARY_PATH) and the problem is gone; I can use key UP in the test code as posted.

Revision history for this message
Hinko Kocevar (hinkocevar) said :
#2

Another way to get my example working is not to supply '-lhistory' at compile time..

Revision history for this message
Manfred Hampl (m-hampl) said :
#3

For diagnostic purposes, what is the output of the commands

uname -a
lsb_release -crid
dpkg -l | grep readline
apt-cache policy libreadline7
ldd ./readline

Revision history for this message
Hinko Kocevar (hinkocevar) said :
#4

Here they are:

$ uname -a
Linux obzen 5.4.0-42-generic #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -crid
Distributor ID: LinuxMint
Description: Linux Mint 19.3 Tricia
Release: 19.3
Codename: tricia

$ dpkg -l | grep readline
ii libreadline-dev:amd64 7.0-3 amd64 GNU readline and history libraries, development files
ii libreadline5:amd64 5.2+dfsg-3build1 amd64 GNU readline and history libraries, run-time libraries
ii libreadline6:amd64 6.3-8ubuntu2 amd64 GNU readline and history libraries, run-time libraries
ii libreadline7:amd64 7.0-3 amd64 GNU readline and history libraries, run-time libraries
ii libreadline7-dbg 7.0-3 amd64 GNU readline and history libraries, debugging libraries
ii libterm-readline-perl-perl 1.0303-1 all Perl implementation of Readline libraries
ii readline-common 7.0-3 all GNU readline and history libraries, common files
$ apt-cache policy libreadline7
libreadline7:
  Installed: 7.0-3
  Candidate: 7.0-3
  Version table:
 *** 7.0-3 500
        500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages
        100 /var/lib/dpkg/status

$ ldd ./readline
 linux-vdso.so.1 (0x00007ffda3564000)
 libhistory.so.7 => /lib/x86_64-linux-gnu/libhistory.so.7 (0x00007f7adb03c000)
 libreadline.so.7 => /lib/x86_64-linux-gnu/libreadline.so.7 (0x00007f7adadf3000)
 libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7adaa02000)
 libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f7ada7d8000)
 /lib64/ld-linux-x86-64.so.2 (0x00007f7adb448000)

Revision history for this message
Manfred Hampl (m-hampl) said :
#5

Manfred Hampl suggests this article as an answer to your question:
FAQ #3041: “No support for Linux Mint”.

Revision history for this message
Hinko Kocevar (hinkocevar) said :
#6

Ok, wasn’t sure where to ask since this is an Ubuntu package on Mint. Thanks!

Can you help with this problem?

Provide an answer of your own, or ask Hinko Kocevar for more information if necessary.

To post a message you must log in.