From 32706a4dcede0bfe797b05c77ec8d35fbc401d7a Mon Sep 17 00:00:00 2001 From: niksedk Date: Thu, 14 Sep 2023 18:18:52 +0200 Subject: [PATCH] Avoid crashing on large file --- src/XmlContentTranslator/Main.cs | 55 +++++++++++++++----------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/XmlContentTranslator/Main.cs b/src/XmlContentTranslator/Main.cs index 18fd3ee..c7d81f4 100644 --- a/src/XmlContentTranslator/Main.cs +++ b/src/XmlContentTranslator/Main.cs @@ -312,45 +312,40 @@ private void AddListViewItem(XmlNode node) { if (listViewLanguageTags.Columns.Count == 2) { - if (node.NodeType != XmlNodeType.Comment && node.NodeType != XmlNodeType.CDATA) + if (node.NodeType == XmlNodeType.Comment || node.NodeType == XmlNodeType.CDATA) { + return; + } - ListViewItem item; - if (node.NodeType == XmlNodeType.Attribute) - { - item = new ListViewItem("@" + node.Name); - item.SubItems.Add(node.InnerText); - } - else if (XmlUtils.ContainsText(node)) - { - item = new ListViewItem(node.Name); - item.SubItems.Add(node.InnerXml); - } - else - { - item = new ListViewItem(node.Name); - item.SubItems.Add(node.InnerText); - } + ListViewItem item; + if (node.NodeType == XmlNodeType.Attribute) + { + item = new ListViewItem("@" + node.Name); + item.SubItems.Add(node.InnerText); + } + else if (XmlUtils.ContainsText(node)) + { + item = new ListViewItem(node.Name); + item.SubItems.Add(node.InnerXml); + } + else + { + item = new ListViewItem(node.Name); + item.SubItems.Add(node.InnerText); + } - item.Tag = node; - listViewLanguageTags.Items.Add(item); - ListViewItemComparer.NoSortOrder.Add(item, listViewLanguageTags.Items.Count); + item.Tag = node; + listViewLanguageTags.Items.Add(item); + ListViewItemComparer.NoSortOrder.Add(item, listViewLanguageTags.Items.Count); - _listViewItemHashtable.Add(XmlUtils.BuildNodePath(node), item); // fails on some attributes!! - } + _listViewItemHashtable.Add(XmlUtils.BuildNodePath(node), item); // fails on some attributes!! } else if (listViewLanguageTags.Columns.Count == 3) { - var item = _listViewItemHashtable[XmlUtils.BuildNodePath(node)] as ListViewItem; - - if (XmlUtils.ContainsText(node)) - { - item?.SubItems.Add(node.InnerXml); - } - else + if (_listViewItemHashtable[XmlUtils.BuildNodePath(node)] is ListViewItem item && item.SubItems.Count < 4096) { - item?.SubItems.Add(node.InnerText); + item.SubItems.Add(XmlUtils.ContainsText(node) ? node.InnerXml : node.InnerText); } } }