From 5b059ba8954c6dcf305ff4def77fe394c7b57401 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Tue, 30 Aug 2022 23:40:19 +0200
Subject: [PATCH] getparam: correctly clean args

Follow-up to bf7e887b2442783ab52

The previous fix for #9128 was incomplete and caused #9397.

Fixes #9397
Closes #9399
---
 src/tool_getparam.c | 15 ++++++++-------
 src/tool_getparam.h |  1 -
 src/tool_parsecfg.c |  2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/tool_getparam.c b/src/tool_getparam.c
index f2ad91c19..e6c6a273d 100644
--- a/src/tool_getparam.c
+++ b/src/tool_getparam.c
@@ -558,7 +558,6 @@ static void cleanarg(argv_item_t str)
 
 ParameterError getparameter(const char *flag, /* f or -long-flag */
                             char *nextarg,    /* NULL if unset */
-                            argv_item_t clearthis,
                             bool *usedarg,    /* set to TRUE if the arg
                                                  has been used */
                             struct GlobalConfig *global,
@@ -576,7 +575,9 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
   ParameterError err;
   bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
                          by using --OPTION or --no-OPTION */
-  (void)clearthis; /* for !HAVE_WRITABLE_ARGV builds */
+#ifdef HAVE_WRITABLE_ARGV
+  argv_item_t clearthis = NULL;
+#endif
   *usedarg = FALSE; /* default is that we don't use the arg */
 
   if(('-' != flag[0]) || ('-' == flag[1])) {
@@ -652,6 +653,9 @@ ParameterError getparameter(const char *flag, /* f or -long-flag */
       /* this option requires an extra parameter */
       if(!longopt && parse[1]) {
         nextarg = (char *)&parse[1]; /* this is the actual extra parameter */
+#ifdef HAVE_WRITABLE_ARGV
+        clearthis = nextarg;
+#endif
         singleopt = TRUE;   /* don't loop anymore after this */
       }
       else if(!nextarg)
@@ -2443,17 +2447,15 @@ ParameterError parse_args(struct GlobalConfig *global, int argc,
         stillflags = FALSE;
       else {
         char *nextarg = NULL;
-        argv_item_t clear = NULL;
         if(i < (argc - 1)) {
           nextarg = curlx_convert_tchar_to_UTF8(argv[i + 1]);
           if(!nextarg) {
             curlx_unicodefree(orig_opt);
             return PARAM_NO_MEM;
           }
-          clear = argv[i + 1];
         }
 
-        result = getparameter(orig_opt, nextarg, clear, &passarg,
+        result = getparameter(orig_opt, nextarg, &passarg,
                               global, config);
         curlx_unicodefree(nextarg);
         config = global->last;
@@ -2492,8 +2494,7 @@ ParameterError parse_args(struct GlobalConfig *global, int argc,
       bool used;
 
       /* Just add the URL please */
-      result = getparameter("--url", orig_opt, NULL, &used, global,
-                            config);
+      result = getparameter("--url", orig_opt, &used, global, config);
     }
 
     if(!result)
diff --git a/src/tool_getparam.h b/src/tool_getparam.h
index 0564518a6..3eb177391 100644
--- a/src/tool_getparam.h
+++ b/src/tool_getparam.h
@@ -55,7 +55,6 @@ struct GlobalConfig;
 struct OperationConfig;
 
 ParameterError getparameter(const char *flag, char *nextarg,
-                            argv_item_t clearthis,
                             bool *usedarg,
                             struct GlobalConfig *global,
                             struct OperationConfig *operation);
diff --git a/src/tool_parsecfg.c b/src/tool_parsecfg.c
index 34eb5daa9..a166757f8 100644
--- a/src/tool_parsecfg.c
+++ b/src/tool_parsecfg.c
@@ -223,7 +223,7 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
 #ifdef DEBUG_CONFIG
       fprintf(stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
 #endif
-      res = getparameter(option, param, NULL, &usedarg, global, operation);
+      res = getparameter(option, param, &usedarg, global, operation);
       operation = global->last;
 
       if(!res && param && *param && !usedarg)
-- 
2.33.0

