Permalink
Browse files

Safely handle invalid UTF-8 data by letting the parser fail

  • Loading branch information...
1 parent 75c9259 commit 09c2b930f3547a9831c5d2ebe86e272678182fec @basvandijk basvandijk committed with Feb 24, 2013
Showing with 8 additions and 6 deletions.
  1. +8 −6 Data/Csv/Conversion.hs
View
@@ -393,10 +393,12 @@ instance FromField () where
-- | Assumes UTF-8 encoding.
instance FromField Char where
- parseField s
- | T.compareLength t 1 == EQ = pure (T.head t)
- | otherwise = typeError "Char" s Nothing
- where t = T.decodeUtf8 s
+ parseField s =
+ case T.decodeUtf8' s of
+ Left e -> fail $ show e
+ Right t
+ | T.compareLength t 1 == EQ -> pure (T.head t)
+ | otherwise -> typeError "Char" s Nothing
{-# INLINE parseField #-}
-- | Uses UTF-8 encoding.
@@ -560,7 +562,7 @@ instance ToField L.ByteString where
-- | Assumes UTF-8 encoding.
instance FromField T.Text where
- parseField = pure . T.decodeUtf8
+ parseField = either (fail . show) pure . T.decodeUtf8'
{-# INLINE parseField #-}
-- | Uses UTF-8 encoding.
@@ -570,7 +572,7 @@ instance ToField T.Text where
-- | Assumes UTF-8 encoding.
instance FromField LT.Text where
- parseField s = pure (LT.fromChunks [T.decodeUtf8 s])
+ parseField = either (fail . show) (pure . LT.fromChunks . (:[])) . T.decodeUtf8'
{-# INLINE parseField #-}
-- | Uses UTF-8 encoding.

0 comments on commit 09c2b93

Please sign in to comment.