From bdfbd076f2d3e7212f300bf5edbbad1934fae324 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 27 Sep 2025 20:48:09 +0200 Subject: html.c: Do not recurse on siblings It is not required to do so. Otherwise, nodes with many siblings could lead to a very deep call stack for no reason. --- html.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/html.c b/html.c index 0277498..094b00c 100644 --- a/html.c +++ b/html.c @@ -20,6 +20,9 @@ struct html_node struct html_node *child, *sibling; }; +static int serialize_siblings(struct dynstr *, const struct html_node *, + unsigned); + static char *html_encode(const char *s) { struct dynstr d; @@ -210,9 +213,9 @@ static int serialize_node(struct dynstr *const d, { dynstr_append_or_ret_nonzero(d, "\n"); - if (serialize_node(d, n->child, level + 1)) + if (serialize_siblings(d, n->child, level + 1)) { - fprintf(stderr, "%s: serialize_node failed\n", __func__); + fprintf(stderr, "%s: serialize_siblings failed\n", __func__); return -1; } @@ -223,19 +226,30 @@ static int serialize_node(struct dynstr *const d, dynstr_append_or_ret_nonzero(d, "", n->element); } - /* TODO: print siblings */ - dynstr_append_or_ret_nonzero(d, "\n"); + return 0; +} + +static int serialize_siblings(struct dynstr *const d, + const struct html_node *n, const unsigned level) +{ + struct html_node *sibling; + + do + { + sibling = n->sibling; + + if (serialize_node(d, n, level)) + return -1; - if (n->sibling) - return serialize_node(d, n->sibling, level); + } while ((n = sibling)); return 0; } int html_serialize(const struct html_node *const n, struct dynstr *const d) { - return serialize_node(d, n, 0); + return serialize_siblings(d, n, 0); } static void html_attribute_free(struct html_attribute *const a) -- cgit v1.2.3