Skip to content

Commit 2fa4ca9

Browse files
nawariannikic
authored andcommitted
Fix bug #75785 by attempt switching endianness on Maker's Note
Different manufacturer models may come with a different endianness (motorola/intel) format. In order to avoid a big refactor and a gigantic lookup table, this commit simply attempts to switch the endianness and proceed when values are acceptable. Closes GH-5849.
1 parent dc108fe commit 2fa4ca9

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ PHP NEWS
1616
. Fixed bug #48585 (com_load_typelib holds reference, fails on second call).
1717
(cmb)
1818

19+
- Exif:
20+
. Fixed bug #75785 (Many errors from exif_read_data).
21+
(Níckolas Daniel da Silva)
22+
1923
- Gettext:
2024
. Fixed bug #70574 (Tests fail due to relying on Linux fallback behavior for
2125
gettext()). (Florian Engelhardt)

ext/exif/exif.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef unsigned char uchar;
6666

6767
#define EFREE_IF(ptr) if (ptr) efree(ptr)
6868

69-
#define MAX_IFD_NESTING_LEVEL 150
69+
#define MAX_IFD_NESTING_LEVEL 200
7070

7171
/* {{{ arginfo */
7272
ZEND_BEGIN_ARG_INFO(arginfo_exif_tagname, 0)
@@ -3173,6 +3173,23 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
31733173

31743174
NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
31753175

3176+
/* It can be that motorola_intel is wrongly mapped, let's try inverting it */
3177+
if ((2+NumDirEntries*12) > value_len) {
3178+
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Potentially invalid endianess, trying again with different endianness before imminent failure.");
3179+
3180+
ImageInfo->motorola_intel = ImageInfo->motorola_intel == 0 ? 1 : 0;
3181+
NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
3182+
}
3183+
3184+
if ((2+NumDirEntries*12) > value_len) {
3185+
exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 2 + 0x%04X*12 = 0x%04X > 0x%04X", NumDirEntries, 2+NumDirEntries*12, value_len);
3186+
return FALSE;
3187+
}
3188+
if ((dir_start - value_ptr) > value_len - (2+NumDirEntries*12)) {
3189+
exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 0x%04X > 0x%04X", (dir_start - value_ptr) + (2+NumDirEntries*12), value_len);
3190+
return FALSE;
3191+
}
3192+
31763193
switch (maker_note->offset_mode) {
31773194
case MN_OFFSET_MAKER:
31783195
offset_base = value_ptr;
@@ -3203,15 +3220,6 @@ static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * valu
32033220
break;
32043221
}
32053222

3206-
if ((2+NumDirEntries*12) > value_len) {
3207-
exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 2 + 0x%04X*12 = 0x%04X > 0x%04X", NumDirEntries, 2+NumDirEntries*12, value_len);
3208-
return FALSE;
3209-
}
3210-
if ((dir_start - value_ptr) > value_len - (2+NumDirEntries*12)) {
3211-
exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 0x%04X > 0x%04X", (dir_start - value_ptr) + (2+NumDirEntries*12), value_len);
3212-
return FALSE;
3213-
}
3214-
32153223
for (de=0;de<NumDirEntries;de++) {
32163224
size_t offset = 2 + 12 * de;
32173225
if (!exif_process_IFD_TAG(ImageInfo, dir_start + offset,

ext/exif/tests/bug75785/P1000506.JPG

34.7 KB
Loading

ext/exif/tests/bug75785/bug75785.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #75785 fix corrupt EXIF header issues; Related to mixed endianness. (Thank you @Richard Matzinger for providing the test photo)
3+
--SKIPIF--
4+
<?php if (!extension_loaded('exif')) print 'skip exif extension not available';?>
5+
--FILE--
6+
<?php
7+
$mixedEndiannessFile = dirname(__FILE__).'/P1000506.JPG';
8+
$tags = exif_read_data($mixedEndiannessFile, 'EXIF', true, false);
9+
10+
echo $tags['GPS']['GPSLatitude'][0] . PHP_EOL;
11+
echo $tags['GPS']['GPSLongitude'][0] . PHP_EOL;
12+
?>
13+
===DONE===
14+
--EXPECTF--
15+
38/1
16+
122/1
17+
===DONE===

0 commit comments

Comments
 (0)