From e32c7854bdd61c56e33c081aec6ab98f499c4f45 Mon Sep 17 00:00:00 2001 From: Xie Han <63350856@qq.com> Date: Fri, 11 Aug 2023 21:28:56 +0800 Subject: [PATCH] Avoid using 'sizeof' in macro. --- src/protocol/DnsMessage.cc | 2 -- src/protocol/dns_parser.c | 9 ++++----- src/protocol/dns_parser.h | 2 ++ 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/protocol/DnsMessage.cc b/src/protocol/DnsMessage.cc index 00c71ec67f..fd925ed3be 100644 --- a/src/protocol/DnsMessage.cc +++ b/src/protocol/DnsMessage.cc @@ -22,7 +22,6 @@ #define DNS_LABELS_MAX 63 #define DNS_MESSAGE_MAX_UDP_SIZE 512 -#define DNS_HEADER_SIZE sizeof (struct dns_header) namespace protocol { @@ -77,7 +76,6 @@ int DnsMessage::encode_reply() size_t len; msgbuf.clear(); - msgbuf.reserve(DNS_HEADER_SIZE); msgsize = 0; // TODO encode other field diff --git a/src/protocol/dns_parser.c b/src/protocol/dns_parser.c index 2e7bdc23b0..23e80991f2 100644 --- a/src/protocol/dns_parser.c +++ b/src/protocol/dns_parser.c @@ -24,7 +24,6 @@ #define DNS_LABELS_MAX 63 #define DNS_NAMES_MAX 256 #define DNS_MSGBASE_INIT_SIZE 514 // 512 + 2(leading length) -#define DNS_HEADER_SIZE sizeof (struct dns_header) #define MAX(x, y) ((x) <= (y) ? (y) : (x)) struct __dns_record_entry @@ -706,7 +705,7 @@ void dns_parser_init(dns_parser_t *parser) parser->bufsize = 0; parser->complete = 0; parser->single_packet = 0; - memset(&parser->header, 0, DNS_HEADER_SIZE); + memset(&parser->header, 0, sizeof (struct dns_header)); memset(&parser->question, 0, sizeof (struct dns_question)); INIT_LIST_HEAD(&parser->answer_list); INIT_LIST_HEAD(&parser->authority_list); @@ -769,16 +768,16 @@ int dns_parser_parse_all(dns_parser_t *parser) parser->cur = (const char *)parser->msgbase; h = &parser->header; - if (parser->msgsize < DNS_HEADER_SIZE) + if (parser->msgsize < sizeof (struct dns_header)) return -2; - memcpy(h, parser->msgbase, DNS_HEADER_SIZE); + memcpy(h, parser->msgbase, sizeof (struct dns_header)); h->id = ntohs(h->id); h->qdcount = ntohs(h->qdcount); h->ancount = ntohs(h->ancount); h->nscount = ntohs(h->nscount); h->arcount = ntohs(h->arcount); - parser->cur += DNS_HEADER_SIZE; + parser->cur += sizeof (struct dns_header); ret = __dns_parser_parse_question(parser); if (ret < 0) diff --git a/src/protocol/dns_parser.h b/src/protocol/dns_parser.h index 892af6c7a1..1aa01cd44a 100644 --- a/src/protocol/dns_parser.h +++ b/src/protocol/dns_parser.h @@ -83,6 +83,7 @@ enum * request or response packet, but the byte order is not * transformed. */ +#pragma pack(1) struct dns_header { uint16_t id; @@ -112,6 +113,7 @@ struct dns_header uint16_t nscount; uint16_t arcount; }; +#pragma pack() struct dns_question {