From 6917586acfb49d976b9a8a441aaef694a9e2d774 Mon Sep 17 00:00:00 2001 From: Keith Derrick Date: Thu, 12 Apr 2012 11:44:13 -0700 Subject: [PATCH] Add NULL-safe get object method New json_object_object_get_ex() method protects itself against null pointers and invalid objects being passed in. --- json_object.c | 23 +++++++++++++++++++++-- json_object.h | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/json_object.c b/json_object.c index ef54ecd..842ca22 100644 --- a/json_object.c +++ b/json_object.c @@ -3,6 +3,7 @@ * * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. * Michael Clark + * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See COPYING for details. @@ -24,11 +25,13 @@ #include "json_object.h" #include "json_object_private.h" #include "json_util.h" +#include "json_tokener.h" #if !HAVE_STRNDUP char* strndup(const char* str, size_t n); #endif /* !HAVE_STRNDUP */ +// Don't define this. It's not thread-safe. /* #define REFCOUNT_DEBUG 1 */ const char *json_number_chars = "0123456789.+-eE"; @@ -260,8 +263,24 @@ void json_object_object_add(struct json_object* jso, const char *key, struct json_object* json_object_object_get(struct json_object* jso, const char *key) { - if(!jso) return NULL; - return (struct json_object*) lh_table_lookup(jso->o.c_object, key); + struct json_object *result; + json_object_object_get_ex(jso, key, &result); + return result; +} + +json_bool json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value) +{ + if (NULL == jso) return FALSE; + + switch(jso->o_type) { + case json_type_object: + return lh_table_lookup_ex(jso->o.c_object, (void*)key, (void**)value); + default: + if (value != NULL) { + *value = NULL; + } + return FALSE; + } } void json_object_object_del(struct json_object* jso, const char *key) diff --git a/json_object.h b/json_object.h index 7b2c4ee..f7ec9ea 100644 --- a/json_object.h +++ b/json_object.h @@ -3,6 +3,7 @@ * * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. * Michael Clark + * Copyright (c) 2009 Hewlett-Packard Development Company, L.P. * * This library is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See COPYING for details. @@ -172,10 +173,33 @@ extern void json_object_object_add(struct json_object* obj, const char *key, * @param obj the json_object instance * @param key the object field name * @returns the json_object associated with the given field name + * @deprecated Please use json_object_object_get_ex */ extern struct json_object* json_object_object_get(struct json_object* obj, const char *key); +/** Get the json_object associated with a given object field. + * + * This returns true if the key is found, false in all other cases (including + * if obj isn't a json_type_object). + * + * *No* reference counts will be changed. There is no need to manually adjust + * reference counts through the json_object_put/json_object_get methods unless + * you need to have the child (value) reference maintain a different lifetime + * than the owning parent (obj). Ownership of value is retained by obj. + * + * @param obj the json_object instance + * @param key the object field name + * @param value a pointer where to store a reference to the json_object + * associated with the given field name. + * + * It is safe to pass a NULL value. + * @returns whether or not the key exists + */ +extern json_bool json_object_object_get_ex(struct json_object* obj, + const char *key, + struct json_object **value); + /** Delete the given json_object field * * The reference count will be decremented for the deleted object. If there -- 2.40.0