Skip to content

Commit 82a05c8

Browse files
kevinbackhousekmilos
authored andcommitted
Better bounds checking to fix GHSA-496f-x7cq-cq39
1 parent cfd2856 commit 82a05c8

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

src/epsimage.cpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,17 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
239239
uint32_t posTiff = 0;
240240
uint32_t sizeTiff = 0;
241241

242+
ErrorCode errcode = write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData;
243+
242244
// check for DOS EPS
243245
const bool dosEps =
244246
(size >= dosEpsSignature.size() && memcmp(data, dosEpsSignature.data(), dosEpsSignature.size()) == 0);
245247
if (dosEps) {
246248
#ifdef DEBUG
247249
EXV_DEBUG << "readWriteEpsMetadata: Found DOS EPS signature\n";
248250
#endif
249-
if (size < 30) {
250-
#ifndef SUPPRESS_WARNINGS
251-
EXV_WARNING << "Premature end of file after DOS EPS signature.\n";
252-
#endif
253-
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
254-
}
251+
252+
enforce(size >= 30, errcode);
255253
posEps = getULong(data + 4, littleEndian);
256254
posEndEps = getULong(data + 8, littleEndian) + posEps;
257255
posWmf = getULong(data + 12, littleEndian);
@@ -283,29 +281,13 @@ void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList
283281
if (write)
284282
throw Error(ErrorCode::kerImageWriteFailed);
285283
}
286-
if (posEps < 30 || posEndEps > size) {
287-
#ifndef SUPPRESS_WARNINGS
288-
EXV_WARNING << "DOS EPS file has invalid position (" << posEps << ") or size (" << (posEndEps - posEps)
289-
<< ") for EPS section.\n";
290-
#endif
291-
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
292-
}
293-
if (sizeWmf != 0 && (posWmf < 30 || posWmf + sizeWmf > size)) {
294-
#ifndef SUPPRESS_WARNINGS
295-
EXV_WARNING << "DOS EPS file has invalid position (" << posWmf << ") or size (" << sizeWmf
296-
<< ") for WMF section.\n";
297-
#endif
298-
if (write)
299-
throw Error(ErrorCode::kerImageWriteFailed);
300-
}
301-
if (sizeTiff != 0 && (posTiff < 30 || posTiff + sizeTiff > size)) {
302-
#ifndef SUPPRESS_WARNINGS
303-
EXV_WARNING << "DOS EPS file has invalid position (" << posTiff << ") or size (" << sizeTiff
304-
<< ") for TIFF section.\n";
305-
#endif
306-
if (write)
307-
throw Error(ErrorCode::kerImageWriteFailed);
308-
}
284+
enforce(30 <= posEps, errcode);
285+
enforce(sizeWmf == 0 || 30 <= posWmf, errcode);
286+
enforce(sizeTiff == 0 || 30 <= posTiff, errcode);
287+
288+
enforce(posEps <= posEndEps && posEndEps <= size, errcode);
289+
enforce(posWmf <= size && sizeWmf <= size - posWmf, errcode);
290+
enforce(posTiff <= size && sizeTiff <= size - posTiff, errcode);
309291
}
310292

311293
// check first line

0 commit comments

Comments
 (0)