Bug 40177

Summary: /bin/sh with builtin 'test' has memory leaks
Product: Base System Reporter: Eugene Grosbein <ports>
Component: binAssignee: Maxim Konovalov <maxim>
Status: Closed FIXED    
Severity: Affects Only Me CC: stable
Priority: Normal    
Version: 4.6-STABLE   
Hardware: Any   
OS: Any   

Description Eugene Grosbein 2002-07-04 11:20:04 UTC
	There seem to be memory leak in 'test' command that was not
	a problem when it was external command but it became a problem
	when 'test' was made builtin.

Fix: 

Unknown for me. There are workarounds, though:

	1. alias test=/bin/test; alias [=/bin/[
	This will force using standalone versions of test and
	attenuate the problem.
	2. Rebuild /bin/sh without test as builtin.
	That's enough to delete last line from /usr/src/bin/sh/builtins.def
How-To-Repeat: 
	1. Make test.sh:

	#!/bin/sh

	while :
	do
	  [ 1=1 ]
	done

	2. run top in one session and 'nice ./test.sh' in another,
	see how quickly it leaks.
Comment 1 Maxim Konovalov 2002-07-04 11:35:42 UTC
Could you please try a patch below?

Index: test/test.c
===================================================================
RCS file: /home/ncvs/src/bin/test/test.c,v
retrieving revision 1.29.2.5
diff -u -r1.29.2.5 test.c
--- test/test.c	29 Apr 2002 13:06:02 -0000	1.29.2.5
+++ test/test.c	4 Jul 2002 10:33:31 -0000
@@ -240,6 +240,7 @@
 		syntax(*t_wp, "unexpected operator");
 	(void)setregid(gid, egid);
 	(void)setreuid(uid, euid);
+	free(nargv);

 	return res;
 }

%%%

-- 
Maxim Konovalov, MAcomnet, Internet Dept., system engineer
phone: +7 (095) 796-9079, mailto:maxim@macomnet.ru
Comment 2 Eugene Grosbein 2002-07-04 12:57:24 UTC
On Thu, Jul 04, 2002 at 02:35:42PM +0400, Maxim Konovalov wrote:

> Could you please try a patch below?
> 
> Index: test/test.c
> ===================================================================
> RCS file: /home/ncvs/src/bin/test/test.c,v
> retrieving revision 1.29.2.5
> diff -u -r1.29.2.5 test.c
> --- test/test.c	29 Apr 2002 13:06:02 -0000	1.29.2.5
> +++ test/test.c	4 Jul 2002 10:33:31 -0000
> @@ -240,6 +240,7 @@
>  		syntax(*t_wp, "unexpected operator");
>  	(void)setregid(gid, egid);
>  	(void)setreuid(uid, euid);
> +	free(nargv);
> 
>  	return res;
>  }
> 
> %%%

Yes, it works. Thank you and  Stefan Farfeleder for similar patch,
I tried only this one, though.

Eugene
Comment 3 Maxim Konovalov freebsd_committer freebsd_triage 2002-07-05 11:30:50 UTC
State Changed
From-To: open->patched

Fixed in rev. 1.49 src/bin/test/test.c in -current. 


Comment 4 Maxim Konovalov freebsd_committer freebsd_triage 2002-07-05 11:30:50 UTC
Responsible Changed
From-To: freebsd-bugs->maxim

I will MFC the fix in one week.
Comment 5 Maxim Konovalov freebsd_committer freebsd_triage 2002-08-19 13:00:57 UTC
A more correct patch was committed, rev. 1.51 src/bin/test/test.c.

-- 
Maxim Konovalov, maxim@FreeBSD.org
Comment 6 Maxim Konovalov freebsd_committer freebsd_triage 2002-09-10 10:19:47 UTC
State Changed
From-To: patched->closed

Fixed in rev 1.51 and rev. 1.29.2.7 src/bin/test/test.c in -current 
and -stable. Thanks for the report.