Skip to content

Commit d44352e

Browse files
Handle corrupt files (#236)
1 parent da5ad74 commit d44352e

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

rawpy/_rawpy.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ IF UNAME_SYSNAME == "Windows":
201201
int unpack() nogil
202202
int unpack_thumb() nogil
203203
int COLOR(int row, int col) nogil
204+
int error_count() nogil
204205
int dcraw_process() nogil
205206
libraw_processed_image_t* dcraw_make_mem_image(int *errcode) nogil
206207
libraw_processed_image_t* dcraw_make_mem_thumb(int *errcode) nogil
@@ -218,6 +219,7 @@ ELSE:
218219
int unpack() nogil
219220
int unpack_thumb() nogil
220221
int COLOR(int row, int col)
222+
int error_count() nogil
221223
int dcraw_process() nogil
222224
libraw_processed_image_t* dcraw_make_mem_image(int *errcode) nogil
223225
libraw_processed_image_t* dcraw_make_mem_thumb(int *errcode) nogil
@@ -953,6 +955,12 @@ cdef class RawPy:
953955
else:
954956
raise LibRawNonFatalError(errstr)
955957

958+
cdef int error_count
959+
with nogil:
960+
error_count = self.p.error_count()
961+
if error_count > 0:
962+
raise LibRawDataError("Data error or unsupported file format")
963+
956964
class DemosaicAlgorithm(Enum):
957965
"""
958966
Identifiers for demosaic algorithms.

test/M0054341_01_00005.cr2

8.58 MB
Binary file not shown.

test/test_basic.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
# Kodak DC50 with special characters in filename
3636
raw6TestPath = os.path.join(thisDir, 'RAW_KODAK_DC50_é.KDC')
3737

38+
# CR2 file with corrupted data
39+
raw7TestPath = os.path.join(thisDir, 'M0054341_01_00005.cr2')
40+
3841
def testVersion():
3942
print('using libraw', rawpy.libraw_version)
4043
pprint(rawpy.flags)
@@ -267,7 +270,14 @@ def testLibRawOutOfOrderCallError():
267270
with pytest.raises(rawpy.LibRawOutOfOrderCallError):
268271
raw = rawpy.RawPy()
269272
raw.unpack()
270-
273+
274+
def testCorruptFile():
275+
im = rawpy.imread(raw7TestPath)
276+
with pytest.raises(rawpy.LibRawDataError):
277+
im.postprocess()
278+
with pytest.raises(rawpy.LibRawDataError):
279+
im.extract_thumb()
280+
271281
def save(path, im):
272282
# both imageio and skimage currently save uint16 images with 180deg rotation
273283
# as they both use freeimage and this has some weird internal formats
@@ -289,4 +299,4 @@ def print_stats(rgb):
289299
#testFileOpenAndPostProcess()
290300
#testBadPixelRepair()
291301
testFindBadPixelsNikonD4()
292-
302+
testCorruptFile()

0 commit comments

Comments
 (0)