Skip to content

Commit 95fe77c

Browse files
committed
Fix NULL pointer dereference in HTTPS response callback
Move NULL check before accessing req->tx_id. Include buflen in error message for debugging. Add missing return statement to prevent dereferencing NULL after logging.
1 parent 5b95a64 commit 95fe77c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/dns_poller.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,20 @@ static char *get_addr_listing(struct ares_addrinfo_node * nodes) {
5555
for (struct ares_addrinfo_node *node = nodes; node != NULL; node = node->ai_next) {
5656
const char *res = NULL;
5757

58+
// Check that we have space for at least one character plus null terminator
59+
if (pos >= list + POLLER_ADDR_LIST_SIZE - 1) {
60+
DLOG("Not enough space for more addresses");
61+
break;
62+
}
63+
size_t remaining = (size_t)(list + POLLER_ADDR_LIST_SIZE - 1 - pos);
64+
5865
if (node->ai_family == AF_INET) {
5966
res = ares_inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)node->ai_addr)->sin_addr,
60-
pos, (ares_socklen_t)(list + POLLER_ADDR_LIST_SIZE - 1 - pos));
67+
pos, (ares_socklen_t)remaining);
6168
ipv4++;
6269
} else if (node->ai_family == AF_INET6) {
6370
res = ares_inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)node->ai_addr)->sin6_addr,
64-
pos, (ares_socklen_t)(list + POLLER_ADDR_LIST_SIZE - 1 - pos));
71+
pos, (ares_socklen_t)remaining);
6572
ipv6++;
6673
} else {
6774
WLOG("Unhandled address family: %d", node->ai_family);
@@ -70,6 +77,9 @@ static char *get_addr_listing(struct ares_addrinfo_node * nodes) {
7077

7178
if (res != NULL) {
7279
pos += strlen(pos);
80+
// We already checked above that pos < list + POLLER_ADDR_LIST_SIZE - 1,
81+
// and ares_inet_ntop ensures null termination, so strlen(pos) >= 1.
82+
// Therefore pos++ is safe and there's room for the comma.
7383
*pos = ',';
7484
pos++;
7585
} else {

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static void https_resp_cb(void *data, char *buf, size_t buflen) {
9292
request_t *req = (request_t *)data;
9393
if (req == NULL) {
9494
FLOG("Request data is NULL (buflen: %zu)", buflen);
95+
return;
9596
}
9697
DLOG("Received response for id: %hX, len: %zu", req->tx_id, buflen);
9798
if (buf != NULL) { // May be NULL for timeout, DNS failure, or something similar.

0 commit comments

Comments
 (0)