Skip to content

Commit 3b10e57

Browse files
authored
Fix internal datatype usage and warnings (#573)
PR #568 increased the warning levels and c-ares code emitted a bunch of warnings. This PR fixes those warnings and starts transitioning internal data types into more proper forms (e.g. data lengths should be size_t not int). It does, however, have to manually cast back to what the public API needs due to API and ABI compliance (we aren't looking to break integrations, just clean up internals). Fix By: Brad House (@bradh352)
1 parent 9feea5d commit 3b10e57

48 files changed

Lines changed: 794 additions & 666 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (C) The c-ares project and its contributors
22
# SPDX-License-Identifier: MIT
3-
CMAKE_MINIMUM_REQUIRED (VERSION 3.4.3)
3+
CMAKE_MINIMUM_REQUIRED (VERSION 3.5.0)
44

55
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
66

src/lib/ares__addrinfo2hostent.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family,
6161
struct ares_addrinfo_cname *next_cname;
6262
char **aliases = NULL;
6363
char *addrs = NULL;
64-
int naliases = 0, naddrs = 0, alias = 0, i;
64+
size_t naliases = 0, naddrs = 0, alias = 0, i;
6565

6666
if (ai == NULL || host == NULL)
6767
return ARES_EBADQUERY;
@@ -153,7 +153,7 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family,
153153

154154
if (naddrs)
155155
{
156-
addrs = ares_malloc(naddrs * (*host)->h_length);
156+
addrs = ares_malloc(naddrs * (size_t)(*host)->h_length);
157157
if (!addrs)
158158
{
159159
goto enomem;
@@ -165,18 +165,18 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family,
165165
{
166166
if(next->ai_family == family)
167167
{
168-
(*host)->h_addr_list[i] = addrs + (i * (*host)->h_length);
168+
(*host)->h_addr_list[i] = addrs + (i * (size_t)(*host)->h_length);
169169
if (family == AF_INET6)
170170
{
171171
memcpy((*host)->h_addr_list[i],
172172
&(CARES_INADDR_CAST(struct sockaddr_in6 *, next->ai_addr)->sin6_addr),
173-
(*host)->h_length);
173+
(size_t)(*host)->h_length);
174174
}
175175
else
176176
{
177177
memcpy((*host)->h_addr_list[i],
178178
&(CARES_INADDR_CAST(struct sockaddr_in *, next->ai_addr)->sin_addr),
179-
(*host)->h_length);
179+
(size_t)(*host)->h_length);
180180
}
181181
++i;
182182
}
@@ -206,10 +206,10 @@ ares_status_t ares__addrinfo2hostent(const struct ares_addrinfo *ai, int family,
206206

207207

208208
ares_status_t ares__addrinfo2addrttl(const struct ares_addrinfo *ai, int family,
209-
int req_naddrttls,
209+
size_t req_naddrttls,
210210
struct ares_addrttl *addrttls,
211211
struct ares_addr6ttl *addr6ttls,
212-
int *naddrttls)
212+
size_t *naddrttls)
213213
{
214214
struct ares_addrinfo_node *next;
215215
struct ares_addrinfo_cname *next_cname;

src/lib/ares__addrinfo_localhost.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
ares_status_t ares_append_ai_node(int aftype,
5151
unsigned short port,
52-
int ttl,
52+
unsigned int ttl,
5353
const void *adata,
5454
struct ares_addrinfo_node **nodes)
5555
{
@@ -80,7 +80,7 @@ ares_status_t ares_append_ai_node(int aftype,
8080
node->ai_family = AF_INET;
8181
node->ai_addrlen = sizeof(*sin);
8282
node->ai_addr = (struct sockaddr *)sin;
83-
node->ai_ttl = ttl;
83+
node->ai_ttl = (int)ttl;
8484
}
8585

8686
if (aftype == AF_INET6)
@@ -100,7 +100,7 @@ ares_status_t ares_append_ai_node(int aftype,
100100
node->ai_family = AF_INET6;
101101
node->ai_addrlen = sizeof(*sin6);
102102
node->ai_addr = (struct sockaddr *)sin6;
103-
node->ai_ttl = ttl;
103+
node->ai_ttl = (int)ttl;
104104
}
105105

106106
return ARES_SUCCESS;

src/lib/ares__close_sockets.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void ares__check_cleanup_conn(ares_channel channel, ares_socket_t fd)
7474
{
7575
ares__llist_node_t *node;
7676
struct server_connection *conn;
77-
int do_cleanup = 0;
77+
ares_bool_t do_cleanup = ARES_FALSE;
7878

7979
node = ares__htable_asvp_get_direct(channel->connnode_by_socket, fd);
8080
if (node == NULL) {
@@ -89,13 +89,13 @@ void ares__check_cleanup_conn(ares_channel channel, ares_socket_t fd)
8989

9090
/* If we are configured not to stay open, close it out */
9191
if (!(channel->flags & ARES_FLAG_STAYOPEN)) {
92-
do_cleanup = 1;
92+
do_cleanup = ARES_TRUE;
9393
}
9494

9595
/* If the udp connection hit its max queries, always close it */
9696
if (!conn->is_tcp && channel->udp_max_queries > 0 &&
97-
conn->total_queries >= (size_t)channel->udp_max_queries) {
98-
do_cleanup = 1;
97+
conn->total_queries >= channel->udp_max_queries) {
98+
do_cleanup = ARES_TRUE;
9999
}
100100

101101
if (do_cleanup) {

src/lib/ares__parse_into_addrinfo.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@
5050
#include "ares_private.h"
5151

5252
ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
53-
int alen,
53+
size_t alen,
5454
ares_bool_t cname_only_is_enodata,
5555
unsigned short port,
5656
struct ares_addrinfo *ai)
5757
{
58-
unsigned int qdcount, ancount;
58+
size_t qdcount, ancount;
5959
ares_status_t status;
60-
int i, rr_type, rr_class, rr_len, rr_ttl;
60+
size_t i;
61+
int rr_type, rr_class;
62+
size_t rr_len;
63+
unsigned int rr_ttl;
6164
ares_bool_t got_a = ARES_FALSE, got_aaaa = ARES_FALSE, got_cname = ARES_FALSE;
62-
long len;
65+
size_t len;
6366
const unsigned char *aptr;
6467
char *question_hostname = NULL;
6568
char *hostname, *rr_name = NULL, *rr_data;
@@ -93,7 +96,7 @@ ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
9396
aptr += len + QFIXEDSZ;
9497

9598
/* Examine each answer resource record (RR) in turn. */
96-
for (i = 0; i < (int)ancount; i++)
99+
for (i = 0; i < ancount; i++)
97100
{
98101
/* Decode the RR up to the data field. */
99102
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len, 0);
@@ -172,7 +175,7 @@ ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
172175
ares_free(rr_data);
173176
goto failed_stat;
174177
}
175-
cname->ttl = rr_ttl;
178+
cname->ttl = (int)rr_ttl;
176179
cname->alias = rr_name;
177180
cname->name = rr_data;
178181
rr_name = NULL;

src/lib/ares__readaddrinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ares_status_t ares__readaddrinfo(FILE *fp,
5252
char *line = NULL, *p, *q;
5353
char *txtaddr, *txthost, *txtalias;
5454
char *aliases[MAX_ALIASES];
55-
unsigned int i, alias_count;
55+
size_t i, alias_count;
5656
ares_status_t status = ARES_SUCCESS;
5757
size_t linesize;
5858
struct ares_addrinfo_cname *cname = NULL, *cnames = NULL;

src/lib/ares__sortaddrinfo.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
struct addrinfo_sort_elem
5858
{
5959
struct ares_addrinfo_node *ai;
60-
int has_src_addr;
60+
ares_bool_t has_src_addr;
6161
ares_sockaddr src_addr;
62-
int original_order;
62+
size_t original_order;
6363
};
6464

6565
#define ARES_IPV6_ADDR_MC_SCOPE(a) ((a)->s6_addr[1] & 0x0f)
@@ -255,15 +255,16 @@ static int get_precedence(const struct sockaddr *addr)
255255
/*
256256
* Find number of matching initial bits between the two addresses a1 and a2.
257257
*/
258-
static int common_prefix_len(const struct in6_addr *a1,
258+
static size_t common_prefix_len(const struct in6_addr *a1,
259259
const struct in6_addr *a2)
260260
{
261-
const char *p1 = (const char *)a1;
262-
const char *p2 = (const char *)a2;
263-
unsigned i;
261+
const unsigned char *p1 = (const unsigned char *)a1;
262+
const unsigned char *p2 = (const unsigned char *)a2;
263+
size_t i;
264264
for (i = 0; i < sizeof(*a1); ++i)
265265
{
266-
int x, j;
266+
unsigned char x;
267+
size_t j;
267268
if (p1[i] == p2[i])
268269
{
269270
continue;
@@ -294,12 +295,12 @@ static int rfc6724_compare(const void *ptr1, const void *ptr2)
294295
int label_src1, label_dst1, label_match1;
295296
int label_src2, label_dst2, label_match2;
296297
int precedence1, precedence2;
297-
int prefixlen1, prefixlen2;
298+
size_t prefixlen1, prefixlen2;
298299

299300
/* Rule 1: Avoid unusable destinations. */
300301
if (a1->has_src_addr != a2->has_src_addr)
301302
{
302-
return a2->has_src_addr - a1->has_src_addr;
303+
return ((int)a2->has_src_addr) - ((int)a1->has_src_addr);
303304
}
304305

305306
/* Rule 2: Prefer matching scope. */
@@ -372,22 +373,22 @@ static int rfc6724_compare(const void *ptr1, const void *ptr2)
372373
prefixlen2 = common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
373374
if (prefixlen1 != prefixlen2)
374375
{
375-
return prefixlen2 - prefixlen1;
376+
return (int)prefixlen2 - (int)prefixlen1;
376377
}
377378
}
378379

379380
/*
380381
* Rule 10: Leave the order unchanged.
381382
* We need this since qsort() is not necessarily stable.
382383
*/
383-
return a1->original_order - a2->original_order;
384+
return ((int)a1->original_order) - ((int)a2->original_order);
384385
}
385386

386387
/*
387388
* Find the source address that will be used if trying to connect to the given
388389
* address.
389390
*
390-
* Returns 1 if a source address was found, 0 if the address is unreachable,
391+
* Returns 1 if a source address was found, 0 if the address is unreachable
391392
* and -1 if a fatal error occurred. If 0 or 1, the contents of src_addr are
392393
* undefined.
393394
*/
@@ -454,7 +455,7 @@ ares_status_t ares__sortaddrinfo(ares_channel channel,
454455
struct ares_addrinfo_node *list_sentinel)
455456
{
456457
struct ares_addrinfo_node *cur;
457-
int nelem = 0, i;
458+
size_t nelem = 0, i;
458459
int has_src_addr;
459460
struct addrinfo_sort_elem *elems;
460461

@@ -490,7 +491,7 @@ ares_status_t ares__sortaddrinfo(ares_channel channel,
490491
ares_free(elems);
491492
return ARES_ENOTFOUND;
492493
}
493-
elems[i].has_src_addr = has_src_addr;
494+
elems[i].has_src_addr = (has_src_addr == 1)?ARES_TRUE:ARES_FALSE;
494495
}
495496

496497
/* Sort the addresses, and rearrange the linked list so it matches the sorted

src/lib/ares__timeval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct timeval ares__tvnow(void)
5959
struct timespec tsnow;
6060
if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
6161
now.tv_sec = tsnow.tv_sec;
62-
now.tv_usec = tsnow.tv_nsec / 1000;
62+
now.tv_usec = (int)(tsnow.tv_nsec / 1000);
6363
}
6464
/*
6565
** Even when the configure process has truly detected monotonic clock

src/lib/ares_create_query.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ int ares_create_query(const char *name, int dnsclass, int type,
163163
{
164164
if (*p == '\\' && *(p + 1) != 0)
165165
p++;
166-
*q++ = *p;
166+
*q++ = (unsigned char)*p;
167167
}
168168

169169
/* Go to the next label and repeat, unless we hit the end. */
@@ -188,7 +188,7 @@ int ares_create_query(const char *name, int dnsclass, int type,
188188
DNS_RR_SET_CLASS(q, max_udp_size);
189189
q += (EDNSFIXEDSZ-1);
190190
}
191-
buflen = (q - buf);
191+
buflen = (size_t)(q - buf);
192192

193193
/* Reject names that are longer than the maximum of 255 bytes that's
194194
* specified in RFC 1035 ("To simplify implementations, the total length of

src/lib/ares_destroy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void ares_destroy_options(struct ares_options *options)
5454

5555
void ares_destroy(ares_channel channel)
5656
{
57-
int i;
57+
size_t i;
5858
ares__llist_node_t *node = NULL;
5959

6060
if (!channel)
@@ -121,7 +121,7 @@ void ares_destroy(ares_channel channel)
121121
void ares__destroy_servers_state(ares_channel channel)
122122
{
123123
struct server_state *server;
124-
int i;
124+
size_t i;
125125

126126
if (channel->servers)
127127
{
@@ -136,5 +136,5 @@ void ares__destroy_servers_state(ares_channel channel)
136136
ares_free(channel->servers);
137137
channel->servers = NULL;
138138
}
139-
channel->nservers = -1;
139+
channel->nservers = 0;
140140
}

0 commit comments

Comments
 (0)