Hello,

We've discovered two errors in parsing of mp3 files in tika-0.9:

 * *ArrayIndexOutOfBoundsException: *

   This exception occurs in the constructors of
   org.apache.tika.parser.mp3.ID3v22Handler,
   org.apache.tika.parser.mp3.ID3v23Handler and
   org.apache.tika.parser.mp3.ID3v24Handler.
   It's connected with TCON tag and genres:

       /genre = ID3Tags.GENRES[genreID];/

   But genreID can be out of array bounds.
   The fix is the following:

       /genre = ID3Tags.GENRES[Math.min(genreID, GENRES.length - 1)];/

 * *NegativeArraySizeException *

   This exception occurs in the constructor of
   org.apache.tika.parser.mp3.ID3v2Frame.RawTag.
   It's connected with data size parameter:

       /rawSize = getInt(frameData, offset+nameLength);
       ...
       int size = rawSize * sizeMultiplier;
       size = Math.min(size, frameData.length-copyFrom);
       data = new byte[size];/

   It turns out that the rawSize for some of my mp3 files at this point
   has negative value. Maybe the content of file is incorrect.
   So, one of the possible workarounds is the following:

       /size = Math.min(size, frameData.length-copyFrom);
       *size = Math.max(size, 0);*
       data = new byte[size];/

   Maybe the problem is deeper, but such modification fixes exception.


I've attached the .diff file with my changes.
I hope it will be useful in the next patch.

--
Best regards,
  Alexander

Alexander Shcherbakov | Software Engineer | DSR Company | e-mail: alexander.sherba...@dsr-company.com <mailto:%20alexander.sherba...@dsr-company.com> | skype: shcherbakov.alexander
Index: ID3v2Frame.java
===================================================================
--- ID3v2Frame.java     (revision 11551)
+++ ID3v2Frame.java     (working copy)
@@ -304,6 +304,7 @@
             // Now data
             int copyFrom = offset+nameLength+sizeLength+flagLength;
             size = Math.min(size, frameData.length-copyFrom);
+            size = Math.max(size, 0);
             data = new byte[size];
             System.arraycopy(frameData, copyFrom, data, 0, size);
         }
Index: ID3v22Handler.java
===================================================================
--- ID3v22Handler.java  (revision 11551)
+++ ID3v22Handler.java  (working copy)
@@ -65,7 +65,7 @@
                 if (open < close) {
                     try {
                         int genreID = 
Integer.parseInt(rawGenre.substring(open+1, close));
-                        genre = ID3Tags.GENRES[genreID];
+                        genre = ID3Tags.GENRES[Math.min(genreID, GENRES.length 
- 1)];
                     } catch(NumberFormatException ignore) {
                     }
                 }
Index: ID3v23Handler.java
===================================================================
--- ID3v23Handler.java  (revision 11551)
+++ ID3v23Handler.java  (working copy)
@@ -65,7 +65,7 @@
                 if (open < close) {
                     try {
                         int genreID = 
Integer.parseInt(rawGenre.substring(open+1, close));
-                        genre = ID3Tags.GENRES[genreID];
+                        genre = ID3Tags.GENRES[Math.min(genreID, GENRES.length 
- 1)];
                     } catch(NumberFormatException ignore) {
                     }
                 }
Index: ID3v24Handler.java
===================================================================
--- ID3v24Handler.java  (revision 11551)
+++ ID3v24Handler.java  (working copy)
@@ -72,7 +72,7 @@
                 } else if (open < close) {
                     try {
                         int genreID = 
Integer.parseInt(rawGenre.substring(open+1, close));
-                        genre = ID3Tags.GENRES[genreID];
+                        genre = ID3Tags.GENRES[Math.min(genreID, GENRES.length 
- 1)];
                     } catch(NumberFormatException ignore) {
                     }
                 }

Reply via email to