From 9208d73274da0bd18c0d77cdf59ead3dc8e06021 Mon Sep 17 00:00:00 2001
From: liuxu <liuxu156@huawei.com>
Date: Fri, 10 May 2024 18:12:49 +0800
Subject: [PATCH 85/85] cdi:allow env variable has an empty value

Signed-off-by: liuxu <liuxu156@huawei.com>
---
 src/daemon/modules/spec/specs.c | 28 ++++++----------------------
 test/specs/specs/specs_ut.cc    | 16 ++++++++--------
 2 files changed, 14 insertions(+), 30 deletions(-)

diff --git a/src/daemon/modules/spec/specs.c b/src/daemon/modules/spec/specs.c
index 65a860d4..e779c22e 100644
--- a/src/daemon/modules/spec/specs.c
+++ b/src/daemon/modules/spec/specs.c
@@ -2607,17 +2607,11 @@ int spec_module_init(void)
 static int add_env(defs_process *dp, const char *env, const char *key)
 {
     size_t i;
-    char *oci_key = NULL;
-    char *oci_value = NULL;
-    char *saveptr = NULL;
-    __isula_auto_free char *tmp_env = NULL;
  
     for (i = 0; i < dp->env_len; i++) {
-        tmp_env = util_strdup_s(dp->env[i]);
-        oci_key = strtok_r(tmp_env, "=", &saveptr);
-        oci_value = strtok_r(NULL, "=", &saveptr);
-        if (oci_key == NULL || oci_value == NULL) {
-            ERROR("Bad env format");
+        __isula_auto_free char *oci_key = NULL;
+        if (util_valid_split_env(dp->env[i], &oci_key, NULL) < 0) {
+            ERROR("Bad env format, %s", dp->env[i]);
             return -1;
         }
         if (strcmp(key, oci_key) == 0) {
@@ -2625,8 +2619,6 @@ static int add_env(defs_process *dp, const char *env, const char *key)
             dp->env[i] = util_strdup_s(env);
             return 0;
         }
-        free(tmp_env);
-        tmp_env = NULL;
     }
     if (util_mem_realloc((void **)&dp->env, (dp->env_len + 1) * sizeof(char *),
                          (void *)dp->env, dp->env_len * sizeof(char *)) != 0) {
@@ -2641,10 +2633,6 @@ static int add_env(defs_process *dp, const char *env, const char *key)
 int defs_process_add_multiple_env(defs_process *dp, const char **envs, size_t env_len)
 {
     size_t i;
-    char *key = NULL;
-    char *value = NULL;
-    char *saveptr = NULL;
-    __isula_auto_free char *tmp_env = NULL;
 
     if (envs == NULL || env_len == 0) {
         DEBUG("empty envs");
@@ -2656,18 +2644,14 @@ int defs_process_add_multiple_env(defs_process *dp, const char **envs, size_t en
     }
 
     for (i = 0; i < env_len; i++) {
-        tmp_env = util_strdup_s(envs[i]);
-        key = strtok_r(tmp_env, "=", &saveptr);
-        value = strtok_r(NULL, "=", &saveptr);
-        if (key == NULL || value == NULL) {
-            ERROR("Bad env format: %s", tmp_env);
+        __isula_auto_free char *key = NULL;
+        if (util_valid_split_env(envs[i], &key, NULL) < 0) {
+            ERROR("Bad env format: %s", envs[i]);
             return -1;
         }
         if (add_env(dp, envs[i], key) != 0) {
             return -1;
         }
-        free(tmp_env);
-        tmp_env = NULL;
     }
 
     return 0;
diff --git a/test/specs/specs/specs_ut.cc b/test/specs/specs/specs_ut.cc
index 47836e5b..3f108f0f 100644
--- a/test/specs/specs/specs_ut.cc
+++ b/test/specs/specs/specs_ut.cc
@@ -593,20 +593,20 @@ TEST_F(SpecsUnitTest, test_defs_process_add_multiple_env)
     ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), -1);
     free(envs[0]);
     envs[0] = util_strdup_s("key0=");
-    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), -1);
+    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), 0);
     free(envs[0]);
     envs[0] = util_strdup_s("key0xxxx");
-    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), -1);
+    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), 0);
 
     free(dp->env[0]);
     dp->env[0] = util_strdup_s("=value0");
     ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), -1);
     free(dp->env[0]);
     dp->env[0] = util_strdup_s("key0=");
-    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), -1);
+    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), 0);
     free(dp->env[0]);
     dp->env[0] = util_strdup_s("key0xxxx");
-    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), -1);
+    ASSERT_EQ(defs_process_add_multiple_env(dp, (const char **)envs, env_len), 0);
 
     free_defs_process(dp);
     free(envs[0]);
@@ -644,20 +644,20 @@ TEST_F(SpecsUnitTest, test_spec_add_multiple_process_env)
     ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), -1);
     free(envs[0]);
     envs[0] = util_strdup_s("key0=");
-    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), -1);
+    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), 0);
     free(envs[0]);
     envs[0] = util_strdup_s("key0xxxx");
-    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), -1);
+    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), 0);
 
     free(oci_spec->process->env[0]);
     oci_spec->process->env[0] = util_strdup_s("=value0");
     ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), -1);
     free(oci_spec->process->env[0]);
     oci_spec->process->env[0] = util_strdup_s("key0=");
-    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), -1);
+    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), 0);
     free(oci_spec->process->env[0]);
     oci_spec->process->env[0] = util_strdup_s("key0xxxx");
-    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), -1);
+    ASSERT_EQ(spec_add_multiple_process_env(oci_spec, (const char **)envs, env_len), 0);
 
     free_oci_runtime_spec(oci_spec);
     free(envs[0]);
-- 
2.34.1

