Fix for "Invalid UTF-8 stream" error on valid characters (#476). #477

Merged
merged 2 commits into from Oct 22, 2016
Jump to file or symbol
Failed to load files and symbols.
+25 −3
Diff settings

Always

Just for now

View
@@ -180,6 +180,7 @@ test-suite tests
base,
base-compat,
base-orphans >= 0.5.3 && <0.6,
+ base16-bytestring,
containers,
dlist,
generic-deriving >= 1.10 && < 1.12,
View
@@ -130,7 +130,7 @@ int _js_decode_string(uint16_t *const dest, size_t *destoff,
*d++ = (uint16_t) unidata;
if (surrogate) {
- if (unidata <= 0xDC00 || unidata >= 0xDFFF) // is not low surrogate
+ if (unidata < 0xDC00 || unidata > 0xDFFF) // is not low surrogate
return -1;
surrogate = 0;
} else if (unidata >= 0xD800 && unidata <= 0xDBFF ) { // is high surrogate
@@ -147,4 +147,3 @@ int _js_decode_string(uint16_t *const dest, size_t *destoff,
if (codepoint != 'u') { return -1; }
DISPATCH_ASCII(unicode1)
}
-
View
@@ -21,7 +21,7 @@ import Prelude ()
import Prelude.Compat
import Control.Applicative (Const(..))
-import Control.Monad (forM)
+import Control.Monad (forM, forM_)
import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(..), FromJSONKey(..), ToJSON1(..), decode, eitherDecode, encode, genericParseJSON, genericToEncoding, genericToJSON, object, withObject)
import Data.Aeson.Internal (JSONPathElement(..), formatError)
import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)
@@ -50,8 +50,10 @@ import Instances ()
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, assertFailure, assertEqual)
+import Text.Printf (printf)
import Types (Approx(..), Compose3, Compose3', I)
import UnitTests.NullaryConstructors (nullaryConstructors)
+import qualified Data.ByteString.Base16.Lazy as LBase16
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.DList as DList
import qualified Data.HashMap.Strict as HM
@@ -104,6 +106,7 @@ tests = testGroup "unit" [
, testGroup "Nullary constructors" $ fmap (testCase "-") nullaryConstructors
, testGroup "FromJSONKey" $ fmap (testCase "-") fromJSONKeyAssertions
, testCase "PR #455" pr455
+ , testCase "Unescape string (PR #477)" unescapeString
]
roundTripCamel :: String -> Assertion
@@ -574,6 +577,25 @@ data MyRecord2 = MyRecord2 {_field3 :: Maybe Int, _field4 :: Maybe Bool}
instance ToJSON MyRecord2
instance FromJSON MyRecord2
+-- A regression test for: https://github.com/bos/aeson/pull/477
+unescapeString :: Assertion
+unescapeString = do
+ assertEqual "Basic escaping"
+ (Right ("\" / \\ \b \f \n \r \t" :: String))
+ (eitherDecode "\"\\\" \\/ \\\\ \\b \\f \\n \\r \\t\"")
+
+ forM_ [minBound .. maxBound :: Char] $ \ c ->
+ let s = LT.pack [c] in
+ assertEqual (printf "UTF-16 encoded '\\x%X'" c)
+ (Right s) (eitherDecode $ utf16Char s)
+ where
+ utf16Char = formatString . LBase16.encode . LT.encodeUtf16BE
+ formatString s
+ | L.length s == 4 = L.concat ["\"\\u", s, "\""]
+ | L.length s == 8 =
+ L.concat ["\"\\u", L.take 4 s, "\\u", L.drop 4 s, "\""]
+ | otherwise = error "unescapeString: can't happen"
+
-- A regression test for: https://github.com/bos/aeson/pull/455
data Foo a = FooNil | FooCons (Foo Int)