Skip to content

Commit f06a7e4

Browse files
committed
Addressed copilot's comments
1 parent de894f5 commit f06a7e4

6 files changed

Lines changed: 52 additions & 7 deletions

File tree

proxy/dohproxyd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ static int tcp_connect(const char *host, const char *port)
508508
tv.tv_usec = 0;
509509

510510
for (rp = res; rp; rp = rp->ai_next) {
511+
if (!sockaddr_is_public(rp->ai_addr))
512+
continue;
511513
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
512514
if (fd < 0)
513515
continue;

src/dohd.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,17 @@ static void handle_pending_signals(int __attribute__((unused)) fd,
505505
}
506506
}
507507

508+
static int set_fd_nonblocking(int fd)
509+
{
510+
int flags = fcntl(fd, F_GETFL, 0);
511+
512+
if (flags < 0)
513+
return -1;
514+
if ((flags & O_NONBLOCK) != 0)
515+
return 0;
516+
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
517+
}
518+
508519

509520
static void dohd_destroy_client(struct client_data *cd)
510521
{
@@ -1793,8 +1804,11 @@ int main(int argc, char *argv[])
17931804
dohprint(DOH_ERR, "ERROR: failed to create signal pipe");
17941805
return -1;
17951806
}
1796-
fcntl(signal_pipe[0], F_SETFL, O_NONBLOCK);
1797-
fcntl(signal_pipe[1], F_SETFL, O_NONBLOCK);
1807+
if (set_fd_nonblocking(signal_pipe[0]) != 0 ||
1808+
set_fd_nonblocking(signal_pipe[1]) != 0) {
1809+
dohprint(DOH_ERR, "ERROR: failed to make signal pipe non-blocking");
1810+
return -1;
1811+
}
17981812
evquick_addevent(signal_pipe[0], EVQUICK_EV_READ, handle_pending_signals, NULL, NULL);
17991813

18001814
/* Initialize memory pools */

src/heap.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ static inline uint64_t heap_insert(struct heap_##type *heap, type *el)
3636
(heap->n + 1) * sizeof(struct heap_element_##type)); \
3737
if (!_tmp) { \
3838
heap->n--; \
39-
return -1; \
39+
return UINT64_MAX; \
4040
} \
4141
heap->top = _tmp; \
4242
heap->size++; \
4343
} \
44+
if (heap->last_id == UINT64_MAX) \
45+
heap->last_id = 0; \
4446
etmp.id = heap->last_id++; \
4547
if (heap->n == 1) { \
4648
memcpy(&heap->top[1], &etmp, sizeof(struct heap_element_##type)); \

src/url64.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
#include <inttypes.h>
22+
#include <limits.h>
2223
#include <stddef.h>
2324

2425
static const unsigned char asciitable[256] = {
@@ -43,11 +44,16 @@ static const unsigned char asciitable[256] = {
4344
// returns an estimation of the length of the data once decoded
4445
int dohd_url64_declen(int len) { return ((len + 3) >> 2) * 3; }
4546

47+
static size_t dohd_url64_declen_size(size_t len)
48+
{
49+
return ((len + 3U) >> 2) * 3U;
50+
}
51+
4652
int dohd_url64_check(const char *in, size_t in_len) {
4753
size_t c;
4854
const unsigned char *bufin;
4955

50-
if (!in)
56+
if (!in || in_len > (size_t)INT_MAX)
5157
return 0;
5258
bufin = (const unsigned char *)in;
5359
for (c = 0; c < in_len; c++) {
@@ -66,9 +72,11 @@ int dohd_url64_decode(const char *src, size_t src_len, uint8_t *dest, size_t des
6672

6773
if (!src || !dest || dest_cap == 0)
6874
return -1;
75+
if (src_len > (size_t)INT_MAX)
76+
return -1;
6977
if (dohd_url64_check(src, src_len) != (int)src_len)
7078
return -1;
71-
if ((size_t)dohd_url64_declen((int)src_len) + 1 > dest_cap)
79+
if (dohd_url64_declen_size(src_len) + 1 > dest_cap)
7280
return -1;
7381

7482
bufin = _buf;

test/test_dns_parser.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ static uint32_t dnsreply_min_age(const void *p, size_t len) {
141141
for (i = 0; i < total_rr; i++) {
142142
uint32_t ttl;
143143
uint16_t datalen;
144+
uint32_t ttl_net;
145+
uint16_t datalen_net;
144146
size_t remain;
145147

146148
if (dns_skip_rr_name(&record, end) < 0)
@@ -151,8 +153,10 @@ static uint32_t dnsreply_min_age(const void *p, size_t len) {
151153
return min_ttl;
152154

153155
/* TYPE (2) + CLASS (2) + TTL (4) + RDLENGTH (2) = 10 bytes */
154-
ttl = ntohl(*(uint32_t *)(record + 4));
155-
datalen = ntohs(*(uint16_t *)(record + 8));
156+
memcpy(&ttl_net, record + 4, sizeof(ttl_net));
157+
memcpy(&datalen_net, record + 8, sizeof(datalen_net));
158+
ttl = ntohl(ttl_net);
159+
datalen = ntohs(datalen_net);
156160

157161
if (remain < (size_t)(10 + datalen))
158162
return min_ttl;

test/test_heap.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,20 @@ static int test_heap_id_wrap(void) {
262262
return 1;
263263
}
264264

265+
static int test_heap_skips_error_sentinel(void) {
266+
heap_test_timer *h = heap_init();
267+
test_timer t = { .expire = 1, .value = 1 };
268+
uint64_t id;
269+
270+
h->last_id = UINT64_MAX;
271+
id = heap_insert(h, &t);
272+
TEST_ASSERT(id == 0, "heap_insert skips UINT64_MAX sentinel");
273+
TEST_ASSERT(h->last_id == 1, "heap_insert advances after sentinel wrap");
274+
275+
heap_destroy(h);
276+
return 1;
277+
}
278+
265279
int main(int argc, char **argv) {
266280
(void)argc;
267281
(void)argv;
@@ -279,6 +293,7 @@ int main(int argc, char **argv) {
279293
test_heap_stress();
280294
test_heap_growth();
281295
test_heap_id_wrap();
296+
test_heap_skips_error_sentinel();
282297

283298
fprintf(stderr, "\n=== Results: %d/%d tests passed ===\n", tests_passed, tests_run);
284299

0 commit comments

Comments
 (0)