bug-readline
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-readline] Memory leaks (identified with valgrind)


From: Timothe Litt
Subject: [Bug-readline] Memory leaks (identified with valgrind)
Date: Sun, 6 Mar 2016 07:56:55 -0500
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0

readline 6.3 under Fedora Linux.
(same failures with 6.2)

Any trivial program using readline leaks memory. 
There is also a reference to uninitialized memory.

A sample distilled from a real application is included.

With no valgrindrc or leaker.history:

 cc -o rl_leaker -g rl_leaker.c -lreadline -ltermcap &&  \
    valgrind --gen-suppressions=all --show-leak-kinds=all \
    --leak-check=full ./rl_leaker 2>leaker63.txt

Without (even) doing any line editing:

To the prompt, type "hello readline"
To the next prompt, type "q"

==1841== HEAP SUMMARY:
==1841==     in use at exit: 76,701 bytes in 176 blocks
==1841==   total heap usage: 276 allocs, 100 frees, 87,744 bytes allocated

grep match-leak-kinds leaker63.txt

reveals that all the leaks are reachable.

I suggest that readline establish an "atexit" function during initialization
that will cleanup... or  (less desirable) provide a suitable function
for the
application to call "atexit".

Obviously, further testing with more user interaction is in order.

Program:
/* Demonstrate memory leaks in readline */

/* cc -o rl_leaker -g rl_leaker.c -lreadline -ltermcap && \
 * valgrind --gen-suppressions=all --show-leak-kinds=all \
 *    --leak-check=full ./rl_leaker 2>leaker.txt
 */

#include <stdlib.h>
#include <stdio.h>

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


int main( int argc, char **argv ) {
    const char *hfname = "leaker.history";
    char *cmd;

    (void) argc;
    (void) argv;

    printf( "Readline version %s\n", rl_library_version );

    rl_readline_name = "leaker";
    history_truncate_file( hfname, 200 );
    stifle_history( 200 );
    read_history( hfname );

    do {
        char c;

        cmd = readline( "more> " );
        if( cmd == NULL ) break;

        if( *cmd != '\0' && *cmd != 'q' )
            add_history( cmd );
        printf( "less: %s\n", cmd );
        c = *cmd;
        free( cmd );
        if( c == 'q' ) break;
    } while( 1 );

    write_history( hfname );

    exit(EXIT_SUCCESS);
}

The following suppression rules can mask the issue, but are unwise:
{
   <Readline senmdsg>
   Memcheck:Param
   sendmsg(msg.msg_name)
   fun:__sendmsg_nocancel
   fun:readline
   fun:main
}
{
  <readline>
  Memcheck:Leak
  ...
  obj:/lib64/libreadline*
}

-- 
Timothe Litt
ACM Distinguished Engineer
--------------------------
This communication may not represent the ACM or my employer's views,
if any, on the matters discussed. 


Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]