summaryrefslogtreecommitdiff
path: root/json_object.h
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2012-10-20 20:26:37 -0500
committerEric Haszlakiewicz <erh+git@nimenees.com>2012-10-20 20:26:37 -0500
commitf6b27cbb6cd2797641e71247e59299d771f13530 (patch)
tree76b1b0a9653436a19db8e14dfddcfdfc3af44ce8 /json_object.h
parent5abc0ea4443e46f8e508f17e3768c6404dee0d09 (diff)
Make it safe to delete keys while iterating with the json_object_object_foreach macro.
Diffstat (limited to 'json_object.h')
-rw-r--r--json_object.h14
1 files changed, 9 insertions, 5 deletions
diff --git a/json_object.h b/json_object.h
index 3f0ec6a..2621112 100644
--- a/json_object.h
+++ b/json_object.h
@@ -290,9 +290,10 @@ extern void json_object_object_del(struct json_object* obj, const char *key);
/**
* Iterate through all keys and values of an object.
*
- * Adding or deleting keys to the object while iterating is NOT allowed.
+ * Adding keys to the object while iterating is NOT allowed.
*
- * Replacing an existing key with a new value IS allowed.
+ * Deleting an existing key, or replacing an existing key with a
+ * new value IS allowed.
*
* @param obj the json_object instance
* @param key the local name for the char* key variable defined in the body
@@ -304,12 +305,13 @@ extern void json_object_object_del(struct json_object* obj, const char *key);
# define json_object_object_foreach(obj,key,val) \
char *key; \
struct json_object *val; \
- for(struct lh_entry *entry = json_object_get_object(obj)->head; \
+ for(struct lh_entry *entry = json_object_get_object(obj)->head, *entry_next = NULL; \
({ if(entry) { \
key = (char*)entry->k; \
val = (struct json_object*)entry->v; \
+ entry_next = entry->next; \
} ; entry; }); \
- entry = entry->next )
+ entry = entry_next )
#else /* ANSI C or MSC */
@@ -317,12 +319,14 @@ extern void json_object_object_del(struct json_object* obj, const char *key);
char *key;\
struct json_object *val; \
struct lh_entry *entry; \
+ struct lh_entry *entry_next = NULL; \
for(entry = json_object_get_object(obj)->head; \
(entry ? ( \
key = (char*)entry->k, \
val = (struct json_object*)entry->v, \
+ entry_next = entry->next, \
entry) : 0); \
- entry = entry->next)
+ entry = entry_next)
#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */