Skip to content

Commit 8ab7800

Browse files
committed
Remove 2nd param from Photoshop::isIrb() since it is always hardcoded
1 parent 7366a64 commit 8ab7800

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

include/exiv2/jpgimage.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ struct EXIV2API Photoshop {
2727
static constexpr uint16_t preview_ = 0x040c; //!< %Photoshop preview marker
2828

2929
/// @brief Checks an IRB
30-
/// @param pPsData Existing IRB buffer
31-
/// @param sizePsData Size of the IRB buffer
32-
/// @return true if the IRB marker is known and the buffer is big enough to check this;<BR>
33-
/// false otherwise
34-
static bool isIrb(const byte* pPsData, size_t sizePsData);
30+
/// @param pPsData Existing IRB buffer. It is expected to be of size 4.
31+
/// @return true if the IRB marker is known
32+
/// @todo This should be an implementation detail and not exposed in the API. An attacker could try to pass
33+
/// a smaller buffer or null pointer.
34+
static bool isIrb(const byte* pPsData);
3535

3636
/// @brief Validates all IRBs
3737
/// @param pPsData Existing IRB buffer

src/jpgimage.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ static inline bool inRange2(int value, int lo1, int hi1, int lo2, int hi2) {
3535
return inRange(lo1, value, hi1) || inRange(lo2, value, hi2);
3636
}
3737

38-
bool Photoshop::isIrb(const byte* pPsData, size_t sizePsData) {
39-
if (sizePsData < 4)
38+
bool Photoshop::isIrb(const byte* pPsData) {
39+
if (pPsData == nullptr) {
4040
return false;
41+
}
42+
/// \todo check if direct array comparison is faster than a call to memcmp
4143
return std::any_of(irbId_.begin(), irbId_.end(), [pPsData](auto id) { return memcmp(pPsData, id, 4) == 0; });
4244
}
4345

@@ -69,7 +71,7 @@ int Photoshop::locateIrb(const byte* pPsData, size_t sizePsData, uint16_t psTag,
6971
std::cerr << "Photoshop::locateIrb: ";
7072
#endif
7173
// Data should follow Photoshop format, if not exit
72-
while (position <= sizePsData - 12 && isIrb(pPsData + position, 4)) {
74+
while (position <= sizePsData - 12 && isIrb(pPsData + position)) {
7375
const byte* hrd = pPsData + position;
7476
position += 4;
7577
uint16_t type = getUShort(pPsData + position, bigEndian);

src/psdimage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void PsdImage::readMetadata() {
175175
throw Error(ErrorCode::kerNotAnImage, "Photoshop");
176176
}
177177

178-
if (!Photoshop::isIrb(buf, 4)) {
178+
if (!Photoshop::isIrb(buf)) {
179179
break; // bad resource type
180180
}
181181
uint16_t resourceId = getUShort(buf + 4, bigEndian);
@@ -413,7 +413,7 @@ void PsdImage::doWriteMetadata(BasicIo& outIo) {
413413
// read resource type and ID
414414
uint32_t resourceType = getULong(buf, bigEndian);
415415

416-
if (!Photoshop::isIrb(buf, 4)) {
416+
if (!Photoshop::isIrb(buf)) {
417417
throw Error(ErrorCode::kerNotAnImage, "Photoshop"); // bad resource type
418418
}
419419
uint16_t resourceId = getUShort(buf + 4, bigEndian);

unitTests/test_Photoshop.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ constexpr std::array validMarkers{"8BIM", "AgHg", "DCSR", "PHUT"};
1212

1313
TEST(Photoshop_isIrb, returnsTrueWithValidMarkers) {
1414
for (const auto& marker : validMarkers) {
15-
ASSERT_TRUE(Photoshop::isIrb(reinterpret_cast<const byte*>(marker), 4));
15+
ASSERT_TRUE(Photoshop::isIrb(reinterpret_cast<const byte*>(marker)));
1616
}
1717
}
1818

1919

2020
TEST(Photoshop_isIrb, returnsFalseWithInvalidMarkers) {
21-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("7BIM"), 4));
22-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("AGHg"), 4));
23-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("dcsr"), 4));
24-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("LUIS"), 4));
21+
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("7BIM")));
22+
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("AGHg")));
23+
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("dcsr")));
24+
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("LUIS")));
2525
}
2626

27-
TEST(Photoshop_isIrb, returnsFalseWithInvalidSize) {
28-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("8BIM"), 3));
29-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("8BIM"), 0));
30-
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("8BIM"), 5));
27+
TEST(Photoshop_isIrb, returnsFalseWithNullPointer) {
28+
ASSERT_FALSE(Photoshop::isIrb(nullptr));
29+
}
30+
31+
/// \note probably this is not safe and we need to remove it
32+
TEST(Photoshop_isIrb, returnsFalseWithShorterMarker) {
33+
ASSERT_FALSE(Photoshop::isIrb(reinterpret_cast<const byte *>("8BI")));
3134
}

0 commit comments

Comments
 (0)