Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cpp/src/remote_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -179,6 +179,11 @@ void setup_range_request_impl(CurlHandle& curl, std::size_t file_offset, std::si
curl.setopt(CURLOPT_RANGE, byte_range.c_str());
}

bool is_read_out_of_bounds(std::size_t file_offset, std::size_t size, std::size_t nbytes) noexcept
{
return file_offset > nbytes || size > nbytes - file_offset;
}

/**
* @brief Whether the given URL is compatible with the S3 endpoint (including the credential-based
* access and presigned URL) which uses HTTP/HTTPS.
Expand Down Expand Up @@ -762,7 +767,7 @@ std::size_t RemoteHandle::read(void* buf, std::size_t size, std::size_t file_off

if (size == 0) { return 0; }

if (file_offset + size > _nbytes) {
if (is_read_out_of_bounds(file_offset, size, _nbytes)) {
std::stringstream ss;
ss << "cannot read " << file_offset << "+" << size << " bytes into a " << _nbytes
<< " bytes file (" << _endpoint->str() << ")";
Expand Down Expand Up @@ -858,7 +863,7 @@ std::future<std::size_t> RemoteHandle::pread(void* buf,
"device-memory buffers.",
std::invalid_argument);
KVIKIO_EXPECT(task_size > 0, "`task_size` must be positive", std::invalid_argument);
if (file_offset + size > _nbytes) {
if (is_read_out_of_bounds(file_offset, size, _nbytes)) {
std::stringstream ss;
ss << "cannot read " << file_offset << "+" << size << " bytes into a " << _nbytes
<< " bytes file (" << _endpoint->str() << ")";
Expand Down
18 changes: 17 additions & 1 deletion cpp/tests/test_remote_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <stdexcept>
Expand Down Expand Up @@ -173,6 +174,21 @@ TEST_F(RemoteHandleTest, read_zero_size_returns_without_range_request)
EXPECT_EQ(endpoint_ptr->range_request_calls, 0);
}

TEST_F(RemoteHandleTest, read_overflowing_range_throws_without_range_request)
{
auto endpoint = std::make_unique<CountingEndpoint>();
auto* endpoint_ptr = endpoint.get();
kvikio::RemoteHandle remote_handle(std::move(endpoint), endpoint_ptr->file_size);

std::vector<char> output(1);
auto constexpr file_offset = std::numeric_limits<std::size_t>::max() - 1;
auto constexpr size = std::size_t{4};
EXPECT_THAT([&] { remote_handle.read(output.data(), size, file_offset); },
ThrowsMessage<std::invalid_argument>(HasSubstr("cannot read ")));
EXPECT_EQ(endpoint_ptr->setopt_calls, 0);
EXPECT_EQ(endpoint_ptr->range_request_calls, 0);
}

TEST_F(RemoteHandleTest, test_s3_url)
{
kvikio::test::EnvVarContext env_var_ctx{{"AWS_DEFAULT_REGION", "my_aws_default_region"},
Expand Down