build: enable -Wcast-qual, fix or silence compiler warnings

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
This commit is contained in:
Viktor Szakats 2025-01-31 23:20:46 +01:00
parent 8b1b5cd4d2
commit f4e23950c7
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
114 changed files with 461 additions and 409 deletions

View file

@ -43,12 +43,12 @@ CURLcode test(char *URL)
}
asize = (int)sizeof(a);
ptr = curl_easy_escape(NULL, (char *)a, asize);
ptr = curl_easy_escape(NULL, (const char *)a, asize);
printf("%s\n", ptr);
curl_free(ptr);
/* deprecated API */
ptr = curl_escape((char *)a, asize);
ptr = curl_escape((const char *)a, asize);
if(!ptr) {
res = TEST_ERR_MAJOR_BAD;
goto test_cleanup;
@ -75,12 +75,12 @@ CURLcode test(char *URL)
curl_free(ptr);
/* weird input length */
ptr = curl_easy_escape(NULL, (char *)a, -1);
ptr = curl_easy_escape(NULL, (const char *)a, -1);
printf("escape -1 length: %s\n", ptr);
/* weird input length */
outlen = 2017; /* just a value */
ptr = curl_easy_unescape(NULL, (char *)"moahahaha", -1, &outlen);
ptr = curl_easy_unescape(NULL, "moahahaha", -1, &outlen);
printf("unescape -1 length: %s %d\n", ptr, outlen);
test_cleanup:

View file

@ -56,7 +56,7 @@ CURLcode test(char *URL)
easy_setopt(curl, CURLOPT_UPLOAD, 1L);
easy_setopt(curl, CURLOPT_HEADER, 1L);
easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
pbuf.buf = (char *)testput;
pbuf.buf = (char *)CURL_UNCONST(testput);
pbuf.len = strlen(testput);
easy_setopt(curl, CURLOPT_READDATA, &pbuf);
easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(testput));

View file

@ -339,7 +339,8 @@ static CURLcode empty_multi_test(void)
/* calling curl_multi_waitfds() on multi handle with added easy handle. */
easy_init(easy);
if(set_easy((char *)"http://example.com", easy, TEST_USE_HTTP1) != CURLE_OK)
if(set_easy((char *)CURL_UNCONST("http://example.com"), easy,
TEST_USE_HTTP1) != CURLE_OK)
goto test_cleanup;
multi_add_handle(multi, easy);

View file

@ -95,7 +95,7 @@ test_thread(void *ptr)
for(i = 0; i < PER_THREAD_SIZE; i++) {
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, (char *)ctx->URL);
curl_easy_setopt(curl, CURLOPT_URL, (char *)CURL_UNCONST(ctx->URL));
/* use the share object */
curl_easy_setopt(curl, CURLOPT_SHARE, ctx->share);

View file

@ -335,7 +335,7 @@ CURLcode test(char *URL)
void *conv_to_network_cb = NULL;
void *conv_from_utf8_cb = NULL;
void *interleavecb = NULL;
char *stringpointerextra = (char *)"moooo";
char *stringpointerextra = (char *)CURL_UNCONST("moooo");
struct curl_slist *slist = NULL;
struct curl_httppost *httppost = NULL;
curl_mime *mimepost = NULL;
@ -348,7 +348,7 @@ CURLcode test(char *URL)
curl_socket_t sockfd;
struct curl_certinfo *certinfo;
struct curl_tlssessioninfo *tlssession;
struct curl_blob blob = { (void *)"silly", 5, 0};
struct curl_blob blob = { CURL_UNCONST("silly"), 5, 0};
CURLcode res = CURLE_OK;
(void)URL; /* not used */
global_init(CURL_GLOBAL_ALL);

View file

@ -218,7 +218,7 @@ OM_uint32 gss_init_sec_context(OM_uint32 *min,
/* Note: this is using the *real* snprintf() and not the curl provided
one */
used = (size_t) snprintf(token, length, "%s:%s:%d:", creds,
(char *) target_name, ctx->sent);
(const char *)target_name, ctx->sent);
if(used >= length) {
free(token);

View file

@ -326,7 +326,7 @@ static size_t decode_length(unsigned char *buffer,
/* return 0 on success */
static int publish(FILE *dump,
curl_socket_t fd, unsigned short packetid,
char *topic, char *payload, size_t payloadlen)
char *topic, const char *payload, size_t payloadlen)
{
size_t topiclen = strlen(topic);
unsigned char *packet;
@ -607,7 +607,7 @@ static curl_socket_t mqttit(curl_socket_t fd)
}
}
else {
char *def = (char *)"this is random payload yes yes it is";
const char *def = "this is random payload yes yes it is";
publish(dump, fd, packet_id, topic, def, strlen(def));
}
disconnect(dump, fd);

View file

@ -135,7 +135,7 @@ int main(int argc, char *argv[])
struct hostent *he; /* gethostbyname() resolve */
#ifdef __AMIGA__
he = gethostbyname((unsigned char *)host);
he = gethostbyname((unsigned char *)CURL_UNCONST(host));
#else
he = gethostbyname(host);
#endif

