From fd506e2964cbd0934d63e9def99dd83707e83db2 Mon Sep 17 00:00:00 2001 From: Shravan Rajinikanth Date: Fri, 1 Dec 2017 03:33:16 -0600 Subject: [PATCH] Fixed SOE on deserializing an empty input - Added VdfException type to allow handling invalid input. - Fixes #5. --- Gameloop.Vdf/VdfException.cs | 9 +++++++++ Gameloop.Vdf/VdfSerializer.cs | 17 ++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 Gameloop.Vdf/VdfException.cs diff --git a/Gameloop.Vdf/VdfException.cs b/Gameloop.Vdf/VdfException.cs new file mode 100644 index 0000000..41cb434 --- /dev/null +++ b/Gameloop.Vdf/VdfException.cs @@ -0,0 +1,9 @@ +using System; + +namespace Gameloop.Vdf +{ + public class VdfException : Exception + { + public VdfException(string message) : base(message) { } + } +} diff --git a/Gameloop.Vdf/VdfSerializer.cs b/Gameloop.Vdf/VdfSerializer.cs index 41135a9..42da8d8 100644 --- a/Gameloop.Vdf/VdfSerializer.cs +++ b/Gameloop.Vdf/VdfSerializer.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; namespace Gameloop.Vdf { @@ -23,7 +24,8 @@ public VProperty Deserialize(TextReader textReader) { using (VdfReader vdfReader = new VdfTextReader(textReader, _settings)) { - vdfReader.ReadToken(); + if (!vdfReader.ReadToken()) + throw new VdfException("Incomplete VDF data."); return ReadProperty(vdfReader); } } @@ -33,7 +35,9 @@ private VProperty ReadProperty(VdfReader reader) VProperty result = new VProperty(); result.Key = reader.Value; - reader.ReadToken(); + if (!reader.ReadToken()) + throw new VdfException("Incomplete VDF data."); + if (reader.CurrentState == VdfReader.State.Property) result.Value = new VValue(reader.Value); else @@ -46,11 +50,14 @@ private VObject ReadObject(VdfReader reader) { VObject result = new VObject(); - reader.ReadToken(); + if (!reader.ReadToken()) + throw new VdfException("Incomplete VDF data."); + while (reader.CurrentState != VdfReader.State.Object || reader.Value != VdfStructure.ObjectEnd.ToString()) { result.Add(ReadProperty(reader)); - reader.ReadToken(); + if (!reader.ReadToken()) + throw new VdfException("Incomplete VDF data."); } return result;