Skip to content

Fix Division By Zero Warning for Focal Length #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/PHPExif/Mapper/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ public function mapRawData(array $data)
break;
case self::FOCALLENGTH:
$parts = explode('/', $value);
$value = (int) reset($parts) / (int) end($parts);
// Avoid division by zero if focal length is invalid
if (end($parts) == '0') {
$value = 0;
} else {
$value = (int) reset($parts) / (int) end($parts);
}
break;
case self::XRESOLUTION:
case self::YRESOLUTION:
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPExif/Mapper/NativeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ public function testMapRawDataCorrectlyFormatsFocalLength()
$this->assertEquals(6, reset($mapped));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
*/
public function testMapRawDataCorrectlyFormatsFocalLengthDivisionByZero()
{
$rawData = array(
\PHPExif\Mapper\Native::FOCALLENGTH => '1/0',
);

$mapped = $this->mapper->mapRawData($rawData);

$this->assertEquals(0, reset($mapped));
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::mapRawData
Expand Down