mirror of
https://github.com/curl/curl.git
synced 2026-04-11 12:01:42 +08:00
The issues found fell into these categories, with the applied fixes:
- const was accidentally stripped.
Adjust code to not cast or cast with const.
- const/volatile missing from arguments, local variables.
Constify arguments or variables, adjust/delete casts. Small code
changes in a few places.
- const must be stripped because an API dependency requires it.
Strip `const` with `CURL_UNCONST()` macro to silence the warning out
of our control. These happen at API boundaries. Sometimes they depend
on dependency version, which this patch handles as necessary. Also
enable const support for the zlib API, using `ZLIB_CONST`. Supported
by zlib 1.2.5.2 and newer.
- const must be stripped because a curl API requires it.
Strip `const` with `CURL_UNCONST()` macro to silence the warning out
of our immediate control. For example we promise to send a non-const
argument to a callback, though the data is const internally.
- other cases where we may avoid const stripping by code changes.
Also silenced with `CURL_UNCONST()`.
- there are 3 places where `CURL_UNCONST()` is cast again to const.
To silence this type of warning:
```
lib/vquic/curl_osslq.c:1015:29: error: to be safe all intermediate
pointers in cast from 'unsigned char **' to 'const unsigned char **'
must be 'const' qualified [-Werror=cast-qual]
lib/cf-socket.c:734:32: error: to be safe all intermediate pointers in
cast from 'char **' to 'const char **' must be 'const' qualified
[-Werror=cast-qual]
```
There may be a better solution, but I couldn't find it.
These cases are handled in separate subcommits, but without further
markup.
If you see a `-Wcast-qual` warning in curl, we appreciate your report
about it.
Closes #16142
130 lines
3.1 KiB
C
130 lines
3.1 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) 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
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
*
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
*
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
* KIND, either express or implied.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
***************************************************************************/
|
|
|
|
#include "curl_setup.h"
|
|
#include "urldata.h"
|
|
#include "bufref.h"
|
|
#include "strdup.h"
|
|
|
|
#include "curl_memory.h"
|
|
#include "memdebug.h"
|
|
|
|
#ifdef DEBUGBUILD
|
|
#define SIGNATURE 0x5c48e9b2 /* Random pattern. */
|
|
#endif
|
|
|
|
/*
|
|
* Init a bufref struct.
|
|
*/
|
|
void Curl_bufref_init(struct bufref *br)
|
|
{
|
|
DEBUGASSERT(br);
|
|
br->dtor = NULL;
|
|
br->ptr = NULL;
|
|
br->len = 0;
|
|
|
|
#ifdef DEBUGBUILD
|
|
br->signature = SIGNATURE;
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* Free the buffer and re-init the necessary fields. It does not touch the
|
|
* 'signature' field and thus this buffer reference can be reused.
|
|
*/
|
|
|
|
void Curl_bufref_free(struct bufref *br)
|
|
{
|
|
DEBUGASSERT(br);
|
|
DEBUGASSERT(br->signature == SIGNATURE);
|
|
DEBUGASSERT(br->ptr || !br->len);
|
|
|
|
if(br->ptr && br->dtor)
|
|
br->dtor(CURL_UNCONST(br->ptr));
|
|
|
|
br->dtor = NULL;
|
|
br->ptr = NULL;
|
|
br->len = 0;
|
|
}
|
|
|
|
/*
|
|
* Set the buffer reference to new values. The previously referenced buffer
|
|
* is released before assignment.
|
|
*/
|
|
void Curl_bufref_set(struct bufref *br, const void *ptr, size_t len,
|
|
void (*dtor)(void *))
|
|
{
|
|
DEBUGASSERT(ptr || !len);
|
|
DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
|
|
|
|
Curl_bufref_free(br);
|
|
br->ptr = (const unsigned char *) ptr;
|
|
br->len = len;
|
|
br->dtor = dtor;
|
|
}
|
|
|
|
/*
|
|
* Get a pointer to the referenced buffer.
|
|
*/
|
|
const unsigned char *Curl_bufref_ptr(const struct bufref *br)
|
|
{
|
|
DEBUGASSERT(br);
|
|
DEBUGASSERT(br->signature == SIGNATURE);
|
|
DEBUGASSERT(br->ptr || !br->len);
|
|
|
|
return br->ptr;
|
|
}
|
|
|
|
/*
|
|
* Get the length of the referenced buffer data.
|
|
*/
|
|
size_t Curl_bufref_len(const struct bufref *br)
|
|
{
|
|
DEBUGASSERT(br);
|
|
DEBUGASSERT(br->signature == SIGNATURE);
|
|
DEBUGASSERT(br->ptr || !br->len);
|
|
|
|
return br->len;
|
|
}
|
|
|
|
CURLcode Curl_bufref_memdup(struct bufref *br, const void *ptr, size_t len)
|
|
{
|
|
unsigned char *cpy = NULL;
|
|
|
|
DEBUGASSERT(br);
|
|
DEBUGASSERT(br->signature == SIGNATURE);
|
|
DEBUGASSERT(br->ptr || !br->len);
|
|
DEBUGASSERT(ptr || !len);
|
|
DEBUGASSERT(len <= CURL_MAX_INPUT_LENGTH);
|
|
|
|
if(ptr) {
|
|
cpy = Curl_memdup0(ptr, len);
|
|
if(!cpy)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
Curl_bufref_set(br, cpy, len, curl_free);
|
|
return CURLE_OK;
|
|
}
|