From 5446c18fe129fa81a9a05c294fbc4e5d7c1c71a5 Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Sat, 27 Apr 2024 14:32:19 +0800
Subject: [PATCH 210/213] allow env variable has an empty value

Signed-off-by: jikai <jikai11@huawei.com>
---
 src/daemon/modules/spec/specs_extend.c | 10 ++++++----
 src/utils/cutils/utils_string.c        |  3 +++
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/daemon/modules/spec/specs_extend.c b/src/daemon/modules/spec/specs_extend.c
index 5ede7936..a9e8142e 100644
--- a/src/daemon/modules/spec/specs_extend.c
+++ b/src/daemon/modules/spec/specs_extend.c
@@ -196,6 +196,7 @@ static int generate_env_map_from_file(FILE *fp, json_map_string_string *env_map)
     char *pline = NULL;
     size_t length = 0;
     char *saveptr = NULL;
+    char empty_str[1] = {'\0'};
 
     while (getline(&pline, &length, fp) != -1) {
         util_trim_newline(pline);
@@ -205,7 +206,9 @@ static int generate_env_map_from_file(FILE *fp, json_map_string_string *env_map)
         }
         key = strtok_r(pline, "=", &saveptr);
         value = strtok_r(NULL, "=", &saveptr);
-        if (key != NULL && value != NULL) {
+        // value of an env varible is allowed to be empty
+        value = value ? value : empty_str;
+        if (key != NULL) {
             key = util_trim_space(key);
             value = util_trim_space(value);
             if ((size_t)(MAX_BUFFER_SIZE - 1) - strlen(key) < strlen(value)) {
@@ -292,15 +295,14 @@ static int check_env_need_append(const oci_runtime_spec *oci_spec, const char *e
 {
     size_t i = 0;
     char *key = NULL;
-    char *value = NULL;
     char *saveptr = NULL;
 
     for (i = 0; i < oci_spec->process->env_len; i++) {
         char *tmp_env = NULL;
         tmp_env = util_strdup_s(oci_spec->process->env[i]);
         key = strtok_r(tmp_env, "=", &saveptr);
-        value = strtok_r(NULL, "=", &saveptr);
-        if (key == NULL || value == NULL) {
+        // value of an env varible is allowed to be empty
+        if (key == NULL) {
             ERROR("Bad env format");
             free(tmp_env);
             tmp_env = NULL;
diff --git a/src/utils/cutils/utils_string.c b/src/utils/cutils/utils_string.c
index 64afb570..11a65f19 100644
--- a/src/utils/cutils/utils_string.c
+++ b/src/utils/cutils/utils_string.c
@@ -534,6 +534,9 @@ static char *util_left_trim_space(char *str)
 {
     char *begin = str;
     char *tmp = str;
+    if (strlen(str) == 0) {
+        return str;
+    }
     while (isspace(*begin)) {
         begin++;
     }
-- 
2.25.1

