From b2808bcf275ea3a25b748e5688273c3402b3bb04 Mon Sep 17 00:00:00 2001 From: Kevin Backhouse Date: Sat, 17 Jun 2023 12:55:43 +0100 Subject: [PATCH 1/3] Allow c_data() to return zero byte buffer. --- src/types.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.cpp b/src/types.cpp index dea5c94b73..6559e20f4d 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -176,7 +176,7 @@ const byte* Exiv2::DataBuf::c_data(size_t offset) const { if (pData_.empty()) { return nullptr; } - if (offset >= pData_.size()) { + if (offset > pData_.size()) { throw std::out_of_range("Overflow in Exiv2::DataBuf::c_data"); } return &pData_[offset]; From ec5fcd7f87b8bf00386f031aeaa5bf885b422b28 Mon Sep 17 00:00:00 2001 From: Kevin Backhouse Date: Sat, 17 Jun 2023 13:10:07 +0100 Subject: [PATCH 2/3] Fix unit test failure. --- unitTests/test_types.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unitTests/test_types.cpp b/unitTests/test_types.cpp index 5cfd80d5e9..b13bbc6825 100644 --- a/unitTests/test_types.cpp +++ b/unitTests/test_types.cpp @@ -49,8 +49,8 @@ TEST(DataBuf, canBeConstructedFromExistingData) { TEST(DataBuf, tryingToAccessTooFarElementThrows) { const std::array 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) { From 5f9ecada11ec9b3a599cd97872bcd73dab066c29 Mon Sep 17 00:00:00 2001 From: Kevin Backhouse Date: Sat, 17 Jun 2023 13:39:15 +0100 Subject: [PATCH 3/3] Avoid triggering assertion in std::vector::operator[] --- src/types.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.cpp b/src/types.cpp index 6559e20f4d..1c6963cda4 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -173,7 +173,7 @@ 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()) {