[glib/glib-2-32] environ: Allow NULL envp
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/glib-2-32] environ: Allow NULL envp
- Date: Sat, 14 Jul 2012 23:19:23 +0000 (UTC)
commit 47fdbf894847cd7df814e4d1fcdb7fd518399421
Author: Christian Persch <chpe gnome org>
Date: Sat May 19 23:59:01 2012 +0200
environ: Allow NULL envp
https://bugzilla.gnome.org/show_bug.cgi?id=676397
glib/genviron.c | 31 ++++++++++++++++++++-----------
glib/tests/environment.c | 20 ++++++++++++++++++++
2 files changed, 40 insertions(+), 11 deletions(-)
---
diff --git a/glib/genviron.c b/glib/genviron.c
index b92c865..7512f55 100644
--- a/glib/genviron.c
+++ b/glib/genviron.c
@@ -54,6 +54,9 @@ g_environ_find (gchar **envp,
{
gint len, i;
+ if (envp == NULL)
+ return -1;
+
len = strlen (variable);
for (i = 0; envp[i]; i++)
@@ -68,8 +71,9 @@ g_environ_find (gchar **envp,
/**
* g_environ_getenv:
- * @envp: (array zero-terminated=1) (transfer none): an environment
- * list (eg, as returned from g_get_environ())
+ * @envp: (allow-none) (array zero-terminated=1) (transfer none): an environment
+ * list (eg, as returned from g_get_environ()), or %NULL
+ * for an empty environment list
* @variable: the environment variable to get, in the GLib file name
* encoding
*
@@ -96,7 +100,6 @@ g_environ_getenv (gchar **envp,
{
gint index;
- g_return_val_if_fail (envp != NULL, NULL);
g_return_val_if_fail (variable != NULL, NULL);
index = g_environ_find (envp, variable);
@@ -108,8 +111,9 @@ g_environ_getenv (gchar **envp,
/**
* g_environ_setenv:
- * @envp: (array zero-terminated=1) (transfer full): an environment
- * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ())
+ * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
+ * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()), or %NULL
+ * for an empty environment list
* @variable: the environment variable to set, must not contain '='
* @value: the value for to set the variable to
* @overwrite: whether to change the variable if it already exists
@@ -134,7 +138,6 @@ g_environ_setenv (gchar **envp,
{
gint index;
- g_return_val_if_fail (envp != NULL, NULL);
g_return_val_if_fail (variable != NULL, NULL);
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
@@ -151,7 +154,7 @@ g_environ_setenv (gchar **envp,
{
gint length;
- length = g_strv_length (envp);
+ length = envp ? g_strv_length (envp) : 0;
envp = g_renew (gchar *, envp, length + 2);
envp[length] = g_strdup_printf ("%s=%s", variable, value);
envp[length + 1] = NULL;
@@ -191,14 +194,18 @@ g_environ_unsetenv_internal (gchar **envp,
}
*f = NULL;
- return envp;
+ if (free_value_and_realloc)
+ return g_renew (gchar *, envp, envc + 1);
+ else
+ return envp;
}
/**
* g_environ_unsetenv:
- * @envp: (array zero-terminated=1) (transfer full): an environment
- * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ())
+ * @envp: (allow-none) (array zero-terminated=1) (transfer full): an environment
+ * list that can be freed using g_strfreev() (e.g., as returned from g_get_environ()),
+ * or %NULL for an empty environment list
* @variable: the environment variable to remove, must not contain '='
*
* Removes the environment variable @variable from the provided
@@ -213,10 +220,12 @@ gchar **
g_environ_unsetenv (gchar **envp,
const gchar *variable)
{
- g_return_val_if_fail (envp != NULL, NULL);
g_return_val_if_fail (variable != NULL, NULL);
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
+ if (envp == NULL)
+ return NULL;
+
return g_environ_unsetenv_internal (envp, variable, TRUE);
}
diff --git a/glib/tests/environment.c b/glib/tests/environment.c
index 1e4698c..36c875b 100644
--- a/glib/tests/environment.c
+++ b/glib/tests/environment.c
@@ -96,6 +96,25 @@ test_environ_array (void)
g_strfreev (env);
}
+static void
+test_environ_null (void)
+{
+ gchar **env;
+ const gchar *value;
+
+ env = NULL;
+
+ value = g_environ_getenv (env, "foo");
+ g_assert (value == NULL);
+
+ env = g_environ_setenv (NULL, "foo", "bar", TRUE);
+ g_assert (env != NULL);
+ g_strfreev (env);
+
+ env = g_environ_unsetenv (NULL, "foo");
+ g_assert (env == NULL);
+}
+
int
main (int argc, char **argv)
{
@@ -104,6 +123,7 @@ main (int argc, char **argv)
g_test_add_func ("/environ/listenv", test_listenv);
g_test_add_func ("/environ/setenv", test_setenv);
g_test_add_func ("/environ/array", test_environ_array);
+ g_test_add_func ("/environ/null", test_environ_null);
return g_test_run ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]