dns-packet: add asserts for allocation overflow safety

Coverity flags ALIGN(sizeof(DnsPacket)) + size calculations in
dns_packet_new() and dns_packet_dup() as potential overflows. The
sizes are bounded by DNS_PACKET_SIZE_MAX but add asserts to make
this explicit for static analyzers.

CID#1548058
CID#1548076

Follow-up for c73ce96b56
This commit is contained in:
Luca Boccassi
2026-03-30 09:37:31 +01:00
parent 09bb6448ae
commit 21cc3216a7
+4
View File
@@ -120,6 +120,8 @@ int dns_packet_new(
a = min_alloc_dsize;
/* round up to next page size */
/* Silence static analyzers */
assert(a <= SIZE_MAX - ALIGN(sizeof(DnsPacket)));
a = PAGE_ALIGN(ALIGN(sizeof(DnsPacket)) + a) - ALIGN(sizeof(DnsPacket));
/* make sure we never allocate more than useful */
@@ -226,6 +228,8 @@ int dns_packet_dup(DnsPacket **ret, DnsPacket *p) {
if (r < 0)
return r;
/* Silence static analyzers */
assert(p->size <= SIZE_MAX - ALIGN(sizeof(DnsPacket)));
c = malloc(ALIGN(sizeof(DnsPacket)) + p->size);
if (!c)
return -ENOMEM;