Skip to content
This repository
Browse code

detect invalid UTF-8 encoding in JSON strings, throw appropriate error

  • Loading branch information...
commit eca645045aef5fd4d5df5c4a8179c6d44a89e463 1 parent 2539e74
Philip Weaver authored February 19, 2013

Showing 1 changed file with 11 additions and 6 deletions. Show diff stats Hide diff stats

  1. 17  Data/Aeson/Parser/Internal.hs
17  Data/Aeson/Parser/Internal.hs
@@ -37,7 +37,7 @@ import Data.ByteString as B
37 37
 import Data.Char (chr)
38 38
 import Data.Monoid (mappend, mempty)
39 39
 import Data.Text as T
40  
-import Data.Text.Encoding (decodeUtf8)
  40
+import Data.Text.Encoding (decodeUtf8')
41 41
 import Data.Vector as Vector hiding ((++))
42 42
 import Data.Word (Word8)
43 43
 import qualified Data.Attoparsec as A
@@ -175,11 +175,16 @@ jstring_ = {-# SCC "jstring_" #-} do
175 175
                                         then Nothing
176 176
                                         else Just (c == backslash)
177 177
   _ <- A.word8 doubleQuote
178  
-  if backslash `B.elem` s
179  
-    then case Z.parse unescape s of
180  
-           Right r  -> return (decodeUtf8 r)
181  
-           Left err -> fail err
182  
-    else return (decodeUtf8 s)
  178
+  s' <- if backslash `B.elem` s
  179
+        then case Z.parse unescape s of
  180
+            Right r  -> return r
  181
+            Left err -> fail err
  182
+         else return s
  183
+
  184
+  case decodeUtf8' s' of
  185
+      Right r  -> return r
  186
+      Left err -> fail $ show err
  187
+
183 188
 {-# INLINE jstring_ #-}
184 189
 
185 190
 unescape :: Z.Parser ByteString

0 notes on commit eca6450

Please sign in to comment.
Something went wrong with that request. Please try again.