Skip to content

Commit

Permalink
Read(): check file size
Browse files Browse the repository at this point in the history
  • Loading branch information
madsbk committed Sep 19, 2024
1 parent c87ad73 commit d99b1eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cpp/include/kvikio/remote_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ class RemoteHandle {
{
KVIKIO_NVTX_FUNC_RANGE("RemoteHandle::read()", size);

if (file_offset + size > _nbytes) {
std::stringstream ss;
ss << "cannot read " << file_offset << "+" << size << " bytes into a " << _nbytes
<< " bytes file (" << _url << ")";
throw std::invalid_argument(ss.str());
}

auto curl = create_curl_handle();

curl.setopt(CURLOPT_URL, _url.c_str());
Expand Down
20 changes: 18 additions & 2 deletions python/kvikio/tests/test_http_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_read(http_server, tmpdir, xp, size, nthreads, tasksize):
with kvikio.RemoteFile(f"{http_server}/a") as f:
assert f.nbytes() == a.nbytes
b = xp.empty_like(a)
assert f.read(buf=b) == a.nbytes
assert f.read(b) == a.nbytes
xp.testing.assert_array_equal(a, b)


Expand All @@ -76,5 +76,21 @@ def test_large_read(http_server, tmpdir, xp, nthreads):
with kvikio.RemoteFile(f"{http_server}/a") as f:
assert f.nbytes() == a.nbytes
b = xp.empty_like(a)
assert f.read(buf=b) == a.nbytes
assert f.read(b) == a.nbytes
xp.testing.assert_array_equal(a, b)


def test_error_too_small_file(http_server, tmpdir, xp):
a = xp.arange(10, dtype="uint8")
b = xp.empty(100, dtype="uint8")
a.tofile(tmpdir / "a")
with kvikio.RemoteFile(f"{http_server}/a") as f:
assert f.nbytes() == a.nbytes
with pytest.raises(
ValueError, match=r"cannot read 0\+100 bytes into a 10 bytes file"
):
f.read(b)
with pytest.raises(
ValueError, match=r"cannot read 100\+5 bytes into a 10 bytes file"
):
f.read(b, size=5, file_offset=100)

0 comments on commit d99b1eb

Please sign in to comment.