Bug 31533

Summary: /bin/sh memory leak
Product: Base System Reporter: Bob Bishop <rb>
Component: binAssignee: tegge
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.3-RELEASE   
Hardware: Any   
OS: Any   

Description Bob Bishop 2001-10-27 14:00:02 UTC
/bin/sh leaks memory under certain conditions

How-To-Repeat: Run the following (minimal) script, watch the shell with top(1):

#!/bin/sh
while true
do
        cd /tmp
        while true
        do
                break
        done
done
Comment 1 Bob Bishop 2001-10-27 16:39:58 UTC
Also affects -CURRENT

--
Bob Bishop		    +44 (0)118 977 4017
rb@gid.co.uk		fax +44 (0)118 989 4254
Comment 2 Maxim Konovalov 2001-10-29 13:32:32 UTC
I resend the message because there is no my followup in gnats db.

---------- Forwarded message ----------
Date: Sat, 27 Oct 2001 20:11:25 +0400 (MSD)
From: Maxim Konovalov <maxim@macomnet.ru>
To: Bob Bishop <rb@gid.co.uk>
Cc: freebsd-bugs@FreeBSD.ORG
Subject: Re: bin/31533: /bin/sh memory leak

On Sat, 27 Oct 2001, Bob Bishop wrote:

> The following reply was made to PR bin/31533; it has been noted by GNATS.
>
> From: Bob Bishop <rb@gid.co.uk>
> To: freebsd-gnats-submit@FreeBSD.org, rb@gid.co.uk
> Cc:
> Subject: Re: bin/31533: /bin/sh memory leak
> Date: Sat, 27 Oct 2001 16:39:58 +0100
>
>  Also affects -CURRENT

How about this patch:

Index: cd.c
===================================================================
RCS file: /home/ncvs/src/bin/sh/cd.c,v
retrieving revision 1.20
diff -u -r1.20 cd.c
--- cd.c	29 Nov 1999 19:10:58 -0000	1.20
+++ cd.c	27 Oct 2001 16:07:20 -0000
@@ -238,8 +238,8 @@
 		curdir = NULL;
 		if (getpwd() == NULL)
 			error("getcwd() failed: %s", strerror(errno));
-		setvar("PWD", curdir, VEXPORT | VTEXTFIXED);
-		setvar("OLDPWD", prevdir, VEXPORT | VTEXTFIXED);
+		setvar("PWD", curdir, VEXPORT);
+		setvar("OLDPWD", prevdir, VEXPORT);
 		INTON;
 		return;
 	}
@@ -270,8 +270,8 @@
 		ckfree(prevdir);
 	prevdir = curdir;
 	curdir = savestr(stackblock());
-	setvar("PWD", curdir, VEXPORT | VTEXTFIXED);
-	setvar("OLDPWD", prevdir, VEXPORT | VTEXTFIXED);
+	setvar("PWD", curdir, VEXPORT);
+	setvar("OLDPWD", prevdir, VEXPORT);
 	INTON;
 }

-- 
Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
phone: +7 (095) 796-9079, mailto: maxim@macomnet.ru
Comment 3 Bob Bishop 2001-10-29 22:07:02 UTC
[ My followup got lost as well. ]

Maxim's patch appears to fix the problem. Someone please commit.


--
Bob Bishop              (0118) 977 4017  international code +44 118
rb@gid.co.uk        fax (0118) 989 4254
Comment 4 Maxim Konovalov 2001-11-07 08:06:20 UTC
Just as follow up.

>From maxim@macomnet.ru Wed Nov  7 11:03:46 2001
Date: Mon, 5 Nov 2001 23:12:21 +0300 (MSK)
From: Maxim Konovalov <maxim@macomnet.ru>
To: Alfred Perlstein <bright@mu.org>
Cc: Bob Bishop <rb@gid.co.uk>, hackers@FreeBSD.ORG
Subject: Re: Committer please


Hello,

On Mon, 5 Nov 2001, Alfred Perlstein wrote:

> * Bob Bishop <rb@gid.co.uk> [011105 06:35] wrote:
> > Hi,
> >
> > Would some kind committer please have a look at bin/31533: /bin/sh memory
> > leak. There is a patch there which I believe is OK. Having a memory leak
> > loose in the shell seems like a Bad Idea to me. TIA
>
> Understandable, things like this could be applied more quickly if
> someone would take the time to explain what the delta does other
> than fix the problem.  Meaning, what is VTEXTFIXED, and what does
> removing it change?

OK, mea culpa.

/usr/src/bin/sh/var.h:48

#define VTEXTFIXED      0x08    /* text is staticly allocated */

Afaiu it is used for internal variables names which are statically
allocated in /usr/src/bin/sh/var.c:102. VTEXTFIXED prevents them from
freing:

/usr/src/bin/sh/var.c:319

                        if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
                                ckfree(vp->text);

PWD and OLDPWD variables are not statical ones and should be
reallocated every time when changed.

- -maxim

-- 
Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
phone: +7 (095) 796-9079, mailto: maxim@macomnet.ru
Comment 5 Tor.Egge 2001-11-07 23:03:26 UTC
>  PWD and OLDPWD variables are not statical ones and should be
>  reallocated every time when changed.

I introduced this leak in revision 1.18 of cd.c while fixing a
different problem (cf. PR 2541).  I've commited your suggested fix to
-current and will MFC it to RELENG_4 in a few days.

- Tor Egge
Comment 6 Maxim Konovalov 2001-11-08 07:42:54 UTC
On Wed, 7 Nov 2001 Tor.Egge@cvsup.no.freebsd.org wrote:

>
> >  PWD and OLDPWD variables are not statical ones and should be
> >  reallocated every time when changed.
>
> I introduced this leak in revision 1.18 of cd.c while fixing a
> different problem (cf. PR 2541).  I've commited your suggested fix to
> -current and will MFC it to RELENG_4 in a few days.
>
> - Tor Egge

Thank you!

-- 
Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
phone: +7 (095) 796-9079, mailto: maxim@macomnet.ru
Comment 7 bill fumerola freebsd_committer freebsd_triage 2001-11-08 10:33:56 UTC
Responsible Changed
From-To: freebsd-bugs->tegge

tegge introduced and fixed this bug. over to him to nag until MFC.
Comment 8 Jeroen Ruigrok van der Werven freebsd_committer freebsd_triage 2001-11-15 18:51:43 UTC
State Changed
From-To: open->closed

Originator says this got MFC'd. 

Thanks.
Comment 9 Maxim Konovalov 2001-11-18 12:26:08 UTC
Hello,

This PR can be closed. The fix was committed to -current and -stable.

- -maxim

-- 
Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
phone: +7 (095) 796-9079, mailto: maxim@macomnet.ru
Comment 10 Bob Bishop 2001-11-18 13:06:50 UTC
Hi,

Maxim Konovalov <maxim@macomnet.ru> wrote:
> This PR can be closed. The fix was committed to -current and -stable.

I agree with that.


--
Bob Bishop              (0118) 977 4017  international code +44 118
rb@gid.co.uk        fax (0118) 989 4254