Skip to content

Commit

Permalink
Fix force_utf8 for object keys
Browse files Browse the repository at this point in the history
Previously if a key was malformed UTF-8 and the user specified the
`force_utf8` option we would fail to try and encode a fixed up version
of the object. This was due to missing a clause to catch the
`invalid_object_member_key` exception. This adds the clause and a couple
tests to ensure it works.
  • Loading branch information
davisp committed Mar 31, 2016
1 parent 6303ff9 commit 1febce3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/jiffy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ encode(Data, Options) ->
{error, {invalid_string, _}} when ForceUTF8 == true ->
FixedData = jiffy_utf8:fix(Data),
encode(FixedData, Options -- [force_utf8]);
{error, {invalid_object_member_key, _}} when ForceUTF8 == true ->
FixedData = jiffy_utf8:fix(Data),
encode(FixedData, Options -- [force_utf8]);
{error, _} = Error ->
throw(Error);
{partial, IOData} ->
Expand Down Expand Up @@ -191,6 +194,9 @@ encode_loop(Data, Options, Encoder, Stack, IOBuf) ->
{error, {invalid_string, _}} when ForceUTF8 == true ->
FixedData = jiffy_utf8:fix(Data),
encode(FixedData, Options -- [force_utf8]);
{error, {invalid_object_member_key, _}} when ForceUTF8 == true ->
FixedData = jiffy_utf8:fix(Data),
encode(FixedData, Options -- [force_utf8]);
{error, _} = Error ->
throw(Error);
{partial, IOData} ->
Expand Down
24 changes: 24 additions & 0 deletions test/jiffy_04_string_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ string_error_test_() ->
string_utf8_test_() ->
[gen(utf8, Case) || Case <- cases(utf8)].


string_bad_utf8_key_test_() ->
Cases = cases(bad_utf8_key),
{{J}, {E}} = hd(Cases),
ExtraProps = [{<<"abcdeefeadasffasdfa">>, I} || I <- lists:seq(1, 10000)],
Big = {{ExtraProps ++ J}, {ExtraProps ++ E}},
AllCases = [Big | Cases],
[gen(bad_utf8_key, Case) || Case <- AllCases].


string_escaped_slashes_test_() ->
[gen(escaped_slashes, Case) || Case <- cases(escaped_slashes)].

Expand Down Expand Up @@ -54,6 +64,12 @@ gen(utf8, {Case, Fixed}) ->
?_assertThrow({error, {_, invalid_string}}, jiffy:decode(Case2))
]};

gen(bad_utf8_key, {J, E}) ->
{msg("Bad UTF-8 key: - ~p", [size(term_to_binary(J))]), [
?_assertThrow({error, {invalid_object_member_key, _}}, jiffy:encode(J)),
?_assertEqual(E, jiffy:decode(jiffy:encode(J, [force_utf8])))
]};

gen(escaped_slashes, {J, E}) ->
{msg("escaped_slashes - ~s", [J]), [
{"Decode", ?_assertEqual(E, dec(J))},
Expand Down Expand Up @@ -152,6 +168,14 @@ cases(utf8) ->
{<<16#FC, 16#82, 16#80, 16#80, 16#80, 16#80>>, <<16#EF, 16#BF, 16#BD>>}
];

cases(bad_utf8_key) ->
[
{
{[{<<"foo", 16#80, "bar">>, true}]},
{[{<<"foo", 16#EF, 16#BF, 16#BD, "bar">>, true}]}
}
];

cases(escaped_slashes) ->
[
{<<"\"\\/\"">>, <<"/">>}
Expand Down

0 comments on commit 1febce3

Please sign in to comment.