From ae7564c42f742e6c7a069a3a631cc7ba7669d25b Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Sun, 21 Jul 2019 16:05:49 +0200 Subject: [PATCH 1/3] Added missing initialization for entities in SvgNodeReader (#519) - prevents crash, fixes #518 - added test --- Source/SvgNodeReader.cs | 57 +++++++------------ Tests/Svg.UnitTests/NodeReaderTests.cs | 19 +++++++ .../Resources/Issue518_Entities/Entities.svg | 17 ++++++ Tests/Svg.UnitTests/Svg.UnitTests.csproj | 3 + 4 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 Tests/Svg.UnitTests/NodeReaderTests.cs create mode 100644 Tests/Svg.UnitTests/Resources/Issue518_Entities/Entities.svg diff --git a/Source/SvgNodeReader.cs b/Source/SvgNodeReader.cs index cb8c534fe..4ed686a1d 100644 --- a/Source/SvgNodeReader.cs +++ b/Source/SvgNodeReader.cs @@ -1,10 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Xml; -using System.IO; -using System.Collections.Specialized; namespace Svg { @@ -18,7 +14,7 @@ internal sealed class SvgNodeReader : XmlNodeReader public SvgNodeReader(XmlNode node, Dictionary entities) : base(node) { - this._entities = entities; + _entities = entities ?? new Dictionary(); } /// @@ -30,7 +26,7 @@ public override string Value { get { - return (this._customValue) ? this._value : base.Value; + return _customValue ? _value : base.Value; } } @@ -43,20 +39,7 @@ public override string LocalName { get { - return (this._customValue) ? this._localName : base.LocalName; - } - } - - private IDictionary Entities - { - get - { - if (this._entities == null) - { - this._entities = new Dictionary(); - } - - return this._entities; + return _customValue ? _localName : base.LocalName; } } @@ -72,20 +55,20 @@ public override bool MoveToNextAttribute() if (moved) { - this._localName = base.LocalName; + _localName = base.LocalName; - if (this.ReadAttributeValue()) + if (ReadAttributeValue()) { - if (this.NodeType == XmlNodeType.EntityReference) + if (NodeType == XmlNodeType.EntityReference) { - this.ResolveEntity(); + ResolveEntity(); } else { - this._value = base.Value; + _value = base.Value; } } - this._customValue = true; + _customValue = true; } return moved; @@ -100,12 +83,12 @@ public override bool MoveToNextAttribute() /// An error occurred while parsing the XML. public override bool Read() { - this._customValue = false; + _customValue = false; bool read = base.Read(); - if (this.NodeType == XmlNodeType.DocumentType) + if (NodeType == XmlNodeType.DocumentType) { - this.ParseEntities(); + ParseEntities(); } return read; @@ -114,7 +97,7 @@ public override bool Read() private void ParseEntities() { const string entityText = " public override void ResolveEntity() { - if (this.NodeType == XmlNodeType.EntityReference) + if (NodeType == XmlNodeType.EntityReference) { - if (this._entities.ContainsKey(this.Name)) + if (_entities.ContainsKey(Name)) { - this._value = this._entities[this.Name]; + _value = _entities[Name]; } else { - this._value = string.Empty; + _value = string.Empty; } - this._customValue = true; + _customValue = true; } } } diff --git a/Tests/Svg.UnitTests/NodeReaderTests.cs b/Tests/Svg.UnitTests/NodeReaderTests.cs new file mode 100644 index 000000000..d138844c8 --- /dev/null +++ b/Tests/Svg.UnitTests/NodeReaderTests.cs @@ -0,0 +1,19 @@ +using NUnit.Framework; + +namespace Svg.UnitTests +{ + [TestFixture] + public class SvgNodeReaderTests : SvgTestHelper + { + protected override string TestResource { get { return GetFullResourceString("Issue518_Entities.Entities.svg"); } } + //protected override int ExpectedSize { get { return 4300; } } // original image has 4314 bytes + + [Test] + public void ReadingEntitiesFromXmlSucceeds() + { + var xmlDoc = GetXMLDocFromResource(); + var doc = SvgDocument.Open(xmlDoc); + Assert.DoesNotThrow(() => doc.Draw()); + } + } +} diff --git a/Tests/Svg.UnitTests/Resources/Issue518_Entities/Entities.svg b/Tests/Svg.UnitTests/Resources/Issue518_Entities/Entities.svg new file mode 100644 index 000000000..1fbb8ab0f --- /dev/null +++ b/Tests/Svg.UnitTests/Resources/Issue518_Entities/Entities.svg @@ -0,0 +1,17 @@ + + + + + + + + + + +]> + + + diff --git a/Tests/Svg.UnitTests/Svg.UnitTests.csproj b/Tests/Svg.UnitTests/Svg.UnitTests.csproj index 8d4b81aed..f6db43f86 100644 --- a/Tests/Svg.UnitTests/Svg.UnitTests.csproj +++ b/Tests/Svg.UnitTests/Svg.UnitTests.csproj @@ -61,6 +61,9 @@ + + + From 7d15d6c88258202e3f2c63c4e523739d870db6fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Sun, 4 Aug 2019 16:02:14 +0200 Subject: [PATCH 2/3] Remove empty entries --- Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs b/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs index d9fae88b6..7037f82d3 100644 --- a/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs +++ b/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs @@ -87,7 +87,7 @@ public override void Process(ImageBuffer buffer) }; break; default: // Matrix - var parts = this.Values.Replace(" ", " ").Split(new char[] { ' ', '\t', '\n', '\r', ',' }); + var parts = this.Values.Replace(" ", " ").Split(new char[] { ' ', '\t', '\n', '\r', ',' }, StringSplitOptions.RemoveEmptyEntries); colorMatrixElements = new float[5][]; for (int i = 0; i < 4; i++) { From f355c4f984059cf9bf04fedc5d6b81cf923cb0a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Sun, 4 Aug 2019 16:58:29 +0200 Subject: [PATCH 3/3] Remove unnecessary Replace call --- Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs b/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs index 7037f82d3..0e9495bf8 100644 --- a/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs +++ b/Source/Filter Effects/feColourMatrix/SvgColourMatrix.cs @@ -87,7 +87,7 @@ public override void Process(ImageBuffer buffer) }; break; default: // Matrix - var parts = this.Values.Replace(" ", " ").Split(new char[] { ' ', '\t', '\n', '\r', ',' }, StringSplitOptions.RemoveEmptyEntries); + var parts = this.Values.Split(new char[] { ' ', '\t', '\n', '\r', ',' }, StringSplitOptions.RemoveEmptyEntries); colorMatrixElements = new float[5][]; for (int i = 0; i < 4; i++) {