Skip to content

Commit

Permalink
Merge pull request #1346 from Barenboim/master
Browse files Browse the repository at this point in the history
Avoid using 'sizeof' in macro.
  • Loading branch information
Barenboim authored Aug 11, 2023
2 parents cd03ed2 + e32c785 commit df6298f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/protocol/DnsMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -77,7 +76,6 @@ int DnsMessage::encode_reply()
size_t len;

msgbuf.clear();
msgbuf.reserve(DNS_HEADER_SIZE);
msgsize = 0;

// TODO encode other field
Expand Down
9 changes: 4 additions & 5 deletions src/protocol/dns_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/dns_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,6 +113,7 @@ struct dns_header
uint16_t nscount;
uint16_t arcount;
};
#pragma pack()

struct dns_question
{
Expand Down

0 comments on commit df6298f

Please sign in to comment.