From 17659d59c2f907e4e7e38f500051b00a1816f463 Mon Sep 17 00:00:00 2001 From: Neil Mayhew Date: Mon, 26 Apr 2021 11:34:57 -0600 Subject: [PATCH] Fail parsing if the input isn't completely consumed Without this, parsing stops at the start of any invalid input and returns a successful result Closes #29 --- src/Data/Ini.hs | 3 ++- test/Main.hs | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Data/Ini.hs b/src/Data/Ini.hs index 7c2b6ee..f08e71d 100644 --- a/src/Data/Ini.hs +++ b/src/Data/Ini.hs @@ -245,7 +245,8 @@ iniParser :: Parser Ini iniParser = (\kv secs -> Ini {iniSections = M.fromList secs, iniGlobals = kv}) <$> many keyValueParser <*> - many sectionParser + many sectionParser <* + (endOfInput <|> (fail . T.unpack =<< takeWhile (not . isControl))) -- | A section. Format: @[foo]@. Conventionally, @[FOO]@. sectionParser :: Parser (Text,[(Text, Text)]) diff --git a/test/Main.hs b/test/Main.hs index 50da8f5..f45b7f2 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -64,4 +64,11 @@ main = ] , iniGlobals = [("port", "6667"), ("hostname", "localhost")] - }))))) + }))) + it + "File with invalid keys" + (shouldBe + (parseIni + "Name=Foo\n\ + \Name[en_GB]=Fubar") + (Left "Failed reading: Name[en_GB]=Fubar"))))