OSDN Git Service

DO NOT MERGE) ExifInterface: Make saveAttributes throw an exception before change
authorSungsoo <sungsoo@google.com>
Thu, 8 Sep 2016 07:04:44 +0000 (16:04 +0900)
committergitbuildkicker <android-build@google.com>
Tue, 27 Sep 2016 22:58:46 +0000 (15:58 -0700)
ExifInterface object can be created with a unsupported file format.
If saveAttribute is called with an unsupported file format, ExifInterface
makes the file corrupted. This CL prevents those cases by throwing
an exception before making any change on the file.

Bug: 30936376
Change-Id: I915f56b00ec9422b53591ac5534e070a1d6798e6
(cherry picked from commit 2ee53c82cc8c8b2e76a19910074672f6204a5d63)

media/java/android/media/ExifInterface.java

index 74bb55b..a2ccdc8 100644 (file)
@@ -1039,6 +1039,7 @@ public class ExifInterface {
     private int mThumbnailOffset;
     private int mThumbnailLength;
     private byte[] mThumbnailBytes;
+    private boolean mIsSupportedFile;
 
     // Pattern to check non zero timestamp
     private static final Pattern sNonZeroTimePattern = Pattern.compile(".*[1-9].*");
@@ -1337,9 +1338,11 @@ public class ExifInterface {
         try {
             InputStream in = new FileInputStream(mFilename);
             getJpegAttributes(in);
+            mIsSupportedFile = true;
         } catch (IOException e) {
             // Ignore exceptions in order to keep the compatibility with the old versions of
             // ExifInterface.
+            mIsSupportedFile = false;
             Log.w(TAG, "Invalid image.", e);
         } finally {
             addDefaultValuesForCompatibility();
@@ -1368,6 +1371,10 @@ public class ExifInterface {
      * and make a single call rather than multiple calls for each attribute.
      */
     public void saveAttributes() throws IOException {
+        if (!mIsSupportedFile) {
+            throw new UnsupportedOperationException(
+                    "ExifInterface only supports saving attributes on JPEG formats.");
+        }
         // Keep the thumbnail in memory
         mThumbnailBytes = getThumbnail();