Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B12702009D9 for ; Tue, 17 May 2016 23:17:39 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id AFDD51607A8; Tue, 17 May 2016 21:17:39 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 06D001609F5 for ; Tue, 17 May 2016 23:17:38 +0200 (CEST) Received: (qmail 24125 invoked by uid 500); 17 May 2016 21:17:38 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 24108 invoked by uid 99); 17 May 2016 21:17:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 May 2016 21:17:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B5F71DFADE; Tue, 17 May 2016 21:17:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: eiri@apache.org To: commits@couchdb.apache.org Date: Tue, 17 May 2016 21:17:37 -0000 Message-Id: <45c181bb98084c7c8781d9de101fc86f@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/4] couch commit: updated refs/heads/master to 23f1880 archived-at: Tue, 17 May 2016 21:17:39 -0000 Repository: couchdb-couch Updated Branches: refs/heads/master 49774f269 -> 23f188096 Raise exception on attempt of reading beyound end of file Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/89990e18 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/89990e18 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/89990e18 Branch: refs/heads/master Commit: 89990e1800934823b83341152d2a103cd4bcad8b Parents: 49774f2 Author: Eric Avdey Authored: Mon May 16 10:15:38 2016 -0300 Committer: Eric Avdey Committed: Mon May 16 16:48:13 2016 -0300 ---------------------------------------------------------------------- src/couch_file.erl | 16 ++++++++++++---- test/couch_file_tests.erl | 19 ++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/89990e18/src/couch_file.erl ---------------------------------------------------------------------- diff --git a/src/couch_file.erl b/src/couch_file.erl index eb5c22e..4bb8be8 100644 --- a/src/couch_file.erl +++ b/src/couch_file.erl @@ -20,6 +20,7 @@ -define(INITIAL_WAIT, 60000). -define(MONITOR_CHECK, 10000). -define(SIZE_BLOCK, 16#1000). % 4 KiB +-define(READ_AHEAD, 2 * ?SIZE_BLOCK). -record(file, { @@ -413,8 +414,10 @@ handle_call(close, _From, #file{fd=Fd}=File) -> handle_call({pread_iolist, Pos}, _From, File) -> {RawData, NextPos} = try % up to 8Kbs of read ahead - read_raw_iolist_int(File, Pos, 2 * ?SIZE_BLOCK - (Pos rem ?SIZE_BLOCK)) + read_raw_iolist_int(File, Pos, ?READ_AHEAD - (Pos rem ?SIZE_BLOCK)) catch + throw:read_beyond_eof -> + throw(read_beyond_eof); _:_ -> read_raw_iolist_int(File, Pos, 4) end, @@ -550,11 +553,16 @@ maybe_read_more_iolist(Buffer, DataSize, NextPos, File) -> {Data::iolist(), CurPos::non_neg_integer()}. read_raw_iolist_int(Fd, {Pos, _Size}, Len) -> % 0110 UPGRADE CODE read_raw_iolist_int(Fd, Pos, Len); -read_raw_iolist_int(#file{fd = Fd}, Pos, Len) -> +read_raw_iolist_int(#file{fd = Fd} = F, Pos, Len) -> BlockOffset = Pos rem ?SIZE_BLOCK, TotalBytes = calculate_total_read_len(BlockOffset, Len), - {ok, <>} = file:pread(Fd, Pos, TotalBytes), - {remove_block_prefixes(BlockOffset, RawBin), Pos + TotalBytes}. + case Pos + TotalBytes of + Size when Size > F#file.eof + ?READ_AHEAD -> + throw(read_beyond_eof); + Size -> + {ok, <>} = file:pread(Fd, Pos, TotalBytes), + {remove_block_prefixes(BlockOffset, RawBin), Size} + end. -spec extract_md5(iolist()) -> {binary(), iolist()}. extract_md5(FullIoList) -> http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/89990e18/test/couch_file_tests.erl ---------------------------------------------------------------------- diff --git a/test/couch_file_tests.erl b/test/couch_file_tests.erl index 27e0414..24edf46 100644 --- a/test/couch_file_tests.erl +++ b/test/couch_file_tests.erl @@ -24,7 +24,10 @@ setup() -> Fd. teardown(Fd) -> - ok = couch_file:close(Fd). + case is_process_alive(Fd) of + true -> ok = couch_file:close(Fd); + false -> ok + end. open_close_test_() -> { @@ -126,8 +129,18 @@ should_read_iolist(Fd) -> should_fsync(Fd) -> {"How does on test fsync?", ?_assertMatch(ok, couch_file:sync(Fd))}. -should_not_read_beyond_eof(_) -> - {"No idea how to test reading beyond EOF", ?_assert(true)}. +should_not_read_beyond_eof(Fd) -> + BigBin = list_to_binary(lists:duplicate(100000, 0)), + DoubleBin = round(byte_size(BigBin) * 2), + {ok, Pos, _Size} = couch_file:append_binary(Fd, BigBin), + {_, Filepath} = couch_file:process_info(Fd), + %% corrupt db file + {ok, Io} = file:open(Filepath, [read, write, binary]), + ok = file:pwrite(Io, Pos, <<0:1/integer, DoubleBin:31/integer>>), + file:close(Io), + unlink(Fd), + ExpectedError = {badmatch, {'EXIT', {bad_return_value, read_beyond_eof}}}, + ?_assertError(ExpectedError, couch_file:pread_binary(Fd, Pos)). should_truncate(Fd) -> {ok, 0, _} = couch_file:append_term(Fd, foo),