]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: fix strv_env_get_n for unclean arrays
authorRay Strode <rstrode@redhat.com>
Wed, 3 Aug 2016 18:35:50 +0000 (14:35 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 20 Feb 2017 23:49:14 +0000 (18:49 -0500)
If an environment array has duplicates, strv_env_get_n returns
the results for the first match. This is wrong, because later
entries in the environment are supposed to replace earlier
entries.

src/basic/env-util.c
src/test/test-env-util.c

index a28707fb4902607f40a31eb7eebb74bd9798a503..f9208d1475a5033518995fa64835737c7b73883b 100644 (file)
@@ -462,7 +462,7 @@ char *strv_env_get_n(char **l, const char *name, size_t k) {
         if (k <= 0)
                 return NULL;
 
-        STRV_FOREACH(i, l)
+        STRV_FOREACH_BACKWARDS(i, l)
                 if (strneq(*i, name, k) &&
                     (*i)[k] == '=')
                         return *i + k + 1;
index 35bb62906e161d32962d7aea2b1fe4574e525d4c..e004c518fbce4f834307c2e4ce1e3bc1fa3cedd1 100644 (file)
@@ -45,6 +45,16 @@ static void test_strv_env_delete(void) {
         assert_se(strv_length(d) == 2);
 }
 
+static void test_strv_env_get(void) {
+        char **l;
+
+        l = STRV_MAKE("ONE_OR_TWO=1", "THREE=3", "ONE_OR_TWO=2", "FOUR=4");
+
+        assert_se(streq(strv_env_get(l, "ONE_OR_TWO"), "2"));
+        assert_se(streq(strv_env_get(l, "THREE"), "3"));
+        assert_se(streq(strv_env_get(l, "FOUR"), "4"));
+}
+
 static void test_strv_env_unset(void) {
         _cleanup_strv_free_ char **l = NULL;
 
@@ -211,6 +221,7 @@ static void test_env_assignment_is_valid(void) {
 
 int main(int argc, char *argv[]) {
         test_strv_env_delete();
+        test_strv_env_get();
         test_strv_env_unset();
         test_strv_env_set();
         test_strv_env_merge();