You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2016/07/14 20:42:00 UTC

[16/20] jiffy commit: updated refs/heads/master to d3c00e1

Fix force_utf8 for object keys

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.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/commit/1febce3c
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/tree/1febce3c
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/diff/1febce3c

Branch: refs/heads/master
Commit: 1febce3ca86c5ca5d5a3618ed3d5f125bb99e4c5
Parents: 6303ff9
Author: Paul J. Davis <pa...@gmail.com>
Authored: Thu Mar 31 11:00:10 2016 -0500
Committer: Paul J. Davis <pa...@gmail.com>
Committed: Thu Mar 31 11:00:10 2016 -0500

----------------------------------------------------------------------
 src/jiffy.erl                  |  6 ++++++
 test/jiffy_04_string_tests.erl | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/blob/1febce3c/src/jiffy.erl
----------------------------------------------------------------------
diff --git a/src/jiffy.erl b/src/jiffy.erl
index 78f4cf2..215674c 100644
--- a/src/jiffy.erl
+++ b/src/jiffy.erl
@@ -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} ->
@@ -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} ->

http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/blob/1febce3c/test/jiffy_04_string_tests.erl
----------------------------------------------------------------------
diff --git a/test/jiffy_04_string_tests.erl b/test/jiffy_04_string_tests.erl
index 0d642d5..7cf645d 100644
--- a/test/jiffy_04_string_tests.erl
+++ b/test/jiffy_04_string_tests.erl
@@ -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)].
 
@@ -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))},
@@ -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) ->
     [
         {<<"\"\\/\"">>, <<"/">>}