From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2F56A206953; Wed, 16 Oct 2024 12:17:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=163.172.96.212 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729081066; cv=none; b=H1htrlBjCW4oDd0NVRGSDBdFlq5YTnyxOwVi+UBDCc/6CLFtTl5ZeywWVVSLwzy3y1cH/VJkoIsZDzfsMCIP/qJfKVHG/nxW95Wf0N3oXCVzNnJwO9F13uAOWGZFIIUujsIPV4iaYXcqFymXySqfYXWBG2yDWm2e71wbosDULEo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729081066; c=relaxed/simple; bh=bO5v/NOUlppeLzNBHzyNKSewCI43HW1yo0iNqORVgFM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=C92FGixe4ri9Rzw3TSzzO4LAXNL8ztRiMjzcNNuL8St5Z2a+RN7pt/IEBMt+hXUGTHsQ2VILizaYDlG04etsnc/GQpDSvOUTyoeHiIcxhx5VJzBA2SCfiAjamQVdo/U3bIl8JQyi6OAELZ89cx1qkNtdhjJ9mCZaNMy0hB2sOzM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=1wt.eu; spf=pass smtp.mailfrom=1wt.eu; arc=none smtp.client-ip=163.172.96.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=1wt.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=1wt.eu Received: (from willy@localhost) by mail.home.local (8.17.1/8.17.1/Submit) id 49GCH8xc019277; Wed, 16 Oct 2024 14:17:08 +0200 Date: Wed, 16 Oct 2024 14:17:08 +0200 From: Willy Tarreau To: Thomas =?iso-8859-1?Q?Wei=DFschuh?= Cc: Thomas =?iso-8859-1?Q?Wei=DFschuh?= , "Paul E. McKenney" , linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] tools/nolibc/stdlib: fix getenv() with empty environment Message-ID: References: <20241016-nolibc-getenv-v1-1-8bc11abd486d@linutronix.de> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20241016-nolibc-getenv-v1-1-8bc11abd486d@linutronix.de> Hi Thomas! On Wed, Oct 16, 2024 at 01:14:51PM +0200, Thomas Weißschuh wrote: > The environ pointer itself is never NULL, this is guaranteed by crt.h. > However if the environment is empty, environ will point to a NULL > pointer. Good point, however from what I'm seeing on glibc, if the user sets environ to NULL, getenv() safely reports NULL and doesn't crash. I don't know what the spec says about environ being NULL, though. I just tested on freebsd to compare and also get a NULL in this case as well. So I'd be tempted by keeping the check. > int idx, i; > > - if (environ) { > + if (*environ) { > for (idx = 0; environ[idx]; idx++) { > for (i = 0; name[i] && name[i] == environ[idx][i];) > i++; However as a quick note, if we decide we don't care about environ being NULL, and since this is essentially a cleanup, why not even get rid of the whole "if" condition, since the loop takes care of it ? FWIW I tested glibc with this: #include int main(int argc, char **argv) { extern char **environ; environ=NULL; return getenv("HOME") == NULL; } Cheers, Willy