Fix a bug in list copy for bindata. Fix some memory errors

This commit is contained in:
Neel Goyal 2013-10-17 20:34:18 -04:00
parent dfa5fc82c8
commit 840939aac8
4 changed files with 37 additions and 21 deletions

View File

@ -37,12 +37,12 @@ main()
{ {
getdns_return_t context_create_return; getdns_return_t context_create_return;
struct getdns_list *just_the_addresses_ptr; struct getdns_list *just_the_addresses_ptr;
size_t *num_addresses_ptr = NULL; size_t num_addresses = 0;
size_t rec_count; size_t rec_count;
struct getdns_dict *this_address; struct getdns_dict *this_address;
struct getdns_bindata *this_address_data; struct getdns_bindata *this_address_data;
struct getdns_context_t *this_context = NULL; struct getdns_context_t *this_context = NULL;
uint32_t *this_error = NULL; uint32_t this_error = 0;
struct getdns_dict *this_extensions = NULL; struct getdns_dict *this_extensions = NULL;
const char *this_name = "www.example.com"; const char *this_name = "www.example.com";
uint8_t this_request_type = GETDNS_RRTYPE_A; uint8_t this_request_type = GETDNS_RRTYPE_A;
@ -79,22 +79,21 @@ main()
else else
{ {
/* Be sure the search returned something */ /* Be sure the search returned something */
this_ret = getdns_dict_get_int(this_response, "status", this_error); // Ignore any error this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error
if (this_error && (*this_error != GETDNS_RESPSTATUS_GOOD)) // If the search didn't return "good" if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good"
{ {
fprintf(stderr, "The search had no results, and a return value of %d. Exiting.", *this_error); fprintf(stderr, "The search had no results, and a return value of %d. Exiting.", this_error);
return(GETDNS_RETURN_GENERIC_ERROR); return(GETDNS_RETURN_GENERIC_ERROR);
} }
this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error
this_ret = getdns_list_get_length(just_the_addresses_ptr, num_addresses_ptr); // Ignore any error this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error
/* Go through each record */ /* Go through each record */
if (num_addresses_ptr) { if (num_addresses > 0) {
for (rec_count = 0; rec_count <= *num_addresses_ptr; ++rec_count ) for (rec_count = 0; rec_count < num_addresses; ++rec_count )
{ {
this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error
/* Just print the address */ /* Just print the address */
this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data); // Ignore any error printf("The address is %s\n", getdns_display_ip_address(this_address_data));
printf("The address is %s", getdns_display_ip_address(this_address_data));
} }
} }
} }

View File

@ -208,8 +208,8 @@ getdns_list_copy(struct getdns_list *srclist, struct getdns_list **dstlist)
(*dstlist)->items[i].data.bindata->data = (uint8_t *) (*dstlist)->items[i].data.bindata->data = (uint8_t *)
malloc(srclist->items[i].data.bindata->size); malloc(srclist->items[i].data.bindata->size);
if((*dstlist)->items[i].data.bindata->data != NULL) if((*dstlist)->items[i].data.bindata->data != NULL)
memcpy(srclist->items[i].data.bindata->data memcpy((*dstlist)->items[i].data.bindata->data,
, (*dstlist)->items[i].data.bindata->data srclist->items[i].data.bindata->data
, srclist->items[i].data.bindata->size); , srclist->items[i].data.bindata->size);
else else
retval = GETDNS_RETURN_GENERIC_ERROR; retval = GETDNS_RETURN_GENERIC_ERROR;

View File

@ -33,6 +33,7 @@
#include <unbound-event.h> #include <unbound-event.h>
#include "context.h" #include "context.h"
#include "general.h" #include "general.h"
#include <string.h>
/* stuff to make it compile pedantically */ /* stuff to make it compile pedantically */
#define UNUSED_PARAM(x) ((void)(x)) #define UNUSED_PARAM(x) ((void)(x))
@ -40,7 +41,7 @@
/* struct used for the request */ /* struct used for the request */
typedef struct sync_request_data { typedef struct sync_request_data {
getdns_context_t context; getdns_context_t context;
const char* name; char* name;
uint16_t request_type; uint16_t request_type;
getdns_dict *extensions; getdns_dict *extensions;
getdns_return_t response_status; getdns_return_t response_status;
@ -85,11 +86,12 @@ getdns_general_sync(
{ {
/* we will cheat and spawn a thread */ /* we will cheat and spawn a thread */
/* set up for sync resolution */ /* set up for sync resolution */
pthread_t thread; pthread_t thread;
pthread_attr_t attr; pthread_attr_t attr;
sync_request_data req_data = { sync_request_data req_data = {
context, name, request_type, context,
strdup(name),
request_type,
extensions, extensions,
GETDNS_RETURN_GOOD, GETDNS_RETURN_GOOD,
response response
@ -98,11 +100,13 @@ getdns_general_sync(
/* create the thread */ /* create the thread */
int ret = pthread_attr_init(&attr); int ret = pthread_attr_init(&attr);
if (ret != 0) { if (ret != 0) {
free(req_data.name);
return GETDNS_RETURN_GENERIC_ERROR; return GETDNS_RETURN_GENERIC_ERROR;
} }
ret = pthread_create(&thread, &attr, request_thread_start, &req_data); ret = pthread_create(&thread, &attr, request_thread_start, &req_data);
if (ret != 0) { if (ret != 0) {
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
free(req_data.name);
return GETDNS_RETURN_GENERIC_ERROR; return GETDNS_RETURN_GENERIC_ERROR;
} }
/* wait for the thread */ /* wait for the thread */
@ -110,9 +114,10 @@ getdns_general_sync(
/* delete attr */ /* delete attr */
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
if (ret != 0) { if (ret != 0) {
free(req_data.name);
return GETDNS_RETURN_GENERIC_ERROR; return GETDNS_RETURN_GENERIC_ERROR;
} }
free(req_data.name);
return req_data.response_status; return req_data.response_status;
} }

View File

@ -231,6 +231,7 @@ static getdns_dict *create_dict_from_rr(ldns_rr* rr) {
if (rd_count >= 1) { if (rd_count >= 1) {
getdns_dict* rdata = create_dict_from_rdf(ldns_rr_rdf(rr, 0)); getdns_dict* rdata = create_dict_from_rdf(ldns_rr_rdf(rr, 0));
r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_RDATA, rdata); r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_RDATA, rdata);
getdns_dict_destroy(rdata);
} }
/* TODO - if more than one, is rdata a list? */ /* TODO - if more than one, is rdata a list? */
@ -251,8 +252,10 @@ static getdns_list *create_list_from_rr_list(ldns_rr_list* rr_list) {
getdns_list *result = getdns_list_create(); getdns_list *result = getdns_list_create();
for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) { for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) {
ldns_rr *rr = ldns_rr_list_rr(rr_list, i); ldns_rr *rr = ldns_rr_list_rr(rr_list, i);
getdns_dict* rrdict = create_dict_from_rr(rr);
r |= getdns_list_add_item(result, &idx); r |= getdns_list_add_item(result, &idx);
r |= getdns_list_set_dict(result, idx, create_dict_from_rr(rr)); r |= getdns_list_set_dict(result, idx, rrdict);
getdns_dict_destroy(rrdict);
} }
if (r != 0) { if (r != 0) {
getdns_list_destroy(result); getdns_list_destroy(result);
@ -272,9 +275,12 @@ static getdns_return_t add_only_addresses(getdns_list* addrs, ldns_rr_list* rr_l
for (j = 0; j < rd_count; ++j) { for (j = 0; j < rd_count; ++j) {
size_t item_idx = 0; size_t item_idx = 0;
ldns_rdf* rdf = ldns_rr_rdf(rr, j); ldns_rdf* rdf = ldns_rr_rdf(rr, j);
getdns_bindata rbin = { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A ||
r |= getdns_list_add_item(addrs, &item_idx); ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_AAAA) {
r |= getdns_list_set_bindata(addrs, item_idx, &rbin); getdns_bindata rbin = { ldns_rdf_size(rdf), ldns_rdf_data(rdf) };
r |= getdns_list_add_item(addrs, &item_idx);
r |= getdns_list_set_bindata(addrs, item_idx, &rbin);
}
} }
} }
return r; return r;
@ -424,6 +430,7 @@ getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request) {
getdns_dict *reply = create_reply_dict(netreq, just_addrs); getdns_dict *reply = create_reply_dict(netreq, just_addrs);
r |= getdns_list_add_item(replies_tree, &idx); r |= getdns_list_add_item(replies_tree, &idx);
r |= getdns_list_set_dict(replies_tree, idx, reply); r |= getdns_list_set_dict(replies_tree, idx, reply);
getdns_dict_destroy(reply);
/* buffer */ /* buffer */
if (s == LDNS_STATUS_OK) { if (s == LDNS_STATUS_OK) {
r |= getdns_list_add_item(replies_full, &idx); r |= getdns_list_add_item(replies_full, &idx);
@ -441,6 +448,11 @@ getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request) {
if (just_addrs) { if (just_addrs) {
r |= getdns_dict_set_list(result, GETDNS_STR_KEY_JUST_ADDRS, just_addrs); r |= getdns_dict_set_list(result, GETDNS_STR_KEY_JUST_ADDRS, just_addrs);
} }
/* cleanup */
getdns_list_destroy(replies_tree);
getdns_list_destroy(replies_full);
getdns_list_destroy(just_addrs);
if (r != 0) { if (r != 0) {
getdns_dict_destroy(result); getdns_dict_destroy(result);