From 0bc2eadecd3209ea3af23f255c9a274cbff80f0e Mon Sep 17 00:00:00 2001 From: Brad House Date: Sun, 12 Nov 2023 12:33:17 -0500 Subject: [PATCH] SonarCloud: fix some minor codesmells --- src/lib/ares__buf.c | 84 ++++++++++++++++++++--------------- src/lib/ares_dns_write.c | 7 ++- src/lib/ares_update_servers.c | 4 +- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/lib/ares__buf.c b/src/lib/ares__buf.c index a54b3988..2cc5d802 100644 --- a/src/lib/ares__buf.c +++ b/src/lib/ares__buf.c @@ -887,61 +887,73 @@ static ares_status_t ares__buf_append_str(ares__buf_t *buf, const char *str) return ares__buf_append(buf, (const unsigned char *)str, ares_strlen(str)); } -ares_status_t ares__buf_hexdump(ares__buf_t *buf, const unsigned char *data, - size_t len) +static ares_status_t ares__buf_hexdump_line(ares__buf_t *buf, + size_t idx, + const unsigned char *data, + size_t len) { size_t i; ares_status_t status; - /* Each line is 16 bytes */ - for (i = 0; i < len; i += 16) { - size_t j; - /* Address */ - status = ares__buf_append_num_hex(buf, i, 6); + /* Address */ + status = ares__buf_append_num_hex(buf, idx, 6); + if (status != ARES_SUCCESS) { + return status; + } + + /* | */ + status = ares__buf_append_str(buf, " | "); + if (status != ARES_SUCCESS) { + return status; + } + + for (i = 0; i < 16; i++) { + if (i >= len) { + status = ares__buf_append_str(buf, " "); + } else { + status = ares__buf_append_num_hex(buf, data[i], 2); + } if (status != ARES_SUCCESS) { return status; } - /* | */ - status = ares__buf_append_str(buf, " | "); + status = ares__buf_append_byte(buf, ' '); if (status != ARES_SUCCESS) { return status; } + } - for (j = i; j < i + 16; j++) { - if (j >= len) { - status = ares__buf_append_str(buf, " "); - } else { - status = ares__buf_append_num_hex(buf, data[j], 2); - } - if (status != ARES_SUCCESS) { - return status; - } + /* | */ + status = ares__buf_append_str(buf, " | "); + if (status != ARES_SUCCESS) { + return status; + } - status = ares__buf_append_byte(buf, ' '); - if (status != ARES_SUCCESS) { - return status; - } + for (i = 0; i < 16; i++) { + if (i >= len) { + break; } - - /* | */ - status = ares__buf_append_str(buf, " | "); + status = + ares__buf_append_byte(buf, ares__isprint(data[i]) ? data[i] : '.'); if (status != ARES_SUCCESS) { return status; } + } - for (j = i; j < i + 16; j++) { - if (j >= len) { - break; - } - status = - ares__buf_append_byte(buf, ares__isprint(data[j]) ? data[j] : '.'); - if (status != ARES_SUCCESS) { - return status; - } - } + return ares__buf_append_byte(buf, '\n'); +} - ares__buf_append_byte(buf, '\n'); +ares_status_t ares__buf_hexdump(ares__buf_t *buf, const unsigned char *data, + size_t len) +{ + size_t i; + + /* Each line is 16 bytes */ + for (i = 0; i < len; i += 16) { + ares_status_t status; + status = ares__buf_hexdump_line(buf, i, data + i, len - i); + if (status != ARES_SUCCESS) + return status; } return ARES_SUCCESS; diff --git a/src/lib/ares_dns_write.c b/src/lib/ares_dns_write.c index 0b4fefa8..e199a735 100644 --- a/src/lib/ares_dns_write.c +++ b/src/lib/ares_dns_write.c @@ -192,7 +192,12 @@ static ares_status_t ares_dns_write_rr_str(ares__buf_t *buf, ares_status_t status; str = ares_dns_rr_get_str(rr, key); - if (str == NULL || (len = ares_strlen(str)) > 255) { + if (str == NULL) { + return ARES_EFORMERR; + } + + len = ares_strlen(str); + if (len > 255) { return ARES_EFORMERR; } diff --git a/src/lib/ares_update_servers.c b/src/lib/ares_update_servers.c index c4e9be12..8de80219 100644 --- a/src/lib/ares_update_servers.c +++ b/src/lib/ares_update_servers.c @@ -505,8 +505,8 @@ static void ares__servers_remove_stale(ares_channel_t *channel, ares__slist_node_t *snode = ares__slist_node_first(channel->servers); while (snode != NULL) { - ares__slist_node_t *snext = ares__slist_node_next(snode); - struct server_state *server = ares__slist_node_val(snode); + ares__slist_node_t *snext = ares__slist_node_next(snode); + const struct server_state *server = ares__slist_node_val(snode); if (!ares__server_in_newconfig(server, srvlist)) { /* This will clean up all server state via the destruction callback and * move any queries to new servers */