Uploaded image for project: 'Tika'
  1. Tika
  2. TIKA-2896

NullPointerException in MimeTypesReader.releaseParser()

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.21
    • Fix Version/s: 1.22
    • Component/s: mime
    • Labels:
      None

      Description

      We have encountered a situation where the call to parser.reset() in the following code snippet results in a NullPointerException.

          private static void releaseParser(SAXParser parser) {
              try {
                  parser.reset();
              } catch (UnsupportedOperationException e) {
                  //ignore
              }
      

      releaseParser() is called in the finally block of MimeTypesReader.read()

          public void read(InputStream stream) throws IOException, MimeTypeException {
              SAXParser parser = null;
              try {
      
                  parser = acquireSAXParser();
                  parser.parse(stream, this);
              } catch (TikaException e) {
                  throw new MimeTypeException("Unable to create an XML parser", e);
              } catch (SAXException e) {
                  throw new MimeTypeException("Invalid type configuration", e);
              } finally {
                  releaseParser(parser);
              }
          }

      The parser variable will be null coming out of acquireSAXParser() if acquireSAXParser() is called on a thread that is interrupted (i.e. the InterruptedException is handled in the following code):

          private static SAXParser acquireSAXParser()
                  throws TikaException {
              while (true) {
                  SAXParser parser = null;
                  try {
                      READ_WRITE_LOCK.readLock().lock();
                      parser = SAX_PARSERS.poll(10, TimeUnit.MILLISECONDS);
                  } catch (InterruptedException e) {
                      throw new TikaException("interrupted while waiting for SAXParser", e);
                  } finally {
                      READ_WRITE_LOCK.readLock().unlock();
      
                  }
                  if (parser != null) {
                      return parser;
                  }
              }
          }

      A simple fix would be to check for null before calling releaseParser() in the finally block.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                esaunders Eamonn Saunders
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: