Throwing when trying to access TooFar elements in DataBuf
This commit is contained in:
parent
941017d281
commit
4a4a8c544c
@ -172,9 +172,8 @@ int Exiv2::DataBuf::cmpBytes(size_t offset, const void* buf, size_t bufsize) con
|
||||
}
|
||||
|
||||
byte* Exiv2::DataBuf::data(size_t offset) {
|
||||
/// \todo this first check should be for <= offset
|
||||
if (pData_.size() < offset) {
|
||||
throw std::overflow_error("Overflow in Exiv2::DataBuf::c_data");
|
||||
if (offset >= pData_.size()) {
|
||||
throw std::out_of_range("Overflow in Exiv2::DataBuf::c_data");
|
||||
}
|
||||
if (pData_.empty() || pData_.size() == offset) {
|
||||
return nullptr;
|
||||
@ -183,9 +182,8 @@ byte* Exiv2::DataBuf::data(size_t offset) {
|
||||
}
|
||||
|
||||
const byte* Exiv2::DataBuf::c_data(size_t offset) const {
|
||||
/// \todo this first check should be for <= offset
|
||||
if (pData_.size() < offset) {
|
||||
throw std::overflow_error("Overflow in Exiv2::DataBuf::c_data");
|
||||
if (offset >= pData_.size()) {
|
||||
throw std::out_of_range("Overflow in Exiv2::DataBuf::c_data");
|
||||
}
|
||||
if (pData_.empty() || pData_.size() == offset) {
|
||||
return nullptr;
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <exiv2/types.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
using namespace Exiv2;
|
||||
|
||||
// More info about tm : http://www.cplusplus.com/reference/ctime/tm/
|
||||
@ -24,10 +28,9 @@ TEST(ExivTime, doesNotGetTimeWithBadFormedString) {
|
||||
ASSERT_EQ(1, exifTime("007:a5:24 aa:bb:cc", &tmInstance));
|
||||
}
|
||||
|
||||
TEST(DataBuf, pointsToNullByDefault) {
|
||||
TEST(DataBuf, defaultInstanceIsEmpty) {
|
||||
DataBuf instance;
|
||||
ASSERT_EQ(nullptr, instance.c_data());
|
||||
ASSERT_EQ(0, instance.size());
|
||||
ASSERT_TRUE(instance.empty());
|
||||
}
|
||||
|
||||
TEST(DataBuf, allocatesDataWithNonEmptyConstructor) {
|
||||
@ -36,6 +39,18 @@ TEST(DataBuf, allocatesDataWithNonEmptyConstructor) {
|
||||
ASSERT_EQ(5, instance.size());
|
||||
}
|
||||
|
||||
TEST(DataBuf, canBeConstructedFromExistingData) {
|
||||
const std::array<byte, 4> data {'h', 'o', 'l', 'a'};
|
||||
DataBuf instance(data.data(), data.size());
|
||||
ASSERT_TRUE(std::equal(data.begin(), data.end(), instance.begin()));
|
||||
}
|
||||
|
||||
TEST(DataBuf, tryingToAccessTooFarElementThrows) {
|
||||
const std::array<byte, 4> data {'h', 'o', 'l', 'a'};
|
||||
DataBuf instance(data.data(), data.size());
|
||||
ASSERT_THROW(instance.data(4), std::out_of_range);
|
||||
}
|
||||
|
||||
// Test methods like DataBuf::read_uint32 and DataBuf::write_uint32.
|
||||
TEST(DataBuf, read_write_endianess) {
|
||||
DataBuf buf(4 + 1 + 2 + 4 + 8);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user