Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aeson.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions cbits/unescape_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -147,4 +147,3 @@ int _js_decode_string(uint16_t *const dest, size_t *destoff,
if (codepoint != 'u') { return -1; }
DISPATCH_ASCII(unicode1)
}

24 changes: 23 additions & 1 deletion tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down