Handle case when answer=NULL due to zero answers diff --git a/openbsd-compat/getrrsetbyname.c b/openbsd-compat/getrrsetbyname.c index dc6fe05..28622b5 100644 --- a/openbsd-compat/getrrsetbyname.c +++ b/openbsd-compat/getrrsetbyname.c @@ -268,7 +268,7 @@ getrrsetbyname(const char *hostname, unsigned int rdclass, } rrset->rri_rdclass = response->query->class; rrset->rri_rdtype = response->query->type; - rrset->rri_ttl = response->answer->ttl; + rrset->rri_ttl = response->answer ? response->answer->ttl : 0; rrset->rri_nrdatas = response->header.ancount; #ifdef HAVE_HEADER_AD @@ -276,6 +276,17 @@ getrrsetbyname(const char *hostname, unsigned int rdclass, if (response->header.ad == 1) rrset->rri_flags |= RRSET_VALIDATED; #endif + /* allocate memory for signatures */ + if (rrset->rri_nsigs > 0) { + rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo)); + if (rrset->rri_sigs == NULL) { + result = ERRSET_NOMEMORY; + goto fail; + } + } + + if (response->answer == NULL || response->header.ancount == 0) + goto done; /* copy name from answer section */ rrset->rri_name = strdup(response->answer->name); @@ -298,15 +309,6 @@ getrrsetbyname(const char *hostname, unsigned int rdclass, goto fail; } - /* allocate memory for signatures */ - if (rrset->rri_nsigs > 0) { - rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo)); - if (rrset->rri_sigs == NULL) { - result = ERRSET_NOMEMORY; - goto fail; - } - } - /* copy answers & signatures */ for (rr = response->answer, index_ans = 0, index_sig = 0; rr; rr = rr->next) { @@ -334,6 +336,7 @@ getrrsetbyname(const char *hostname, unsigned int rdclass, } free_dns_response(response); +done: *res = rrset; return (ERRSET_SUCCESS);