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
8 changes: 6 additions & 2 deletions cpp/src/remote_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstddef>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory>
#include <regex>
#include <sstream>
Expand Down Expand Up @@ -173,8 +174,11 @@ std::size_t get_file_size_using_head_impl(RemoteEndpoint& endpoint, std::string
void setup_range_request_impl(CurlHandle& curl, std::size_t file_offset, std::size_t size)
{
KVIKIO_EXPECT(size > 0, "cannot create a zero-size range request", std::invalid_argument);
std::string const byte_range =
std::to_string(file_offset) + "-" + std::to_string(file_offset + size - 1);
KVIKIO_EXPECT(size <= std::numeric_limits<std::size_t>::max() - file_offset,
"cannot create a range request with overflowing end offset",
std::invalid_argument);
auto const end_offset = file_offset + size - 1;
std::string const byte_range = std::to_string(file_offset) + "-" + std::to_string(end_offset);
curl.setopt(CURLOPT_RANGE, byte_range.c_str());
}

Expand Down
13 changes: 13 additions & 0 deletions cpp/tests/test_remote_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <stdexcept>
Expand All @@ -15,6 +16,7 @@
#include <gtest/gtest.h>
#include <kvikio/hdfs.hpp>
#include <kvikio/remote_handle.hpp>
#include <kvikio/shim/libcurl.hpp>

#include "utils/env.hpp"

Expand Down Expand Up @@ -173,6 +175,17 @@ TEST_F(RemoteHandleTest, read_zero_size_returns_without_range_request)
EXPECT_EQ(endpoint_ptr->range_request_calls, 0);
}

TEST_F(RemoteHandleTest, range_request_rejects_overflowing_end_offset)
{
kvikio::HttpEndpoint endpoint{"http://example.com/test"};
auto curl = create_curl_handle();

auto constexpr file_offset = std::numeric_limits<std::size_t>::max() - 1;
auto constexpr size = std::size_t{4};
EXPECT_THAT([&] { endpoint.setup_range_request(curl, file_offset, size); },
ThrowsMessage<std::invalid_argument>(HasSubstr("overflowing end offset")));
}

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