Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 09c2b930f3547a9831c5d2ebe86e272678182fec 1 parent 75c9259
@basvandijk basvandijk authored committed
Showing with 8 additions and 6 deletions.
  1. +8 −6 Data/Csv/Conversion.hs
View
14 Data/Csv/Conversion.hs
@@ -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.
Please sign in to comment.
Something went wrong with that request. Please try again.