Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow c_data() to return zero byte buffer #2654

Merged
merged 3 commits into from
Jun 18, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ byte* Exiv2::DataBuf::data(size_t offset) {
}

const byte* Exiv2::DataBuf::c_data(size_t offset) const {
if (pData_.empty()) {
if (pData_.empty() || offset == pData_.size()) {
return nullptr;
}
if (offset >= pData_.size()) {
if (offset > pData_.size()) {
throw std::out_of_range("Overflow in Exiv2::DataBuf::c_data");
}
return &pData_[offset];
Expand Down
4 changes: 2 additions & 2 deletions unitTests/test_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ TEST(DataBuf, canBeConstructedFromExistingData) {
TEST(DataBuf, tryingToAccessTooFarElementThrows) {
const std::array<byte, 4> data{'h', 'o', 'l', 'a'};
DataBuf instance(data.data(), data.size());
ASSERT_THROW([[maybe_unused]] auto d = instance.data(4), std::out_of_range);
ASSERT_THROW([[maybe_unused]] auto d = instance.c_data(4), std::out_of_range);
ASSERT_THROW([[maybe_unused]] auto d = instance.data(5), std::out_of_range);
ASSERT_THROW([[maybe_unused]] auto d = instance.c_data(5), std::out_of_range);
}

TEST(DataBuf, readUintFunctionsWorksOnExistingData) {
Expand Down