genviron: fix g_setenv(var, NULL) and some other stuff

Passing a NULL value to g_setenv() was never documented as working,
and in fact it worked on some platforms and crashed on others. Make it
g_return_if_fail() everywhere insted.

Also, remove some incorrect docs in g_environ_getenv() and
g_environ_setenv() that shouldn't have been copied from g_getenv() and
g_setenv(). And belatedly simplify the checks in g_unsetenv().

https://bugzilla.gnome.org/show_bug.cgi?id=704593
This commit is contained in:
Dan Winship 2013-10-19 11:31:06 -04:00
parent 2907d4cc71
commit d94d6ecd3f

View File

@ -81,13 +81,6 @@ g_environ_find (gchar **envp,
* Returns the value of the environment variable @variable in the * Returns the value of the environment variable @variable in the
* provided list @envp. * provided list @envp.
* *
* The name and value are in the GLib file name encoding.
* On UNIX, this means the actual bytes which might or might not
* be in some consistent character set and encoding. On Windows,
* it is in UTF-8. On Windows, in case the environment variable's
* value contains references to other environment variables, they
* are expanded.
*
* Return value: the value of the environment variable, or %NULL if * Return value: the value of the environment variable, or %NULL if
* the environment variable is not set in @envp. The returned * the environment variable is not set in @envp. The returned
* string is owned by @envp, and will be freed if @variable is * string is owned by @envp, and will be freed if @variable is
@ -112,9 +105,10 @@ g_environ_getenv (gchar **envp,
/** /**
* g_environ_setenv: * g_environ_setenv:
* @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment * @envp: (allow-none) (array zero-terminated=1) (transfer full): an
* list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL * environment list that can be freed using g_strfreev() (e.g., as
* for an empty environment list * returned from g_get_environ()), or %NULL for an empty
* environment list
* @variable: the environment variable to set, must not contain '=' * @variable: the environment variable to set, must not contain '='
* @value: the value for to set the variable to * @value: the value for to set the variable to
* @overwrite: whether to change the variable if it already exists * @overwrite: whether to change the variable if it already exists
@ -122,10 +116,6 @@ g_environ_getenv (gchar **envp,
* Sets the environment variable @variable in the provided list * Sets the environment variable @variable in the provided list
* @envp to @value. * @envp to @value.
* *
* Both the variable's name and value should be in the GLib
* file name encoding. On UNIX, this means that they can be
* arbitrary byte strings. On Windows, they should be in UTF-8.
*
* Return value: (array zero-terminated=1) (transfer full): the * Return value: (array zero-terminated=1) (transfer full): the
* updated environment list. Free it using g_strfreev(). * updated environment list. Free it using g_strfreev().
* *
@ -141,6 +131,7 @@ g_environ_setenv (gchar **envp,
g_return_val_if_fail (variable != NULL, NULL); g_return_val_if_fail (variable != NULL, NULL);
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL); g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
g_return_val_if_fail (value != NULL, NULL);
index = g_environ_find (envp, variable); index = g_environ_find (envp, variable);
if (index != -1) if (index != -1)
@ -301,6 +292,7 @@ g_setenv (const gchar *variable,
g_return_val_if_fail (variable != NULL, FALSE); g_return_val_if_fail (variable != NULL, FALSE);
g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE); g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
g_return_val_if_fail (value != NULL, FALSE);
#ifdef HAVE_SETENV #ifdef HAVE_SETENV
result = setenv (variable, value, overwrite); result = setenv (variable, value, overwrite);
@ -356,15 +348,12 @@ extern char **environ;
void void
g_unsetenv (const gchar *variable) g_unsetenv (const gchar *variable)
{ {
#ifdef HAVE_UNSETENV
g_return_if_fail (variable != NULL); g_return_if_fail (variable != NULL);
g_return_if_fail (strchr (variable, '=') == NULL); g_return_if_fail (strchr (variable, '=') == NULL);
#ifdef HAVE_UNSETENV
unsetenv (variable); unsetenv (variable);
#else /* !HAVE_UNSETENV */ #else /* !HAVE_UNSETENV */
g_return_if_fail (variable != NULL);
g_return_if_fail (strchr (variable, '=') == NULL);
/* Mess directly with the environ array. /* Mess directly with the environ array.
* This seems to be the only portable way to do this. * This seems to be the only portable way to do this.
*/ */
@ -527,6 +516,7 @@ g_setenv (const gchar *variable,
g_return_val_if_fail (variable != NULL, FALSE); g_return_val_if_fail (variable != NULL, FALSE);
g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE); g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
g_return_val_if_fail (value != NULL, FALSE);
g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE); g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE); g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);