You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2023/05/06 14:05:47 UTC

[couchdb] 02/05: consolidate checksum verification

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch remove-md5-more
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2663bdc6bf3d7c512d6296026c11ef9d9f3c9465
Author: Robert Newson <rn...@apache.org>
AuthorDate: Sat May 6 14:28:26 2023 +0100

    consolidate checksum verification
---
 src/couch/src/couch_file.erl | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl
index f52417666..315f3a795 100644
--- a/src/couch/src/couch_file.erl
+++ b/src/couch/src/couch_file.erl
@@ -676,13 +676,7 @@ load_header(Fd, Pos, HeaderLen, RestBlock) ->
         end,
     <<Checksum:16/binary, HeaderBin/binary>> =
         iolist_to_binary(remove_block_prefixes(?PREFIX_SIZE, RawBin)),
-    case exxhash:xxhash128(HeaderBin) of
-        Checksum ->
-            ok;
-        <<_/binary>> ->
-            couch_stats:increment_counter([couch_file, old_checksums]),
-            Checksum = couch_hash:md5_hash(HeaderBin)
-    end,
+    true = verify_checksum(HeaderBin, Checksum),
     {ok, HeaderBin}.
 
 %% Read multiple block locations using a single file:pread/2.
@@ -861,16 +855,25 @@ monitored_by_pids() ->
 verify_checksum(_Fd, _Pos, IoList, <<>>) ->
     IoList;
 verify_checksum(Fd, Pos, IoList, Checksum) ->
-    case exxhash:xxhash128(iolist_to_binary(IoList)) of
-        Checksum ->
+    case verify_checksum(IoList, Checksum) of
+        true ->
             IoList;
-        <<_/binary>> ->
-            case couch_hash:md5_hash(IoList) of
-                Checksum ->
+        false ->
+            report_checksum_error(Fd, Pos)
+    end.
+
+verify_checksum(Data, ExpectedChecksum) ->
+    Bin = iolist_to_binary(Data),
+    case ExpectedChecksum == exxhash:xxhash128(Bin) of
+        true ->
+            true;
+        false ->
+            case ExpectedChecksum == couch_hash:md5_hash(Data) of
+                true ->
                     couch_stats:increment_counter([couch_file, old_checksums]),
-                    IoList;
-                _ ->
-                    report_checksum_error(Fd, Pos)
+                    true;
+                false ->
+                    false
             end
     end.