From a00bcbc76151b0829b4d1fc529dba7a8e2f1bc4d Mon Sep 17 00:00:00 2001 From: Neel Goyal Date: Wed, 15 Jan 2014 16:25:46 -0500 Subject: [PATCH] Fix for issue #68. Implement getdns_dict_remove_name. --- src/context.c | 2 +- src/dict.c | 27 ++++++++++++++++++++------- src/general.c | 4 ++-- src/list.c | 46 +++++++++++++++++++++++----------------------- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/context.c b/src/context.c index e249dc49..d4112841 100644 --- a/src/context.c +++ b/src/context.c @@ -253,7 +253,7 @@ getdns_context_create_with_extended_memory_functions( mf_union mf; if (!context || !malloc || !realloc || !free) - return GETDNS_RETURN_GENERIC_ERROR; + return GETDNS_RETURN_INVALID_PARAMETER; /** default init **/ mf.ext.malloc = malloc; diff --git a/src/dict.c b/src/dict.c index c53ce6b6..d7039bd2 100644 --- a/src/dict.c +++ b/src/dict.c @@ -81,7 +81,7 @@ getdns_dict_get_names(struct getdns_dict * dict, struct getdns_list ** answer) struct getdns_bindata bindata; if (!dict || !answer) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_INVALID_PARAMETER; *answer = getdns_list_create_with_extended_memory_functions( dict->mf.mf_arg, dict->mf.mf.ext.malloc, @@ -125,7 +125,7 @@ getdns_dict_get_dict(struct getdns_dict * dict, char *name, struct getdns_dict_item *item; if (!dict || !name || !answer) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_INVALID_PARAMETER; item = getdns_dict_find(dict, name, 0); if (!item) @@ -146,7 +146,7 @@ getdns_dict_get_list(struct getdns_dict * dict, char *name, struct getdns_dict_item *item; if (!dict || !name || !answer) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_INVALID_PARAMETER; item = getdns_dict_find(dict, name, 0); if (!item) @@ -167,7 +167,7 @@ getdns_dict_get_bindata(struct getdns_dict * dict, char *name, struct getdns_dict_item *item; if (!dict || !name || !answer) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_INVALID_PARAMETER; item = getdns_dict_find(dict, name, 0); if (!item) @@ -187,7 +187,7 @@ getdns_dict_get_int(struct getdns_dict * dict, char *name, uint32_t * answer) struct getdns_dict_item *item; if (!dict || !name || !answer) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_INVALID_PARAMETER; item = getdns_dict_find(dict, name, 0); if (!item) @@ -281,7 +281,7 @@ getdns_dict_copy(struct getdns_dict * srcdict, struct getdns_dict ** dstdict) getdns_return_t retval; if (!dstdict) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_INVALID_PARAMETER; if (!srcdict) { *dstdict = NULL; @@ -293,7 +293,7 @@ getdns_dict_copy(struct getdns_dict * srcdict, struct getdns_dict ** dstdict) srcdict->mf.mf.ext.realloc, srcdict->mf.mf.ext.free); if (!*dstdict) - return GETDNS_RETURN_NO_SUCH_DICT_NAME; + return GETDNS_RETURN_GENERIC_ERROR; retval = GETDNS_RETURN_GOOD; LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(srcdict->root)) { @@ -730,6 +730,19 @@ getdns_pretty_print_dict(struct getdns_dict *dict) getdns_return_t getdns_dict_remove_name(struct getdns_dict *this_dict, char *name) { + struct getdns_dict_item *item; + + if (!this_dict || !name) + return GETDNS_RETURN_INVALID_PARAMETER; + + item = getdns_dict_find(this_dict, name, 0); + if (!item) + return GETDNS_RETURN_NO_SUCH_DICT_NAME; + + /* cleanup */ + ldns_rbtree_delete(&this_dict->root, name); + getdns_dict_item_free(&item->node, this_dict); + return GETDNS_RETURN_GENERIC_ERROR; } diff --git a/src/general.c b/src/general.c index 5ea272bb..c5c80b31 100644 --- a/src/general.c +++ b/src/general.c @@ -269,7 +269,7 @@ getdns_general_ub(struct ub_ctx *unbound, int r; if (!name) { - return GETDNS_RETURN_GENERIC_ERROR; + return GETDNS_RETURN_INVALID_PARAMETER; } gr = getdns_context_prepare_for_resolution(context); @@ -337,7 +337,7 @@ getdns_general(struct getdns_context *context, } /* ensure callback is not NULL */ - if (!callback) { + if (!callback || !name) { return GETDNS_RETURN_INVALID_PARAMETER; } diff --git a/src/list.c b/src/list.c index f71bf5c3..8653af17 100644 --- a/src/list.c +++ b/src/list.c @@ -55,12 +55,12 @@ getdns_return_t getdns_list_get_data_type(struct getdns_list * list, size_t index, getdns_data_type * answer) { - if (!list || index >= list->numinuse) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; - - if (!answer) + if (!list || !answer) return GETDNS_RETURN_INVALID_PARAMETER; + if (index >= list->numinuse) + return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + *answer = list->items[index].dtype; return GETDNS_RETURN_GOOD; } /* getdns_list_get_data_type */ @@ -70,12 +70,12 @@ getdns_return_t getdns_list_get_dict(struct getdns_list * list, size_t index, struct getdns_dict ** answer) { - if (!list || index >= list->numinuse) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; - - if (!answer) + if (!list || !answer) return GETDNS_RETURN_INVALID_PARAMETER; + if (index >= list->numinuse) + return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + if (list->items[index].dtype != t_dict) return GETDNS_RETURN_WRONG_TYPE_REQUESTED; @@ -88,13 +88,12 @@ getdns_return_t getdns_list_get_list(struct getdns_list * list, size_t index, struct getdns_list ** answer) { - - if (!list || index >= list->numinuse) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; - - if (!answer) + if (!list || !answer) return GETDNS_RETURN_INVALID_PARAMETER; + if (index >= list->numinuse) + return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + if (list->items[index].dtype != t_list) return GETDNS_RETURN_WRONG_TYPE_REQUESTED; @@ -107,12 +106,13 @@ getdns_return_t getdns_list_get_bindata(struct getdns_list * list, size_t index, struct getdns_bindata ** answer) { - if (!list || index >= list->numinuse) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if (!answer) + if (!list || !answer) return GETDNS_RETURN_INVALID_PARAMETER; + if (index >= list->numinuse) + return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + if (list->items[index].dtype != t_bindata) return GETDNS_RETURN_WRONG_TYPE_REQUESTED; @@ -124,12 +124,12 @@ getdns_list_get_bindata(struct getdns_list * list, size_t index, getdns_return_t getdns_list_get_int(struct getdns_list * list, size_t index, uint32_t * answer) { - if (!list || index >= list->numinuse) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; - - if (!answer) + if (!list || !answer) return GETDNS_RETURN_INVALID_PARAMETER; + if (index >= list->numinuse) + return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + if (list->items[index].dtype != t_int) return GETDNS_RETURN_WRONG_TYPE_REQUESTED; @@ -151,7 +151,7 @@ getdns_list_realloc(struct getdns_list *list) struct getdns_list_item *newlist; if (!list) - return GETDNS_RETURN_GENERIC_ERROR; + return GETDNS_RETURN_INVALID_PARAMETER; newlist = GETDNS_XREALLOC(list->mf, list->items, struct getdns_list_item, @@ -173,7 +173,7 @@ getdns_list_copy(struct getdns_list * srclist, struct getdns_list ** dstlist) getdns_return_t retval; if (!dstlist) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + return GETDNS_RETURN_INVALID_PARAMETER; if (!srclist) { *dstlist = NULL; @@ -186,7 +186,7 @@ getdns_list_copy(struct getdns_list * srclist, struct getdns_list ** dstlist) srclist->mf.mf.ext.free ); if (!dstlist) - return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + return GETDNS_RETURN_GENERIC_ERROR; for (i = 0; i < srclist->numinuse; i++) { retval = getdns_list_add_item(*dstlist, &index);