Skip to content

Commit

Permalink
sdns.c - Fix memory leaks in some code execution paths that were caug…
Browse files Browse the repository at this point in the history
…ht by valgrind
  • Loading branch information
chmorgan committed Aug 31, 2017
1 parent 7c8f7d8 commit 0659508
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ void resolver_process(struct Message* msg)
while(q)
{
rr = malloc(sizeof(struct ResourceRecord));
memset(rr, 0, sizeof(struct ResourceRecord));

rr->name = strdup(q->qName);
rr->type = q->qType;
Expand All @@ -556,13 +557,21 @@ void resolver_process(struct Message* msg)
rr->rd_length = 4;
rc = get_A_Record(rr->rd_data.a_record.addr, q->qName);
if(rc < 0)
{
free(rr->name);
free(rr);
goto next;
}
break;
case AAAA_Resource_RecordType:
rr->rd_length = 16;
rc = get_AAAA_Record(rr->rd_data.aaaa_record.addr, q->qName);
if(rc < 0)
{
free(rr->name);
free(rr);
goto next;
}
break;
/*
case NS_Resource_RecordType:
Expand All @@ -573,6 +582,7 @@ void resolver_process(struct Message* msg)
case TXT_Resource_RecordType:
*/
default:
free(rr);
msg->rcode = NotImplemented_ResponseType;
printf("Cannot answer question of type %d.\n", q->qType);
goto next;
Expand Down Expand Up @@ -688,6 +698,7 @@ int main()
int port = 9000;

struct Message msg;
memset(&msg, 0, sizeof(struct Message));

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
Expand All @@ -707,12 +718,11 @@ int main()

while(1)
{
memset(&msg, 0, sizeof(struct Message));

free_questions(msg.questions);
free_resource_records(msg.answers);
free_resource_records(msg.authorities);
free_resource_records(msg.additionals);
memset(&msg, 0, sizeof(struct Message));

nbytes = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *) &client_addr, &addr_len);
Expand Down

0 comments on commit 0659508

Please sign in to comment.