summaryrefslogtreecommitdiff
path: root/lib/share.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2021-01-19 08:23:52 +0100
committerDaniel Stenberg <daniel@haxx.se>2021-01-20 08:51:01 +0100
commit942cf12c2f73c4bb13858acc9101b1d644bf7c50 (patch)
tree0478f4aa93712d7b06b39da268aaae9ec102f312 /lib/share.c
parent13bc1ea9bcef34c4beb384549e4be64e341426db (diff)
downloadcurl-942cf12c2f73c4bb13858acc9101b1d644bf7c50.tar.gz
urldata: make magic be the first struct field
By making the `magic` identifier the same size and at the same place within the structs (easy, multi, share), libcurl will be able to more reliably detect and safely error out if an application passes in the wrong handle to APIs. Easier to detect and less likely to cause crashes if done. Such mixups can't be detected at compile-time due to them being typedefed void pointers - unless `CURL_STRICTER` is defined. Closes #6484
Diffstat (limited to 'lib/share.c')
-rw-r--r--lib/share.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/share.c b/lib/share.c
index 5ce983033..4f1804dbd 100644
--- a/lib/share.c
+++ b/lib/share.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -37,6 +37,7 @@ curl_share_init(void)
{
struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
if(share) {
+ share->magic = CURL_GOOD_SHARE;
share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
if(Curl_mk_dnscache(&share->hostcache)) {
@@ -59,6 +60,9 @@ curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
void *ptr;
CURLSHcode res = CURLSHE_OK;
+ if(!GOOD_SHARE_HANDLE(share))
+ return CURLSHE_INVALID;
+
if(share->dirty)
/* don't allow setting options while one or more handles are already
using this share */
@@ -184,7 +188,7 @@ curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
CURLSHcode
curl_share_cleanup(struct Curl_share *share)
{
- if(share == NULL)
+ if(!GOOD_SHARE_HANDLE(share))
return CURLSHE_INVALID;
if(share->lockfunc)
@@ -218,6 +222,7 @@ curl_share_cleanup(struct Curl_share *share)
if(share->unlockfunc)
share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
+ share->magic = 0;
free(share);
return CURLSHE_OK;