From c2916968aa8e470b3d62e36e752e1567e69972f3 Mon Sep 17 00:00:00 2001 From: Jaesung Chung Date: Sun, 20 Mar 2016 01:21:18 -0700 Subject: [PATCH] ExifInterface: fix logs - Give more explanations on dealing with files failed to process. - Log and return immediately when handling lost JPEG files. - Minor fixes in logging. Bug: 27583378, Bug: 27587980 Change-Id: Id9a3d137e5f173fa0e6313402e5dc482a5efd57c --- media/java/android/media/ExifInterface.java | 30 ++++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index ed358d3d270a0..042a7ee775944 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -830,7 +830,9 @@ public class ExifInterface { } catch (IOException e) { // Ignore exceptions in order to keep the compatibility with the old versions of // ExifInterface. - Log.w(TAG, "Invalid JPEG", e); + Log.w(TAG, "Invalid JPEG: ExifInterface got an unsupported image format file" + + "(ExifInterface supports JPEG and some RAW image formats only) " + + "or a corrupted JPEG file to ExifInterface.", e); } if (DEBUG) { @@ -1189,6 +1191,10 @@ public class ExifInterface { ++bytesRead; while (true) { marker = dataInputStream.readByte(); + if (marker == -1) { + Log.w(TAG, "Reading JPEG has ended unexpectedly"); + break; + } if (marker != MARKER) { throw new IOException("Invalid marker:" + Integer.toHexString(marker & 0xff)); } @@ -1207,7 +1213,8 @@ public class ExifInterface { int length = dataInputStream.readUnsignedShort() - 2; bytesRead += 2; if (DEBUG) { - Log.d(TAG, "JPEG segment: " + marker + " (length: " + (length + 2) + ")"); + Log.d(TAG, "JPEG segment: " + Integer.toHexString(marker & 0xff) + " (length: " + + (length + 2) + ")"); } if (length < 0) { throw new IOException("Invalid length"); @@ -1270,7 +1277,9 @@ public class ExifInterface { case MARKER_SOF13: case MARKER_SOF14: case MARKER_SOF15: { - dataInputStream.skipBytes(1); + if (dataInputStream.skipBytes(1) != 1) { + throw new IOException("Invalid SOFx"); + } setAttribute("ImageLength", String.valueOf(dataInputStream.readUnsignedShort())); setAttribute("ImageWidth", String.valueOf(dataInputStream.readUnsignedShort())); @@ -1285,7 +1294,9 @@ public class ExifInterface { if (length < 0) { throw new IOException("Invalid length"); } - dataInputStream.skipBytes(length); + if (dataInputStream.skipBytes(length) != length) { + throw new IOException("Invalid JPEG segment"); + } bytesRead += length; } } @@ -1317,10 +1328,15 @@ public class ExifInterface { byte[] bytes = new byte[4096]; while (true) { - if (dataInputStream.readByte() != MARKER) { + byte marker = dataInputStream.readByte(); + if (marker == -1) { + Log.w(TAG, "Reading JPEG has ended unexpectedly"); + break; + } + if (marker != MARKER) { throw new IOException("Invalid marker"); } - byte marker = dataInputStream.readByte(); + marker = dataInputStream.readByte(); switch (marker) { case MARKER_APP1: { int length = dataInputStream.readUnsignedShort() - 2; @@ -1644,7 +1660,7 @@ public class ExifInterface { String tagName = (String) sExifTagMapsForReading[hint].get(tagNumber); if (DEBUG) { - Log.d(TAG, String.format("hint: %d, tagNumber: %d, tagName: %s, dataFormat: %d," + + Log.d(TAG, String.format("hint: %d, tagNumber: %d, tagName: %s, dataFormat: %d, " + "numberOfComponents: %d", hint, tagNumber, tagName, dataFormat, numberOfComponents)); }