]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
setopt: allow CURLOPT_INTERFACE to be set to NULL
authorDaniel Stenberg <daniel@haxx.se>
Wed, 21 Aug 2024 09:07:06 +0000 (11:07 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 21 Aug 2024 12:33:51 +0000 (14:33 +0200)
Ref: https://github.com/curl/curl/discussions/14299#discussioncomment-10393909
Regression from 3060557af702dd591 (shipped in 8.9.0)

Closes #14629

lib/cf-socket.c
lib/cf-socket.h
lib/setopt.c
tests/unit/unit1663.c

index 3f0fb32bf809d1a0ccadedcfee4ae70996aacb84..c1f1ac866e722b8a22bbfe32cfb71f0a0763bb1e 100644 (file)
@@ -511,32 +511,37 @@ void Curl_sndbuf_init(curl_socket_t sockfd)
  *
  * Returns CURLE_OK on success.
  */
-CURLcode Curl_parse_interface(const char *input, size_t len,
+CURLcode Curl_parse_interface(const char *input,
                               char **dev, char **iface, char **host)
 {
   static const char if_prefix[] = "if!";
   static const char host_prefix[] = "host!";
   static const char if_host_prefix[] = "ifhost!";
+  size_t len;
 
   DEBUGASSERT(dev);
   DEBUGASSERT(iface);
   DEBUGASSERT(host);
 
-  if(strncmp(if_prefix, input, strlen(if_prefix)) == 0) {
+  len = strlen(input);
+  if(len > 512)
+    return CURLE_BAD_FUNCTION_ARGUMENT;
+
+  if(!strncmp(if_prefix, input, strlen(if_prefix))) {
     input += strlen(if_prefix);
     if(!*input)
       return CURLE_BAD_FUNCTION_ARGUMENT;
     *iface = Curl_memdup0(input, len - strlen(if_prefix));
     return *iface ? CURLE_OK : CURLE_OUT_OF_MEMORY;
   }
-  if(strncmp(host_prefix, input, strlen(host_prefix)) == 0) {
+  else if(!strncmp(host_prefix, input, strlen(host_prefix))) {
     input += strlen(host_prefix);
     if(!*input)
       return CURLE_BAD_FUNCTION_ARGUMENT;
     *host = Curl_memdup0(input, len - strlen(host_prefix));
     return *host ? CURLE_OK : CURLE_OUT_OF_MEMORY;
   }
-  if(strncmp(if_host_prefix, input, strlen(if_host_prefix)) == 0) {
+  else if(!strncmp(if_host_prefix, input, strlen(if_host_prefix))) {
     const char *host_part;
     input += strlen(if_host_prefix);
     len -= strlen(if_host_prefix);
index 6040058b0955d194502686ab08d20a3ba1c03442..35225f153cb4e557325bf63f36f338d306d48d99 100644 (file)
@@ -57,7 +57,7 @@ struct Curl_sockaddr_ex {
 /*
  * Parse interface option, and return the interface name and the host part.
 */
-CURLcode Curl_parse_interface(const char *input, size_t len,
+CURLcode Curl_parse_interface(const char *input,
                               char **dev, char **iface, char **host);
 
 /*
index 538bd52ad802e4a8caee8b0129908a4666b6c848..9d695e09d3f54e53b72a5f8b6a8bff3228306dea 100644 (file)
@@ -139,30 +139,24 @@ static CURLcode setstropt_userpwd(char *option, char **userp, char **passwdp)
   return CURLE_OK;
 }
 
-static CURLcode setstropt_interface(
-  char *option, char **devp, char **ifacep, char **hostp)
+static CURLcode setstropt_interface(char *option, char **devp,
+                                    char **ifacep, char **hostp)
 {
   char *dev = NULL;
   char *iface = NULL;
   char *host = NULL;
-  size_t len;
   CURLcode result;
 
   DEBUGASSERT(devp);
   DEBUGASSERT(ifacep);
   DEBUGASSERT(hostp);
 
-  /* Parse the interface details */
-  if(!option || !*option)
-    return CURLE_BAD_FUNCTION_ARGUMENT;
-  len = strlen(option);
-  if(len > 255)
-    return CURLE_BAD_FUNCTION_ARGUMENT;
-
-  result = Curl_parse_interface(option, len, &dev, &iface, &host);
-  if(result)
-    return result;
-
+  if(option) {
+    /* Parse the interface details if set, otherwise clear them all */
+    result = Curl_parse_interface(option, &dev, &iface, &host);
+    if(result)
+      return result;
+  }
   free(*devp);
   *devp = dev;
 
index f4801fe5cc6dc4ea7c509f23a6f1d46ffc9d1ac3..e7fe621666c349fd42c4d564aeac69d39a69cf09 100644 (file)
@@ -58,8 +58,7 @@ static void test_parse(
   char *dev = NULL;
   char *iface = NULL;
   char *host = NULL;
-  CURLcode rc = Curl_parse_interface(
-    input, strlen(input), &dev, &iface, &host);
+  CURLcode rc = Curl_parse_interface(input, &dev, &iface, &host);
   fail_unless(rc == exp_rc, "Curl_parse_interface() failed");
 
   fail_unless(!!exp_dev == !!dev, "dev expectation failed.");