summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Avdey <eiri@eiri.ca>2019-02-05 10:39:27 -0400
committerGitHub <noreply@github.com>2019-02-05 10:39:27 -0400
commit20448c654a0f7c82e8defffe7a850d69378e2f33 (patch)
tree0d25944c29da8ce2106daf233b1be084b3702c61
parent9517b260951c6e62c6190a9ccc15a8e28d8cfa63 (diff)
parentfbb5a14dfa52592212eec4f810c459a83125f6a8 (diff)
downloadcouchdb-20448c654a0f7c82e8defffe7a850d69378e2f33.tar.gz
Merge pull request #1901 from apache/fix-doc-update-invalid-rev-crash
Fix `badarg` crash on an invalid revision for individual doc update
-rw-r--r--src/couch/src/couch_doc.erl8
-rw-r--r--src/couch/test/couch_db_doc_tests.erl21
2 files changed, 24 insertions, 5 deletions
diff --git a/src/couch/src/couch_doc.erl b/src/couch/src/couch_doc.erl
index 2a2b4db68..4a49372c7 100644
--- a/src/couch/src/couch_doc.erl
+++ b/src/couch/src/couch_doc.erl
@@ -179,12 +179,12 @@ parse_rev(Rev) when is_list(Rev) ->
SplitRev = lists:splitwith(fun($-) -> false; (_) -> true end, Rev),
case SplitRev of
{Pos, [$- | RevId]} ->
- IntPos = try list_to_integer(Pos) of
- Val -> Val
+ try
+ IntPos = list_to_integer(Pos),
+ {IntPos, parse_revid(RevId)}
catch
error:badarg -> throw({bad_request, <<"Invalid rev format">>})
- end,
- {IntPos, parse_revid(RevId)};
+ end;
_Else -> throw({bad_request, <<"Invalid rev format">>})
end;
parse_rev(_BadRev) ->
diff --git a/src/couch/test/couch_db_doc_tests.erl b/src/couch/test/couch_db_doc_tests.erl
index aa9c6fd71..cdcf81d15 100644
--- a/src/couch/test/couch_db_doc_tests.erl
+++ b/src/couch/test/couch_db_doc_tests.erl
@@ -42,7 +42,8 @@ couch_db_doc_test_() ->
foreach,
fun setup/0, fun teardown/1,
[
- fun should_truncate_number_of_revisions/1
+ fun should_truncate_number_of_revisions/1,
+ fun should_raise_bad_request_on_invalid_rev/1
]
}
}
@@ -59,6 +60,24 @@ should_truncate_number_of_revisions(DbName) ->
?_assertEqual(5, length(Revs)).
+should_raise_bad_request_on_invalid_rev(DbName) ->
+ DocId = <<"foo">>,
+ InvalidRev1 = <<"foo">>,
+ InvalidRev2 = <<"a-foo">>,
+ InvalidRev3 = <<"1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">>,
+ Expect = {bad_request, <<"Invalid rev format">>},
+ Db = open_db(DbName),
+ create_doc(Db, DocId),
+ [
+ {InvalidRev1,
+ ?_assertThrow(Expect, add_revisions(Db, DocId, InvalidRev1, 1))},
+ {InvalidRev2,
+ ?_assertThrow(Expect, add_revisions(Db, DocId, InvalidRev2, 1))},
+ {InvalidRev3,
+ ?_assertThrow(Expect, add_revisions(Db, DocId, InvalidRev3, 1))}
+ ].
+
+
open_db(DbName) ->
{ok, Db} = couch_db:open_int(DbName, [?ADMIN_CTX]),
Db.