Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8141644

could the uncaught exceptions be processed when loading corrupted png/jpg images?

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • 9
    • 9
    • client-libs
    • None

      Sorry, not sure if this is a bug (or just some misunderstanding concerning the exceptions handling). But could the following test case be changed in order to catch the uncaught exceptions when loading the corrupted jpg/png images?(please find some test images attached)

      import java.awt.*;
      import java.awt.event.*;
      import java.io.File;
      import java.text.AttributedCharacterIterator;

      public class UncaughtTest extends Frame implements Runnable {

          public static class ExceptionHandler implements Thread.UncaughtExceptionHandler {

              // EDT?
              public void handle(Throwable t) { printErr(Thread.currentThread().getName(), t); }

              @Override
              public void uncaughtException(Thread thread, Throwable t) { printErr(thread.getName(), t); }

              protected void printErr(String threadName, Throwable t) {
                  System.err.println("NOK: " + threadName + ", " + t.getClass().getSimpleName());
              }
          }

          private final String file;
          public UncaughtTest(String f) { file = f; }

          private void CreateUI() {
              addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) { dispose(); }
              });
              setSize(300, 300);
              setVisible(true);
          }

          @Override
          public void paint(Graphics gr) {

              Graphics2D g = (Graphics2D) gr;
              if (g != null) {

                  File imageFile = new File(file);
                  String fileName = imageFile.getAbsolutePath();

                  //if (true) g.drawString((AttributedCharacterIterator) null, 10, 10);

                  Image img = Toolkit.getDefaultToolkit().getImage(fileName);
                  g.drawImage(img, getInsets().left, getInsets().top, this);
              }
          }

          @Override
          public void run() {

              EventQueue.invokeLater(this::CreateUI);

              try {
                  Robot r = new Robot();
                  r.delay(2000);
                  this.dispose();
              } catch (Exception dummy) {
                  System.out.println("cannot create robot");
                  System.exit(1);
              }
          }

          public static void main(String[] args) {

              if (args.length != 1) { System.out.println("file?"); return; }

              ExceptionHandler eh = new ExceptionHandler();

              Thread.setDefaultUncaughtExceptionHandler(eh);
              System.setProperty("sun.awt.exception.handler", ExceptionHandler.class.getName());

              Thread t = new Thread(new UncaughtTest(args[0]));
              //t.setUncaughtExceptionHandler(eh); //?
              t.start();
          }
      }


      Please uncomment line
      //if (true) g.drawString((AttributedCharacterIterator) null, 10, 10);
      and run:
      > java UncaughtTest f427b6bee1acd1fea3ec953bc556a18a.png
      works as expected:
      NOK: AWT-EventQueue-0, NullPointerException

      then comment it out again and run. Expected (?) output:
      NOK: AWT-EventQueue-0, IllegalArgumentException
      actual output:
      Uncaught error fetching image:
      java.lang.IllegalArgumentException: Map size (0) must be >= 1
              at java.awt.image.IndexColorModel.setRGBs(IndexColorModel.java:510)
              at java.awt.image.IndexColorModel.<init>(IndexColorModel.java:176)
              at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:278)
              at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
              at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:206)
              at sun.awt.image.ImageFetcher.run(ImageFetcher.java:170)

      for other files:
      > java UncaughtTest 18288f761922f9b9b00e927eaeb9e707.png
      sun.awt.image.PNGImageDecoder$PNGException: crc corruption
          at sun.awt.image.PNGImageDecoder.getChunk(PNGImageDecoder.java:718)
          at sun.awt.image.PNGImageDecoder.getData(PNGImageDecoder.java:726)
          at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:253)
          at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
          at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:206)
          at sun.awt.image.ImageFetcher.run(ImageFetcher.java:170)

      > java UncaughtTest 3ba6af611cc5467cfdbd5566561b8478.jpg
      sun.awt.image.ImageFormatException: Invalid component ID 2 in SOS
          at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
          at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:141)
          at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
          at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:206)
          at sun.awt.image.ImageFetcher.run(ImageFetcher.java:170)
      Corrupt JPEG data: 2 extraneous bytes before marker 0xe0


      The trivial way (add nesting try/catch(Throwable) in 'paint') does not help as well.

        1. 18288f761922f9b9b00e927eaeb9e707.png
          510 kB
          Alexander Stepanov
        2. 3ba6af611cc5467cfdbd5566561b8478.jpg
          0.6 kB
          Alexander Stepanov
        3. f427b6bee1acd1fea3ec953bc556a18a.png
          0.5 kB
          Alexander Stepanov

            ssadetsky Semyon Sadetsky (Inactive)
            avstepan Alexander Stepanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: