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

Infinite loop while painting ticks on Slider with maximum=MAX_INT

XMLWordPrintable

    • b55
    • x86
    • windows_xp
    • Verified

      FULL PRODUCT VERSION :
      java version "1.6.0_01"
      Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      When a JSlider whose maximum=Integer.MAX_VALUE is added to a panel and painted, it will enter a seemingly infinite loop when painting the major ticks. I believe this is happening because of an integer overflow issue. BasicSliderUI contains the following loop:

      int value = slider.getMinimum()

      while ( value <= slider.getMaximum() ) {
         ...
         value += slider.getMajorTickSpacing();
      }

      It looks like "value" can never be greater than MAX_INT, so the loop will never terminate! A similar loop can be found for painting minor ticks.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Construct a JSlider with the following values:
      minimum=0
      maximum=2147483646
      majorTickSpacing=536870911

      setPaintLabels(true)
      setPaintTicks(true)

      Add this JSlider to a JPanel and paint it.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A JSlider whose values range from 0 to Integer.MAX_VALUE (maybe Integer.MAX_VALUE - 1) is painted with a few major tick marks (should be 4 ticks for the spacing described above).
      ACTUAL -
      Infinite loop in the painting methods.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.GridLayout;
      import javax.swing.JFrame;
      import javax.swing.JSlider;

      public class BrokenJSliderTest extends JFrame {

         JSlider slider;

         public static void main(String args[]){
            BrokenJSliderTest mainFrame = new BrokenJSliderTest();
         }

         public BrokenJSliderTest() {
            slider = new JSlider(0, Integer.MAX_VALUE - 1, 0);
            slider.setMajorTickSpacing((Integer.MAX_VALUE - 1) / 4);
            slider.setPaintTicks(true);

            this.getContentPane().setLayout(new GridLayout(1,1));
            this.getContentPane().add(slider);

            this.setSize(100,100);
            this.setVisible(true);
         }
      }
      ---------- END SOURCE ----------

            rupashka Pavel Porvatov (Inactive)
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: