Skip to content

Segfault on trying to parse empty strings #27

@oaLightning

Description

@oaLightning

Currently the code segfaults when trying to parse an empty string. I provide here both a unittest that demonstrates the issue and a fix for it.

From: omer_barak <omer@weka.io>
Date: Thu, 8 Feb 2018 13:12:04 +0200
Subject: [PATCH] Handle empty files without segfault

---
 source/dini/parser.d | 6 ++++++
 source/dini/reader.d | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/source/dini/parser.d b/source/dini/parser.d
index d2f7fd5..e333390 100644
--- a/source/dini/parser.d
+++ b/source/dini/parser.d
@@ -681,4 +681,10 @@ unittest {
     );
     auto ini = Ini.ParseStringWith!MyReader(`path=C:\Path`);
     assert(ini("path") == `C:\Path`);
+}
+
+unittest {
+    // Make sure there isn't a segfault on empty files
+    Ini ini;
+    ini.parseString("");
 }
\ No newline at end of file
diff --git a/source/dini/reader.d b/source/dini/reader.d
index 6a03199..06c5127 100644
--- a/source/dini/reader.d
+++ b/source/dini/reader.d
@@ -22,7 +22,7 @@
 module dini.reader;

 import std.algorithm  : countUntil, canFind, map;
-import std.array 	  : array;
+import std.array 	  : array, empty;
 import std.functional : unaryFun;
 import std.string 	  : representation, assumeUTF, strip,
 	stripLeft, stripRight, split, join, format;
@@ -319,7 +319,7 @@ struct INIReader(INIFormatDescriptor Format, ubyte Flags = 0x00, alias Boxer)
 	this(string source)
 	{
 		// Make source end with newline
-		if (source[$-1] != '\n')
+		if (source.empty || (source[$-1] != '\n'))
 			this.source = (source ~ "\n").representation;
 		else
 			this.source = source.representation;
--
2.6.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions