From 64eeef77ee1419e82c8e7deabbd5001d43ab12a1 Mon Sep 17 00:00:00 2001 From: Chong Zhang Date: Wed, 9 May 2018 18:52:01 -0700 Subject: [PATCH] Fix failure in ExifInterface on certain HEIF files Some sniffers in MediaExtractor might issue reads past the end of the file. This results in IOException in our MediaDataSource. The exception itself is not fatal, but need to make sure MediaDataSource state is kept in sync otherwise the other extractors can't continue. Bug: 79497983 Test: test the ExifInterface on the attached file in bug, as well as selected HEIF files from other devices, Exif should be extracted without error in the log. Change-Id: I5c8f597b8ef14288e3465670b202adc93c09e6ea --- media/java/android/media/ExifInterface.java | 25 +++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index c486e68d361a2..aa457092ca3ff 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -2540,19 +2540,20 @@ public class ExifInterface { if (position < 0) { return -1; } - if (mPosition != position) { - in.seek(position); - mPosition = position; - } + try { + if (mPosition != position) { + in.seek(position); + mPosition = position; + } - int bytesRead = in.read(buffer, offset, size); - if (bytesRead < 0) { - mPosition = -1; // need to seek on next read - return -1; - } - - mPosition += bytesRead; - return bytesRead; + int bytesRead = in.read(buffer, offset, size); + if (bytesRead >= 0) { + mPosition += bytesRead; + return bytesRead; + } + } catch (IOException e) {} + mPosition = -1; // need to seek on next read + return -1; } @Override