summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Derrick <keith.derrick@palm.com>2012-04-12 11:43:34 -0700
committerKeith Derrick <keith.derrick@palm.com>2012-04-12 11:47:56 -0700
commit4a2cd966f5ef267545ec57f2490e3ec14f47d70f (patch)
tree3427c0b2fa1618bd4c80d30112d3bcdbc9b069fb
parent74d830dc035db05e2d7c4fecc4aff69c2c6d17e7 (diff)
downloadjson-c-4a2cd966f5ef267545ec57f2490e3ec14f47d70f.tar.gz
Add NULL-safe lookup function
New lh_table_lookup_ex() method protects itself against null pointers and invalid objects being passed in.
-rw-r--r--linkhash.c17
-rw-r--r--linkhash.h12
2 files changed, 26 insertions, 3 deletions
diff --git a/linkhash.c b/linkhash.c
index 88c0a7c..ddedc12 100644
--- a/linkhash.c
+++ b/linkhash.c
@@ -3,6 +3,7 @@
*
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
* Michael Clark <michael@metaparadigm.com>
+ * 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.
@@ -174,11 +175,21 @@ struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
const void* lh_table_lookup(struct lh_table *t, const void *k)
{
- struct lh_entry *e = lh_table_lookup_entry(t, k);
- if(e) return e->v;
- return NULL;
+ void *result;
+ lh_table_lookup_ex(t, k, &result);
+ return result;
}
+json_bool lh_table_lookup_ex(struct lh_table* t, const void* k, void **v)
+{
+ struct lh_entry *e = lh_table_lookup_entry(t, k);
+ if (e != NULL) {
+ if (v != NULL) *v = (void *)e->v;
+ return TRUE; /* key found */
+ }
+ if (v != NULL) *v = NULL;
+ return FALSE; /* key not found */
+}
int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
{
diff --git a/linkhash.h b/linkhash.h
index 9d89460..bbb5488 100644
--- a/linkhash.h
+++ b/linkhash.h
@@ -3,6 +3,7 @@
*
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
* Michael Clark <michael@metaparadigm.com>
+ * 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.
@@ -12,6 +13,8 @@
#ifndef _linkhash_h_
#define _linkhash_h_
+#include "json_object.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -241,9 +244,18 @@ extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
* @param t the table to lookup
* @param k a pointer to the key to lookup
* @return a pointer to the found value or NULL if it does not exist.
+ * @deprecated Use lh_table_lookup_ex instead.
*/
extern const void* lh_table_lookup(struct lh_table *t, const void *k);
+/**
+ * Lookup a record in the table
+ * @param t the table to lookup
+ * @param k a pointer to the key to lookup
+ * @param v a pointer to a where to store the found value (set to NULL if it doesn't exist).
+ * @return whether or not the key was found
+ */
+extern json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v);
/**
* Delete a record from the table.