on master help sign in
Fixed segfault in makeString: peekCString may be invoked with nullPtr (i.e.

after document end, get_parser_context returns nullPtr). Added test case.

commit9e64c3ee67545caaeac7884aa9b1f91bebf3d8a0 authorAnton Ageev <antage@gmail.com> dateMon 10 May 2010, 18:23 (16 years ago) parent7d47e71
2 files changed+20−1
Text/Libyaml.hs+3−1 runtests.hs+17
Text/Libyaml.hs+3−1
@@ -258,7 +258,9 @@ foreign import ccall unsafe "get_parser_error_offset"
258258makeString :: MonadIO m => (a -> m (Ptr CUChar)) -> a -> m String
259259makeString f a = do
260260 cchar <- castPtr `liftM` f a
261− liftIO $ peekCString cchar
261+ if cchar == nullPtr
262+ then return ""
263+ else liftIO $ peekCString cchar
262264
263265parserParseOne :: (MonadFailure YamlException m, With m)
264266 => Parser
runtests.hs+17
@@ -16,6 +16,8 @@ import Test.Framework.Providers.HUnit
1616import Test.HUnit hiding (Test, path)
1717--import Test.QuickCheck
1818
19+import Control.Exception
20+
1921main :: IO ()
2022main = defaultMain
2123 [ testCase "count scalars with anchor" caseCountScalarsWithAnchor
@@ -27,6 +29,7 @@ main = defaultMain
2729 , testCase "encode/decode" caseEncodeDecode
2830 , testCase "encode/decode file" caseEncodeDecodeFile
2931 , testCase "interleaved encode/decode" caseInterleave
32+ , testCase "decode invalid document (without segfault)" caseDecodeInvalidDocument
3033 ]
3134
3235caseCountScalarsWithAnchor :: Assertion
@@ -173,3 +176,17 @@ caseInterleave = do
173176 case e of
174177 EventNone -> return ()
175178 _ -> emitEvent e >> inside
179+
180+caseDecodeInvalidDocument :: Assertion
181+caseDecodeInvalidDocument = do
182+ handle (\(YamlParserException {}) -> return ()) $ do
183+ _ <- decode yamlBS $ dec id
184+ assertFailure "expected parsing exception, but got no errors"
185+ where
186+ yamlString = " - foo\n - baz\nbuz"
187+ yamlBS = B8.pack yamlString
188+ dec front = do
189+ e <- parseEvent
190+ case e of
191+ EventNone -> return $ front []
192+ _ -> dec $ front . (:) e