From: Isaac Dunham <ibid.ag@gmail.com>
To: musl@lists.openwall.com
Subject: Re: setenv if value=NULL, what say standard? Bug?
Date: Wed, 22 Apr 2015 22:52:09 -0700 [thread overview]
Message-ID: <20150423055208.GA3909@newbook> (raw)
In-Reply-To: <55383E43.8010505@skarnet.org>
On Thu, Apr 23, 2015 at 02:35:15AM +0200, Laurent Bercot wrote:
> On 23/04/2015 02:08, Jean-Marc Pigeon wrote:
> >My guess, glibc code is 'blindly" setting the NULL (as "")
> >value to the variable.
> >
> >Is the standard saying otherwise, or do we have a
> >a real bug in setenv??
>
> The standard at
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/setenv.html
> says...
>
> ... exactly nothing about the possibility of envval being NULL.
> This is, in the strictest sense, UB. :)
>
> Actually, it says "The environment variable shall be set to the value
> to which envval points." So, arguably, envval should point to something,
> and since NULL does not, it is forbidden. Another valid interpretation
> could be that envvar is set to the value to which envval points, i.e.
> no value at all, so it is unset; but it doesn't fit the spirit of
> setenv() to unset variables. The glibc interpretation, if it does what
> you think it does, is wrong in any case: the empty string is a very
> different thing from no value at all.
>
> I think the only safe conclusion is that the application is incorrect
> and should ensure that setenv() is never called with a NULL value.
I happen to have just reread
http://www.tedunangst.com/flak/post/zero-size-objects
which has this little bit of information:
The C standard has this to say in the section on “Use of library
functions”.
If an argument to a function has an invalid value (such as a value
outside the domain of the function, or a pointer outside the address
space of the program, or a null pointer, [...]) [...], the behavior
is undefined.
The section on “String function conventions” clarifies further.
Where an argument declared as size_t n specifies the length of the
array for a function, n can have the value zero on a call to that
function. Unless explicitly stated otherwise in the description of
a particular function in this subclause, pointer arguments on such
a call shall still have valid values.
In other words, passing a NULL pointer is undefined behavior unless
spelled out to the contrary, even if a good implementation would have
no reason to follow it.
Now, setenv() is part of POSIX rather than ISO C, and thus has its
own rules, but ISO C is the foundation.
As to the question of what's the "right" thing to do, consider these
two function calls:
setenv("OTHERVAL", getenv("SOMEVAL"), 1);
strcmp(getenv("OTHERVAL"), getenv("OTHERVAL"));
It should be obvious that the second is incorrect.
But it's easy to arrive there if the former is accepted.
So I've sent a patch for this to the util-linux list.
Thanks,
Isaac Dunham
prev parent reply other threads:[~2015-04-23 5:52 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-23 0:08 Jean-Marc Pigeon
2015-04-23 0:35 ` Laurent Bercot
2015-04-23 1:26 ` Jean-Marc Pigeon
2015-04-23 2:15 ` Rich Felker
2015-04-23 4:24 ` Jean-Marc Pigeon
2015-04-23 5:08 ` Raphael Cohn
2015-04-23 12:29 ` Jean-Marc Pigeon
2015-04-23 9:23 ` Laurent Bercot
2015-04-23 9:52 ` Raphael Cohn
2015-04-23 10:47 ` Laurent Bercot
2015-04-23 10:10 ` Rich Felker
2015-04-23 12:58 ` Jean-Marc Pigeon
2015-04-23 13:22 ` Szabolcs Nagy
2015-04-23 13:58 ` Jean-Marc Pigeon
2015-04-23 14:26 ` Szabolcs Nagy
2015-04-23 8:05 ` Jens Gustedt
2015-04-23 9:55 ` Rich Felker
2015-04-24 4:11 ` Isaac Dunham
2015-04-23 5:52 ` Isaac Dunham [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150423055208.GA3909@newbook \
--to=ibid.ag@gmail.com \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).