Skip to content

Commit

Permalink
lib: tidy up types and casts
Browse files Browse the repository at this point in the history
Cherry-picked from curl#13489
Closes curl#13862
  • Loading branch information
vszakats committed Jun 5, 2024
1 parent ad837e9 commit 72abf7c
Show file tree
Hide file tree
Showing 49 changed files with 199 additions and 162 deletions.
8 changes: 4 additions & 4 deletions lib/altsvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static CURLcode altsvc_out(struct altsvc *as, FILE *fp)
"%s %s%s%s %u "
"\"%d%02d%02d "
"%02d:%02d:%02d\" "
"%u %d\n",
"%u %u\n",
Curl_alpnid2str(as->src.alpnid),
src6_pre, as->src.host, src6_post,
as->src.port,
Expand Down Expand Up @@ -462,7 +462,7 @@ static time_t altsvc_debugtime(void *unused)
char *timestr = getenv("CURL_TIME");
(void)unused;
if(timestr) {
unsigned long val = strtol(timestr, NULL, 10);
long val = strtol(timestr, NULL, 10);
return (time_t)val;
}
return time(NULL);
Expand Down Expand Up @@ -624,7 +624,7 @@ CURLcode Curl_altsvc_parse(struct Curl_easy *data,
num = strtoul(value_ptr, &end_ptr, 10);
if((end_ptr != value_ptr) && (num < ULONG_MAX)) {
if(strcasecompare("ma", option))
maxage = num;
maxage = (time_t)num;
else if(strcasecompare("persist", option) && (num == 1))
persist = TRUE;
}
Expand Down Expand Up @@ -696,7 +696,7 @@ bool Curl_altsvc_lookup(struct altsvcinfo *asi,
if((as->src.alpnid == srcalpnid) &&
hostcompare(srchost, as->src.host) &&
(as->src.port == srcport) &&
(versions & as->dst.alpnid)) {
(versions & (int)as->dst.alpnid)) {
/* match */
*dstentry = as;
return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion lib/altsvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct altsvc {
struct althost dst;
time_t expires;
bool persist;
int prio;
unsigned int prio;
struct Curl_llist_element node;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/asyn-ares.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ static int waitperform(struct Curl_easy *data, timediff_t timeout_ms)
}

if(num) {
nfds = Curl_poll(pfd, num, timeout_ms);
nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
if(nfds < 0)
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/asyn-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ static bool init_resolve_thread(struct Curl_easy *data,
NULL, &td->tsd.w8.overlapped,
&query_complete, &td->tsd.w8.cancel_ev);
if(err != WSA_IO_PENDING)
query_complete(err, 0, &td->tsd.w8.overlapped);
query_complete((DWORD)err, 0, &td->tsd.w8.overlapped);
return TRUE;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cf-h2-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ static ssize_t cf_h2_proxy_send(struct Curl_cfilter *cf,
/* Unable to send all data, due to connection blocked or H2 window
* exhaustion. Data is left in our stream buffer, or nghttp2's internal
* frame buffer or our network out buffer. */
size_t rwin = nghttp2_session_get_stream_remote_window_size(
size_t rwin = (size_t)nghttp2_session_get_stream_remote_window_size(
ctx->h2, ctx->tunnel.stream_id);
if(rwin == 0) {
/* H2 flow window exhaustion.
Expand Down
14 changes: 8 additions & 6 deletions lib/cf-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ tcpkeepalive(struct Curl_easy *data,
vals.onoff = 1;
optval = curlx_sltosi(data->set.tcp_keepidle);
KEEPALIVE_FACTOR(optval);
vals.keepalivetime = optval;
vals.keepalivetime = (u_long)optval;
optval = curlx_sltosi(data->set.tcp_keepintvl);
KEEPALIVE_FACTOR(optval);
vals.keepaliveinterval = optval;
vals.keepaliveinterval = (u_long)optval;
if(WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, (LPVOID) &vals, sizeof(vals),
NULL, 0, &dummy, NULL, NULL) != 0) {
infof(data, "Failed to set SIO_KEEPALIVE_VALS on fd "
Expand Down Expand Up @@ -288,7 +288,7 @@ void Curl_sock_assign_addr(struct Curl_sockaddr_ex *dest,
dest->protocol = IPPROTO_UDP;
break;
}
dest->addrlen = ai->ai_addrlen;
dest->addrlen = (unsigned int)ai->ai_addrlen;

if(dest->addrlen > sizeof(struct Curl_sockaddr_storage))
dest->addrlen = sizeof(struct Curl_sockaddr_storage);
Expand Down Expand Up @@ -1063,7 +1063,7 @@ static CURLcode set_remote_ip(struct Curl_cfilter *cf,
struct cf_socket_ctx *ctx = cf->ctx;

/* store remote address and port used in this connection attempt */
if(!Curl_addr2string(&ctx->addr.sa_addr, ctx->addr.addrlen,
if(!Curl_addr2string(&ctx->addr.sa_addr, (curl_socklen_t)ctx->addr.addrlen,
ctx->ip.remote_ip, &ctx->ip.remote_port)) {
char buffer[STRERROR_LEN];

Expand Down Expand Up @@ -1247,7 +1247,8 @@ static int do_connect(struct Curl_cfilter *cf, struct Curl_easy *data,
#endif
}
else {
rc = connect(ctx->sock, &ctx->addr.sa_addr, ctx->addr.addrlen);
rc = connect(ctx->sock, &ctx->addr.sa_addr,
(curl_socklen_t)ctx->addr.addrlen);
}
return rc;
}
Expand Down Expand Up @@ -1785,7 +1786,8 @@ static CURLcode cf_udp_setup_quic(struct Curl_cfilter *cf,
/* On macOS OpenSSL QUIC fails on connected sockets.
* see: <https://github.com/openssl/openssl/issues/23251> */
#else
rc = connect(ctx->sock, &ctx->addr.sa_addr, ctx->addr.addrlen);
rc = connect(ctx->sock, &ctx->addr.sa_addr,
(curl_socklen_t)ctx->addr.addrlen);
if(-1 == rc) {
return socket_connect_result(data, ctx->ip.remote_ip, SOCKERRNO);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/content_encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,13 @@ static CURLcode gzip_do_write(struct Curl_easy *data,
/* Append the new block of data to the previous one */
memcpy(z->next_in + z->avail_in - nbytes, buf, nbytes);

switch(check_gzip_header(z->next_in, z->avail_in, &hlen)) {
switch(check_gzip_header(z->next_in, (ssize_t)z->avail_in, &hlen)) {
case GZIP_OK:
/* This is the zlib stream data */
free(z->next_in);
/* Don't point into the malloced block since we just freed it */
z->next_in = (Bytef *) buf + hlen + nbytes - z->avail_in;
z->avail_in = (uInt) (z->avail_in - hlen);
z->avail_in = z->avail_in - (uInt)hlen;
zp->zlib_init = ZLIB_GZIP_INFLATING; /* Inflating stream state */
break;

Expand Down
3 changes: 2 additions & 1 deletion lib/cookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ static size_t cookie_hash_domain(const char *domain, const size_t len)
size_t h = 5381;

while(domain < end) {
size_t j = (size_t)Curl_raw_toupper(*domain++);
h += h << 5;
h ^= Curl_raw_toupper(*domain++);
h ^= j;
}

return (h % COOKIE_HASH_SIZE);
Expand Down
8 changes: 8 additions & 0 deletions lib/curl_addrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ Curl_he2ai(const struct hostent *he, int port)
addr = (void *)ai->ai_addr; /* storage area for this info */

memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
#ifdef __MINGW32__
addr->sin_family = (short)(he->h_addrtype);
#else
addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
#endif
addr->sin_port = htons((unsigned short)port);
break;

Expand All @@ -326,7 +330,11 @@ Curl_he2ai(const struct hostent *he, int port)
addr6 = (void *)ai->ai_addr; /* storage area for this info */

memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
#ifdef __MINGW32__
addr6->sin6_family = (short)(he->h_addrtype);
#else
addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
#endif
addr6->sin6_port = htons((unsigned short)port);
break;
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/curl_fnmatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static int parsekeyword(unsigned char **pattern, unsigned char *charset)
unsigned char *p = *pattern;
bool found = FALSE;
for(i = 0; !found; i++) {
char c = *p++;
char c = (char)*p++;
if(i >= KEYLEN)
return SETCHARSET_FAIL;
switch(state) {
Expand Down
20 changes: 10 additions & 10 deletions lib/curl_ntlm_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@
*/
static void extend_key_56_to_64(const unsigned char *key_56, char *key)
{
key[0] = key_56[0];
key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
key[0] = (char)key_56[0];
key[1] = (char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
key[2] = (char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
key[3] = (char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
key[4] = (char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
key[5] = (char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
key[6] = (char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
key[7] = (char) ((key_56[6] << 1) & 0xFF);
}
#endif

Expand Down Expand Up @@ -466,13 +466,13 @@ static void time2filetime(struct ms_filetime *ft, time_t t)
unsigned int r, s;
unsigned int i;

ft->dwLowDateTime = t & 0xFFFFFFFF;
ft->dwLowDateTime = (unsigned int)t & 0xFFFFFFFF;
ft->dwHighDateTime = 0;

# ifndef HAVE_TIME_T_UNSIGNED
/* Extend sign if needed. */
if(ft->dwLowDateTime & 0x80000000)
ft->dwHighDateTime = ~0;
ft->dwHighDateTime = ~(unsigned int)0;
# endif

/* Bias seconds to Jan 1, 1601.
Expand Down
18 changes: 13 additions & 5 deletions lib/doh.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ static DOHcode skipqname(const unsigned char *doh, size_t dohlen,
return DOH_OK;
}

static unsigned short get16bit(const unsigned char *doh, int index)
static unsigned short get16bit(const unsigned char *doh, unsigned int index)
{
return (unsigned short)((doh[index] << 8) | doh[index + 1]);
}

static unsigned int get32bit(const unsigned char *doh, int index)
static unsigned int get32bit(const unsigned char *doh, unsigned int index)
{
/* make clang and gcc optimize this to bswap by incrementing
the pointer first. */
Expand Down Expand Up @@ -606,7 +606,7 @@ static DOHcode store_cname(const unsigned char *doh,

/* move to the new index */
newpos = (length & 0x3f) << 8 | doh[index + 1];
index = newpos;
index = (unsigned int)newpos;
continue;
}
else if(length & 0xc0)
Expand Down Expand Up @@ -670,7 +670,7 @@ static DOHcode rdata(const unsigned char *doh,
break;
#endif
case DNS_TYPE_CNAME:
rc = store_cname(doh, dohlen, index, d);
rc = store_cname(doh, dohlen, (unsigned int)index, d);
if(rc)
return rc;
break;
Expand Down Expand Up @@ -771,7 +771,7 @@ UNITTEST DOHcode doh_decode(const unsigned char *doh,
if(dohlen < (index + rdlength))
return DOH_DNS_OUT_OF_RANGE;

rc = rdata(doh, dohlen, rdlength, type, index, d);
rc = rdata(doh, dohlen, rdlength, type, (int)index, d);
if(rc)
return rc; /* bad rdata */
index += rdlength;
Expand Down Expand Up @@ -967,7 +967,11 @@ static CURLcode doh2ai(const struct dohentry *de, const char *hostname,
addr = (void *)ai->ai_addr; /* storage area for this info */
DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
#ifdef __MINGW32__
addr->sin_family = (short)addrtype;
#else
addr->sin_family = addrtype;
#endif
addr->sin_port = htons((unsigned short)port);
break;

Expand All @@ -976,7 +980,11 @@ static CURLcode doh2ai(const struct dohentry *de, const char *hostname,
addr6 = (void *)ai->ai_addr; /* storage area for this info */
DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
#ifdef __MINGW32__
addr6->sin6_family = (short)addrtype;
#else
addr6->sin6_family = addrtype;
#endif
addr6->sin6_port = htons((unsigned short)port);
break;
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/easy.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
before = Curl_now();

/* wait for activity or timeout */
pollrc = Curl_poll(fds, numfds, ev->ms);
pollrc = Curl_poll(fds, (unsigned int)numfds, ev->ms);
if(pollrc < 0)
return CURLE_UNRECOVERABLE_POLL;

Expand Down
11 changes: 6 additions & 5 deletions lib/escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ char *curl_easy_escape(struct Curl_easy *data, const char *string,
return strdup("");

while(length--) {
unsigned char in = *string++; /* treat the characters unsigned */
/* treat the characters unsigned */
unsigned char in = (unsigned char)*string++;

if(ISUNRESERVED(in)) {
/* append this */
Expand Down Expand Up @@ -137,7 +138,7 @@ CURLcode Curl_urldecode(const char *string, size_t length,
*ostring = ns;

while(alloc) {
unsigned char in = *string;
unsigned char in = (unsigned char)*string;
if(('%' == in) && (alloc > 2) &&
ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
/* this is two hexadecimal digits following a '%' */
Expand All @@ -157,7 +158,7 @@ CURLcode Curl_urldecode(const char *string, size_t length,
return CURLE_URL_MALFORMAT;
}

*ns++ = in;
*ns++ = (char)in;
}
*ns = 0; /* terminate it */

Expand Down Expand Up @@ -222,8 +223,8 @@ void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
while(len-- && (olen >= 3)) {
/* clang-tidy warns on this line without this comment: */
/* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
*out++ = hex[(*src & 0xF0)>>4];
*out++ = hex[*src & 0x0F];
*out++ = (unsigned char)hex[(*src & 0xF0)>>4];
*out++ = (unsigned char)hex[*src & 0x0F];
++src;
olen -= 2;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/getinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ static CURLcode getinfo_long(struct Curl_easy *data, CURLINFO info,
#ifdef DEBUGBUILD
char *timestr = getenv("CURL_TIME");
if(timestr) {
unsigned long val = strtol(timestr, NULL, 10);
unsigned long val = strtoul(timestr, NULL, 10);
switch(info) {
case CURLINFO_LOCAL_PORT:
*param_longp = (long)val;
Expand All @@ -216,7 +216,7 @@ static CURLcode getinfo_long(struct Curl_easy *data, CURLINFO info,
/* use another variable for this to allow different values */
timestr = getenv("CURL_DEBUG_SIZE");
if(timestr) {
unsigned long val = strtol(timestr, NULL, 10);
unsigned long val = strtoul(timestr, NULL, 10);
switch(info) {
case CURLINFO_HEADER_SIZE:
case CURLINFO_REQUEST_SIZE:
Expand Down Expand Up @@ -335,7 +335,7 @@ static CURLcode getinfo_long(struct Curl_easy *data, CURLINFO info,
}
break;
case CURLINFO_PROTOCOL:
*param_longp = data->info.conn_protocol;
*param_longp = (long)data->info.conn_protocol;
break;
case CURLINFO_USED_PROXY:
*param_longp =
Expand All @@ -361,7 +361,7 @@ static CURLcode getinfo_offt(struct Curl_easy *data, CURLINFO info,
#ifdef DEBUGBUILD
char *timestr = getenv("CURL_TIME");
if(timestr) {
unsigned long val = strtol(timestr, NULL, 10);
unsigned long val = strtoul(timestr, NULL, 10);
switch(info) {
case CURLINFO_TOTAL_TIME_T:
case CURLINFO_NAMELOOKUP_TIME_T:
Expand Down Expand Up @@ -450,7 +450,7 @@ static CURLcode getinfo_double(struct Curl_easy *data, CURLINFO info,
#ifdef DEBUGBUILD
char *timestr = getenv("CURL_TIME");
if(timestr) {
unsigned long val = strtol(timestr, NULL, 10);
unsigned long val = strtoul(timestr, NULL, 10);
switch(info) {
case CURLINFO_TOTAL_TIME:
case CURLINFO_NAMELOOKUP_TIME:
Expand Down
3 changes: 2 additions & 1 deletion lib/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
size_t h = 5381;

while(key_str < end) {
size_t j = (size_t)*key_str++;
h += h << 5;
h ^= *key_str++;
h ^= j;
}

return (h % slots_num);
Expand Down
Loading

0 comments on commit 72abf7c

Please sign in to comment.