View file

@ -324,7 +324,7 @@ static struct tftphdr *r_init(void)
/* Have emptied current buffer by sending to net and getting ack.
Free it and return next buffer filled with data.
*/
static int readit(struct testcase *test, struct tftphdr **dpp,
static int readit(struct testcase *test, struct tftphdr * volatile *dpp,
int convert /* if true, convert to ASCII */)
{
struct bf *b;
@ -1163,7 +1163,7 @@ static void sendtftp(struct testcase *test, const struct formats *pf)
mysignal(SIGALRM, timer);
#endif
do {
size = readit(test, (struct tftphdr **)&sdp, pf->f_convert);
size = readit(test, (struct tftphdr * volatile *)&sdp, pf->f_convert);
if(size < 0) {
nak(errno + 100);
return;

View file

@ -62,7 +62,7 @@ UNITTEST_START
/*
* Test a non existent login in our netrc file.
*/
s_login = (char *)"me";
s_login = (char *)CURL_UNCONST("me");
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
@ -73,7 +73,7 @@ UNITTEST_START
/*
* Test a non existent login and host in our netrc file.
*/
s_login = (char *)"me";
s_login = (char *)CURL_UNCONST("me");
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"test.example.com", &s_login, &s_password, arg);
@ -85,7 +85,7 @@ UNITTEST_START
* Test a non existent login (substring of an existing one) in our
* netrc file.
*/
s_login = (char *)"admi";
s_login = (char *)CURL_UNCONST("admi");
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
@ -97,7 +97,7 @@ UNITTEST_START
* Test a non existent login (superstring of an existing one)
* in our netrc file.
*/
s_login = (char *)"adminn";
s_login = (char *)CURL_UNCONST("adminn");
Curl_netrc_init(&store);
result = Curl_parsenetrc(&store,
"example.com", &s_login, &s_password, arg);
@ -177,7 +177,6 @@ UNITTEST_START
abort_unless(s_login != NULL, "returned NULL!");
fail_unless(strncmp(s_login, "none", 4) == 0, "login should be 'none'");
Curl_netrc_cleanup(&store);
}
UNITTEST_STOP

View file

@ -177,7 +177,7 @@ UNITTEST_START
if(req[i].packet && memcmp(req[i].packet, buffer, size)) {
fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
fprintf(stderr, "... instead of: %s\n",
hexdump((unsigned char *)req[i].packet, size));
hexdump((const unsigned char *)req[i].packet, size));
abort_if(req[i].packet && memcmp(req[i].packet, buffer, size),
"contents");
}

View file

@ -49,7 +49,7 @@ static void unit_stop(void)
UNITTEST_START
{
char *buffer = NULL;
const char *buffer = NULL;
CURLcode result = CURLE_OK;
/**
@ -72,7 +72,7 @@ UNITTEST_START
abort_unless(buffer, "Out of memory");
Curl_bufref_set(&bufref, buffer, 13, test_free);
fail_unless((char *) bufref.ptr == buffer, "Referenced data badly set");
fail_unless((const char *)bufref.ptr == buffer, "Referenced data badly set");
fail_unless(bufref.len == 13, "Data size badly set");
fail_unless(bufref.dtor == test_free, "Destructor badly set");
@ -80,7 +80,7 @@ UNITTEST_START
* testing Curl_bufref_ptr
*/
fail_unless((char *) Curl_bufref_ptr(&bufref) == buffer,
fail_unless((const char *) Curl_bufref_ptr(&bufref) == buffer,
"Wrong pointer value returned");
/**
@ -96,8 +96,8 @@ UNITTEST_START
result = Curl_bufref_memdup(&bufref, "1661", 3);
abort_unless(result == CURLE_OK, curl_easy_strerror(result));
fail_unless(freecount == 1, "Destructor not called");
fail_unless((char *) bufref.ptr != buffer, "Returned pointer not set");
buffer = (char *) Curl_bufref_ptr(&bufref);
fail_unless((const char *)bufref.ptr != buffer, "Returned pointer not set");
buffer = (const char *)Curl_bufref_ptr(&bufref);
fail_unless(buffer, "Allocated pointer is NULL");
fail_unless(bufref.len == 3, "Wrong data size stored");
fail_unless(!buffer[3], "Duplicated data should have been truncated");

View file

@ -84,7 +84,7 @@ UNITTEST_START
list[0].cp = calloc(1, too_long + 1);
fail_unless(list[0].cp, "could not alloc too long value");
memset((void *)list[0].cp, 'a', too_long);
memset(CURL_UNCONST(list[0].cp), 'a', too_long);
for(i = 0; list[i].home; i++) {
char *path;
@ -112,7 +112,7 @@ UNITTEST_START
}
}
free((void *)list[0].cp);
free(CURL_UNCONST(list[0].cp));
}
#if defined(CURL_GNUC_DIAG) || defined(__clang__)
#pragma GCC diagnostic pop