Use struct sockaddr in several places; distinguish various places where
[adns] / src / query.c
index 1d4dc5f..41aaf5f 100644 (file)
@@ -126,7 +126,7 @@ int adns_submit(adns_state ads,
   adns__vbuf_init(&vb);
 
   ol= strlen(owner);
-  if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; }
+  if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; }
                                 
   if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
 
@@ -164,10 +164,7 @@ static void *alloc_common(adns_query qu, size_t sz) {
   if (!sz) return qu; /* Any old pointer will do */
   assert(!qu->final_allocspace);
   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
-  if (!an) {
-    adns__query_fail(qu,adns_s_nolocalmem);
-    return 0;
-  }
+  if (!an) return 0;
   an->next= qu->allocations;
   qu->allocations= an;
   return (byte*)an + MEM_ROUND(sizeof(*an));
@@ -200,7 +197,7 @@ void *adns__alloc_final(adns_query qu, size_t sz) {
 }
 
 void adns__reset_cnameonly(adns_query qu) {
-  assert(qu->final_allocspace);
+  assert(!qu->final_allocspace);
   qu->answer->nrrs= 0;
   qu->answer->rrs= 0;
   qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
@@ -211,10 +208,19 @@ void adns__query_done(adns_query qu) {
   allocnode *an, *ann;
   int i;
 
-  qu->answer= ans= realloc(qu->answer,
-                          MEM_ROUND(MEM_ROUND(sizeof(*ans)) +
-                                    qu->interim_allocd));
-  qu->final_allocspace= (byte*)qu->answer + MEM_ROUND(sizeof(*ans));
+  if (qu->answer->status == adns_s_nolocalmem && !qu->interim_allocd) {
+    ans= qu->answer;
+  } else {
+    ans= realloc(qu->answer,
+                MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
+    if (!ans) {
+      qu->answer->cname= 0;
+      adns__query_fail(qu, adns_s_nolocalmem);
+      return;
+    }
+    qu->answer= ans;
+  }
+  qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
 
   adns__makefinal_str(qu,&ans->cname);
   if (ans->nrrs) {
@@ -242,6 +248,7 @@ void adns__makefinal_str(adns_query qu, char **strp) {
   char *before, *after;
 
   before= *strp;
+  if (!before) return;
   l= strlen(before)+1;
   after= adns__alloc_final(qu,l);
   memcpy(after,before,l);
@@ -249,10 +256,12 @@ void adns__makefinal_str(adns_query qu, char **strp) {
 }
 
 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
-  void *after;
+  void *before, *after;
 
+  before= *blpp;
+  if (!before) return;
   after= adns__alloc_final(qu,sz);
-  memcpy(after,*blpp,sz);
+  memcpy(after,before,sz);
   *blpp= after;
 }