From 791f5b25d32fa4598a629d25b03a152faefc0f75 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 3 Jul 2026 08:55:44 +0200 Subject: [PATCH] setopt: error for CURLOPT_SHARE when easy handle is used Changing the share while driving would be complicated and error-prone. URL: https://curl.se/mail/lib-2026-07/0000.html Closes #22253 --- lib/setopt.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/setopt.c b/lib/setopt.c index c6215e3e91..a95a11ef0e 100644 --- a/lib/setopt.c +++ b/lib/setopt.c @@ -1449,6 +1449,26 @@ static CURLcode setopt_mimepost(struct Curl_easy *data, curl_mime *mimep) #endif /* !CURL_DISABLE_MIME */ #endif /* !CURL_DISABLE_HTTP || !CURL_DISABLE_SMTP || !CURL_DISABLE_IMAP */ +static CURLcode setopt_share(struct Curl_easy *data, struct Curl_share *set) +{ + CURLcode result; + + if(data->conn) { + /* As this handle already has a connection attached, changing share now + would be complicated and error-prone */ + infof(data, "Cannot change share object while in use"); + result = CURLE_BAD_FUNCTION_ARGUMENT; + } + else { + /* disconnect from old share, if any and possible */ + result = Curl_share_easy_unlink(data); + if(!result && GOOD_SHARE_HANDLE(set)) + /* use new share if it set */ + result = Curl_share_easy_link(data, set); + } + return result; +} + /* assorted pointer type arguments */ static CURLcode setopt_pointers(struct Curl_easy *data, CURLoption option, va_list param) @@ -1496,22 +1516,8 @@ static CURLcode setopt_pointers(struct Curl_easy *data, CURLoption option, if(!s->err) s->err = stderr; break; - case CURLOPT_SHARE: { - struct Curl_share *set = va_arg(param, struct Curl_share *); - - /* disconnect from old share, if any and possible */ - result = Curl_share_easy_unlink(data); - if(result) - return result; - - /* use new share if it set */ - if(GOOD_SHARE_HANDLE(set)) { - result = Curl_share_easy_link(data, set); - if(result) - return result; - } - break; - } + case CURLOPT_SHARE: + return setopt_share(data, va_arg(param, struct Curl_share *)); default: return CURLE_UNKNOWN_OPTION;