SonarCloud: fix some easy codesmells

pull/603/head
Brad House 1 year ago
parent 80fdc96f9a
commit 0a89b8c62f
  1. 2
      src/lib/ares_dns_write.c
  2. 11
      src/lib/ares_fds.c
  3. 64
      src/lib/ares_process.c
  4. 6
      src/lib/ares_update_servers.c

@ -242,7 +242,7 @@ static ares_status_t ares_dns_write_rr_binstrs(ares__buf_t *buf,
/* String */ /* String */
if (len) { if (len) {
status = ares__buf_append(buf, (const unsigned char *)ptr, len); status = ares__buf_append(buf, ptr, len);
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
return status; return status;
} }

@ -48,16 +48,17 @@ int ares_fds(ares_channel_t *channel, fd_set *read_fds, fd_set *write_fds)
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const struct server_connection *conn = ares__llist_node_val(node); const struct server_connection *conn = ares__llist_node_val(node);
/* We only need to register interest in UDP sockets if we have if (!active_queries && !conn->is_tcp)
* outstanding queries. continue;
*/
if (active_queries || conn->is_tcp) { /* Always wait on read */
FD_SET(conn->fd, read_fds); FD_SET(conn->fd, read_fds);
if (conn->fd >= nfds) { if (conn->fd >= nfds) {
nfds = conn->fd + 1; nfds = conn->fd + 1;
} }
}
/* TCP only wait on write if we have buffered data */
if (conn->is_tcp && ares__buf_len(server->tcp_send)) { if (conn->is_tcp && ares__buf_len(server->tcp_send)) {
FD_SET(conn->fd, write_fds); FD_SET(conn->fd, write_fds);
} }

@ -675,10 +675,8 @@ static ares_status_t process_answer(ares_channel_t *channel,
*/ */
if (ares_dns_record_get_flags(rdnsrec) & ARES_FLAG_TC && !tcp && if (ares_dns_record_get_flags(rdnsrec) & ARES_FLAG_TC && !tcp &&
!(channel->flags & ARES_FLAG_IGNTC)) { !(channel->flags & ARES_FLAG_IGNTC)) {
if (!query->using_tcp) {
query->using_tcp = ARES_TRUE; query->using_tcp = ARES_TRUE;
ares__send_query(query, now); ares__send_query(query, now);
}
status = ARES_SUCCESS; /* Switched to TCP is ok */ status = ARES_SUCCESS; /* Switched to TCP is ok */
goto cleanup; goto cleanup;
} }
@ -739,8 +737,9 @@ static void handle_conn_error(struct server_connection *conn,
ares_status_t ares__requeue_query(struct query *query, struct timeval *now) ares_status_t ares__requeue_query(struct query *query, struct timeval *now)
{ {
ares_channel_t *channel = query->channel; const ares_channel_t *channel = query->channel;
size_t max_tries = ares__slist_len(channel->servers) * channel->tries; size_t max_tries = ares__slist_len(channel->servers) *
channel->tries;
query->try_count++; query->try_count++;
@ -797,6 +796,39 @@ static ares_status_t ares__append_tcpbuf(struct server_state *server,
return ares__buf_append(server->tcp_send, query->qbuf, query->qlen); return ares__buf_append(server->tcp_send, query->qbuf, query->qlen);
} }
static size_t ares__retry_penalty(struct query *query)
{
const ares_channel_t *channel = query->channel;
size_t timeplus = channel->timeout;
size_t shift;
/* For each trip through the entire server list, double the channel's
* assigned timeout, avoiding overflow. If channel->timeout is negative,
* leave it as-is, even though that should be impossible here.
*/
/* How many times do we want to double it? Presume sane values here. */
shift = query->try_count / ares__slist_len(channel->servers);
/* Is there enough room to shift timeplus left that many times?
*
* To find out, confirm that all of the bits we'll shift away are zero.
* Stop considering a shift if we get to the point where we could shift
* a 1 into the sign bit (i.e. when shift is within two of the bit
* count).
*
* This has the side benefit of leaving negative numbers unchanged.
*/
if (shift <= (sizeof(int) * CHAR_BIT - 1) &&
(timeplus >> (sizeof(int) * CHAR_BIT - 1 - shift)) == 0) {
timeplus <<= shift;
}
return timeplus;
}
ares_status_t ares__send_query(struct query *query, struct timeval *now) ares_status_t ares__send_query(struct query *query, struct timeval *now)
{ {
ares_channel_t *channel = query->channel; ares_channel_t *channel = query->channel;
@ -920,29 +952,7 @@ ares_status_t ares__send_query(struct query *query, struct timeval *now)
} }
} }
/* For each trip through the entire server list, double the channel's timeplus = ares__retry_penalty(query);
* assigned timeout, avoiding overflow. If channel->timeout is negative,
* leave it as-is, even though that should be impossible here.
*/
timeplus = channel->timeout;
{
/* How many times do we want to double it? Presume sane values here. */
const size_t shift = query->try_count / ares__slist_len(channel->servers);
/* Is there enough room to shift timeplus left that many times?
*
* To find out, confirm that all of the bits we'll shift away are zero.
* Stop considering a shift if we get to the point where we could shift
* a 1 into the sign bit (i.e. when shift is within two of the bit
* count).
*
* This has the side benefit of leaving negative numbers unchanged.
*/
if (shift <= (sizeof(int) * CHAR_BIT - 1) &&
(timeplus >> (sizeof(int) * CHAR_BIT - 1 - shift)) == 0) {
timeplus <<= shift;
}
}
/* Keep track of queries bucketed by timeout, so we can process /* Keep track of queries bucketed by timeout, so we can process
* timeout events quickly. * timeout events quickly.

@ -382,7 +382,7 @@ static ares__slist_node_t *ares__server_find(ares_channel_t *channel,
return NULL; return NULL;
} }
static ares_bool_t ares__server_isdup(ares_channel_t *channel, static ares_bool_t ares__server_isdup(const ares_channel_t *channel,
ares__llist_node_t *s) ares__llist_node_t *s)
{ {
/* Scan backwards to see if this is a duplicate */ /* Scan backwards to see if this is a duplicate */
@ -471,11 +471,11 @@ done:
return status; return status;
} }
static ares_bool_t ares__server_in_newconfig(struct server_state *server, static ares_bool_t ares__server_in_newconfig(const struct server_state *server,
ares__llist_t *srvlist) ares__llist_t *srvlist)
{ {
ares__llist_node_t *node; ares__llist_node_t *node;
ares_channel_t *channel = server->channel; const ares_channel_t *channel = server->channel;
for (node = ares__llist_node_first(srvlist); node != NULL; for (node = ares__llist_node_first(srvlist); node != NULL;
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {

Loading…
Cancel
Save