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. 17
      src/lib/ares_fds.c
  3. 68
      src/lib/ares_process.c
  4. 12
      src/lib/ares_update_servers.c

@ -242,7 +242,7 @@ static ares_status_t ares_dns_write_rr_binstrs(ares__buf_t *buf,
/* String */
if (len) {
status = ares__buf_append(buf, (const unsigned char *)ptr, len);
status = ares__buf_append(buf, ptr, len);
if (status != ARES_SUCCESS) {
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)) {
const struct server_connection *conn = ares__llist_node_val(node);
/* We only need to register interest in UDP sockets if we have
* outstanding queries.
*/
if (active_queries || conn->is_tcp) {
FD_SET(conn->fd, read_fds);
if (conn->fd >= nfds) {
nfds = conn->fd + 1;
}
if (!active_queries && !conn->is_tcp)
continue;
/* Always wait on read */
FD_SET(conn->fd, read_fds);
if (conn->fd >= nfds) {
nfds = conn->fd + 1;
}
/* TCP only wait on write if we have buffered data */
if (conn->is_tcp && ares__buf_len(server->tcp_send)) {
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 &&
!(channel->flags & ARES_FLAG_IGNTC)) {
if (!query->using_tcp) {
query->using_tcp = ARES_TRUE;
ares__send_query(query, now);
}
query->using_tcp = ARES_TRUE;
ares__send_query(query, now);
status = ARES_SUCCESS; /* Switched to TCP is ok */
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_channel_t *channel = query->channel;
size_t max_tries = ares__slist_len(channel->servers) * channel->tries;
const ares_channel_t *channel = query->channel;
size_t max_tries = ares__slist_len(channel->servers) *
channel->tries;
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);
}
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_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
* 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;
}
}
timeplus = ares__retry_penalty(query);
/* Keep track of queries bucketed by timeout, so we can process
* timeout events quickly.

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

Loading…
Cancel
Save