From owner-freebsd-current@FreeBSD.ORG Sat Jul 14 01:10:51 2007 Return-Path: X-Original-To: freebsd-current@FreeBSD.org Delivered-To: freebsd-current@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id F0EA616A400 for ; Sat, 14 Jul 2007 01:10:51 +0000 (UTC) (envelope-from scf@FreeBSD.org) Received: from mail.farley.org (farley.org [67.64.95.201]) by mx1.freebsd.org (Postfix) with ESMTP id 9429513C4A7 for ; Sat, 14 Jul 2007 01:10:51 +0000 (UTC) (envelope-from scf@FreeBSD.org) Received: from thor.farley.org (thor.farley.org [192.168.1.5]) by mail.farley.org (8.14.1/8.14.1) with ESMTP id l6E1CftU058187; Fri, 13 Jul 2007 20:12:41 -0500 (CDT) (envelope-from scf@FreeBSD.org) Date: Fri, 13 Jul 2007 20:10:33 -0500 (CDT) From: "Sean C. Farley" To: Andrey Chernov In-Reply-To: <20070714004116.GA22909@nagual.pp.ru> Message-ID: <20070713200048.X26971@thor.farley.org> References: <20070707205410.B14065@thor.farley.org> <20070708020940.GA80166@nagual.pp.ru> <20070708171727.GA90490@nagual.pp.ru> <20070713162742.GA16260@nagual.pp.ru> <20070713142545.K26096@thor.farley.org> <20070713202433.GA19856@nagual.pp.ru> <20070713203915.GA20270@nagual.pp.ru> <20070713171942.Q26096@thor.farley.org> <20070713224608.GB21695@nagual.pp.ru> <20070713184543.A26096@thor.farley.org> <20070714004116.GA22909@nagual.pp.ru> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.1 X-Spam-Checker-Version: SpamAssassin 3.2.1 (2007-05-02) on mail.farley.org Cc: freebsd-current Subject: Re: Environment handling broken in /bin/sh with changes to t,set,put}env() X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 Jul 2007 01:10:52 -0000 On Sat, 14 Jul 2007, Andrey Chernov wrote: > On Fri, Jul 13, 2007 at 07:08:37PM -0500, Sean C. Farley wrote: >> different. Are you saying that the addresses should not change for >> environ, environ[0-1] or all? > > I try to say that *env() functions should avoid to change memory they > not "own" when it is possible and feel free to change the memory they > "own". > > What standard says is that initial copying all environ to the internal > structure and changing it afterwards (exluding putenv case) is the > right thing (because *env() functions "owns" that memory). > > To avoid even things like > > nenv[0] = "PATH=/bin"; > setenv("PATH", "/bin", 1); > > fails, better way is to strdup() (i.e. copying) every string from the > original environ and you already do that initially in build_env() but > not do the safe way in previous merge_environ(). OK. I thought you were saying that setenv(name, value, 0) and unsetenv(name) must not, as opposed to should not, alter environ if the name was found and not found respectively. It would require a careful change, but it would be more efficient in some cases. I would prefer to keep the code simple. At least as much as I would call it simple. :) On the bright side, keeping the current behavior of rebuilding after a non-getenv() call is safe since the program cannot assume environ is the same after such a call. For example, unsetenv(somename) will return a zero regardless if the name existed or not, therefore the program cannot assume that environ is still at the same address. > BTW, I find that code in build_env() very suspicious: > > if (environ[0] == NULL) > goto SaveEnviron; > ... > SaveEnviron: > origEnviron = environ; > environ = NULL; > if (envVarsTotal > 0) { > ... > } else > rtrnVal = 0; > > return (rtrnVal); > > It ends up with environ = NULL; because envVarsTotal initialized to 0 > i.e. makes from "environ[0] == NULL" case "environ == NULL" case > which is different thing. I changed it to return (0) if either environ or environ[0] equal NULL. The SaveEnviron label is removed as well as the (envVarsTotal > 0) check. Sean -- scf@FreeBSD.org