From 9ce85bcdf8da124ad9538875f945ab5c4bb09e1e Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 5 Jan 2020 23:45:38 +0100
Subject: Improved the stability of the Yaml parser.

---
 plugins/yaml/reader.c | 23 ++++++++++++++---------
 plugins/yaml/tree.c   | 17 +++++++++++++++--
 2 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/plugins/yaml/reader.c b/plugins/yaml/reader.c
index c8852bb..54273c1 100644
--- a/plugins/yaml/reader.c
+++ b/plugins/yaml/reader.c
@@ -197,20 +197,25 @@ GYamlReader *g_yaml_reader_new_from_content(const char *content, size_t length)
          *iter != '\0';
          iter = ++saved, saved = strchr(iter, '\n'), number++)
     {
-        *saved = '\0';
+        if (saved != NULL)
+            *saved = '\0';
 
-        if (*iter == '\0')
-            continue;
+        if (*iter != '\0')
+        {
+            line = g_yaml_line_new(iter, number);
 
-        line = g_yaml_line_new(iter, number);
+            if (line == NULL)
+                goto format_error;
 
-        if (line == NULL)
-            goto format_error;
+            result->lines = realloc(result->lines, ++result->count * sizeof(GYamlLine *));
 
-        result->lines = realloc(result->lines, ++result->count * sizeof(GYamlLine *));
+            g_object_ref_sink(G_OBJECT(line));
+            result->lines[result->count - 1] = line;
 
-        g_object_ref_sink(G_OBJECT(line));
-        result->lines[result->count - 1] = line;
+        }
+
+        if (saved == NULL)
+            break;
 
     }
 
diff --git a/plugins/yaml/tree.c b/plugins/yaml/tree.c
index 5db82ec..4487f9d 100644
--- a/plugins/yaml/tree.c
+++ b/plugins/yaml/tree.c
@@ -270,7 +270,11 @@ static bool g_yaml_tree_build_node(GYamlCollection *collec, GYamlLine **lines, s
         /* Début d'un sous-ensemble */
         else if (indent > expected)
         {
-            assert(last != NULL);
+            if (last == NULL)
+            {
+                result = false;
+                goto done;
+            }
 
             sub = g_yaml_collection_new(is_item);
             g_yaml_pair_set_collection(G_YAML_PAIR(last), sub);
@@ -319,6 +323,13 @@ static bool g_yaml_tree_build_node(GYamlCollection *collec, GYamlLine **lines, s
                 }
 
                 last = G_YAML_NODE(g_yaml_pair_new(line));
+
+                if (last == NULL)
+                {
+                    result = false;
+                    goto done;
+                }
+
                 g_yaml_collection_add_node(collec, last);
 
                 (*cur)++;
@@ -353,7 +364,9 @@ GYamlNode *g_yaml_tree_get_root(const GYamlTree *tree)
     GYamlNode *result;                      /* Liste à retourner           */
 
     result = tree->root;
-    g_object_ref(G_OBJECT(result));
+
+    if (result != NULL)
+        g_object_ref(G_OBJECT(result));
 
     return result;
 
-- 
cgit v0.11.2-87-g4458