summaryrefslogtreecommitdiffstats
path: root/src/jplist.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2026-01-14 02:50:40 +0100
committerGravatar Nikias Bassen2026-01-14 02:50:40 +0100
commit5b3ae4c1326e0c85df906ffea49b97b80b13ceae (patch)
treea6efdca6333188abf924e13cb70a7aae49665ae1 /src/jplist.c
parent25d61ff8b5d994a02c0cc2af8e029bebd3a94cb3 (diff)
downloadlibplist-5b3ae4c1326e0c85df906ffea49b97b80b13ceae.tar.gz
libplist-5b3ae4c1326e0c85df906ffea49b97b80b13ceae.tar.bz2
Add circular reference detection to all format writers
Thanks to @LkkkLxy for pointing out the issue.
Diffstat (limited to 'src/jplist.c')
-rw-r--r--src/jplist.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/jplist.c b/src/jplist.c
index 1c7a932..2e53400 100644
--- a/src/jplist.c
+++ b/src/jplist.c
@@ -38,6 +38,7 @@
#include "plist.h"
#include "strbuf.h"
#include "jsmn.h"
+#include "hashtable.h"
#ifdef DEBUG
static int plist_json_debug = 0;
@@ -315,18 +316,27 @@ static int num_digits_u(uint64_t i)
return n;
}
-static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify)
+static plist_err_t _node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify, hashtable_t *visited)
{
plist_data_t data;
if (!node) {
return PLIST_ERR_INVALID_ARG;
}
+
+ if (hash_table_lookup(visited, node)) {
+ PLIST_JSON_WRITE_ERR("circular reference detected\n");
+ return PLIST_ERR_CIRCULAR_REF;
+ }
+
+ // mark as visited
+ hash_table_insert(visited, node, (void*)1);
+
data = plist_get_data(node);
if (node->children) {
node_t ch;
unsigned int n_children = node_n_children(node);
for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
- plist_err_t res = node_estimate_size(ch, size, depth + 1, prettify);
+ plist_err_t res = _node_estimate_size(ch, size, depth + 1, prettify, visited);
if (res < 0) {
return res;
}
@@ -402,6 +412,15 @@ static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t dept
return PLIST_ERR_SUCCESS;
}
+static plist_err_t node_estimate_size(node_t node, uint64_t *size, uint32_t depth, int prettify)
+{
+ hashtable_t *visited = hash_table_new(plist_node_ptr_hash, plist_node_ptr_compare, NULL);
+ if (!visited) return PLIST_ERR_NO_MEM;
+ plist_err_t err = _node_estimate_size(node, size, depth, prettify, visited);
+ hash_table_destroy(visited);
+ return err;
+}
+
plist_err_t plist_to_json(plist_t plist, char **plist_json, uint32_t* length, int prettify)
{
uint64_t size = 0;