bug-bash
[Top][All Lists]
Advanced

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

Memory leak in hc_erasedups


From: Seiichi Ishitsuka
Subject: Memory leak in hc_erasedups
Date: Wed, 13 Apr 2016 01:49:05 +0000

Hi all,

I found memory leak in case of using "HISTCONTOL=erasedups" in bash-4.2.

- bash version
 CentOS7: bash-4.2.45-5.el7_0.4.x86_64

 $ echo $BASH_VERSION
 4.2.45(1)-release

- How to reproduce memory leak

 1. add ~/.bashrc

    export HISTCONTROL=erasedups
    export HISTSIZE=10
    export PROMPT_COMMAND="history -a; history -r"

 2. login from other terminal(e.g. ssh or telnet)

 3. command execute repeatedly (e.g. echo "a"; echo "b")

  => When terminal macro is used, it's easy to reproduce.
     Following is a python script for localhost.
     Please change 'pass' to right password in your environment.

$ cat /tmp/bash-test.py
#!/usr/bin/python
import pexpect
p = pexpect.spawn ('ssh localhost')
p.expect("password:")
p.sendline('pass')
p.expect('\$ ')
while 1:
  p.sendline('echo a')
  p.expect('\$ ')
  p.sendline('echo b')
  p.expect('\$ ')
p.close()

- Result

 By executing 'bash-test.py', you can see a following anon page is increased.

$ pmap -p 26941 | head -n 5; sleep 60; pmap -p 26941 | head -n 5
26941:   -bash
0000000000400000    884K r-x-- /usr/bin/bash
00000000006dc000      4K r---- /usr/bin/bash
00000000006dd000     36K rw--- /usr/bin/bash
00000000006e6000   2468K rw---   [ anon ]

26941:   -bash
0000000000400000    884K r-x-- /usr/bin/bash
00000000006dc000      4K r---- /usr/bin/bash
00000000006dd000     36K rw--- /usr/bin/bash
00000000006e6000   2700K rw---   [ anon ]

- Proposal patch

 Attached is the testing proposal patch.

---
 bashhist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/bashhist.c b/bashhist.c
index 7240a5b..9bf3b5c 100644
--- a/bashhist.c
+++ b/bashhist.c
@@ -625,6 +625,7 @@ hc_erasedups (line)
      char *line;
 {
   HIST_ENTRY *temp;
+  HIST_ENTRY *discard;
   int r;
 
   using_history ();
@@ -633,7 +634,9 @@ hc_erasedups (line)
       if (STREQ (temp->line, line))
        {
          r = where_history ();
-         remove_history (r);
+         discard = remove_history (r);
+         if (discard)
+           free_history_entry (discard); 
        }
     }
   using_history ();
-- 
1.8.3.1

 I have tested at bash-4.2 branch.
 url = http://git.savannah.gnu.org/r/bash.git

Best regards,
Seiichi Ishitsuka




reply via email to

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