script_setenv: fix append with val == NULL
authorDimitri Papadopoulos Orfanos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Wed, 13 Dec 2023 20:56:34 +0000 (20:56 +0000)
committerDimitri Papadopoulos Orfanos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
Wed, 13 Dec 2023 20:56:34 +0000 (20:56 +0000)
Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
script.c

index 96d57a0f26b1223b235108a14504a443d1932703..75a380912f19a8f2a6dc53b496c8f159b8923223 100644 (file)
--- a/script.c
+++ b/script.c
@@ -44,17 +44,22 @@ int script_setenv(struct openconnect_info *vpninfo,
 
        for (p = vpninfo->script_env; p; p = p->next) {
                if (!strcmp(opt, p->option)) {
-                       if (append) {
-                               if (asprintf(&str, "%s %s", p->value, val) == -1)
-                                       return -ENOMEM;
-                       } else
+                       if (append && p->value) {
+                               if (val) {
+                                       if (asprintf(&str, "%s %s", p->value, val) == -1)
+                                               return -ENOMEM;
+                                       free (p->value);
+                                       p->value = str;
+                               }
+                       } else {
                                str = val ? strdup(val) : NULL;
-
-                       free (p->value);
-                       p->value = str;
+                               free (p->value);
+                               p->value = str;
+                       }
                        return 0;
                }
        }
+
        p = malloc(sizeof(*p));
        if (!p)
                return -ENOMEM;