From 95a3c4a81f7e58041681cfc6da30f9a016bf6b92 Mon Sep 17 00:00:00 2001 From: Pavel Porvatov Date: Thu, 26 Feb 2009 11:44:43 +0300 Subject: [PATCH] 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT Reviewed-by: malenkov --- .../javax/swing/plaf/basic/BasicSliderUI.java | 66 +++++++---- .../swing/JSlider/6794831/bug6794831.java | 105 ++++++++++++++++++ 2 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 jdk/test/javax/swing/JSlider/6794831/bug6794831.java diff --git a/jdk/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java b/jdk/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java index 864ad13264a..0a858d41c18 100644 --- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java +++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java @@ -1004,47 +1004,62 @@ public class BasicSliderUI extends SliderUI{ g.setColor(DefaultLookup.getColor(slider, this, "Slider.tickColor", Color.black)); if ( slider.getOrientation() == JSlider.HORIZONTAL ) { - g.translate( 0, tickBounds.y); + g.translate(0, tickBounds.y); - int value = slider.getMinimum(); - int xPos; + if (slider.getMinorTickSpacing() > 0) { + int value = slider.getMinimum(); - if ( slider.getMinorTickSpacing() > 0 ) { while ( value <= slider.getMaximum() ) { - xPos = xPositionForValue( value ); + int xPos = xPositionForValue(value); paintMinorTickForHorizSlider( g, tickBounds, xPos ); + + // Overflow checking + if (Integer.MAX_VALUE - slider.getMinorTickSpacing() < value) { + break; + } + value += slider.getMinorTickSpacing(); } } - if ( slider.getMajorTickSpacing() > 0 ) { - value = slider.getMinimum(); + if (slider.getMajorTickSpacing() > 0) { + int value = slider.getMinimum(); while ( value <= slider.getMaximum() ) { - xPos = xPositionForValue( value ); + int xPos = xPositionForValue(value); paintMajorTickForHorizSlider( g, tickBounds, xPos ); + + // Overflow checking + if (Integer.MAX_VALUE - slider.getMajorTickSpacing() < value) { + break; + } + value += slider.getMajorTickSpacing(); } } g.translate( 0, -tickBounds.y); - } - else { - g.translate(tickBounds.x, 0); + } else { + g.translate(tickBounds.x, 0); - int value = slider.getMinimum(); - int yPos; - - if ( slider.getMinorTickSpacing() > 0 ) { + if (slider.getMinorTickSpacing() > 0) { int offset = 0; if(!BasicGraphicsUtils.isLeftToRight(slider)) { offset = tickBounds.width - tickBounds.width / 2; g.translate(offset, 0); } - while ( value <= slider.getMaximum() ) { - yPos = yPositionForValue( value ); + int value = slider.getMinimum(); + + while (value <= slider.getMaximum()) { + int yPos = yPositionForValue(value); paintMinorTickForVertSlider( g, tickBounds, yPos ); + + // Overflow checking + if (Integer.MAX_VALUE - slider.getMinorTickSpacing() < value) { + break; + } + value += slider.getMinorTickSpacing(); } @@ -1053,15 +1068,22 @@ public class BasicSliderUI extends SliderUI{ } } - if ( slider.getMajorTickSpacing() > 0 ) { - value = slider.getMinimum(); + if (slider.getMajorTickSpacing() > 0) { if(!BasicGraphicsUtils.isLeftToRight(slider)) { g.translate(2, 0); } - while ( value <= slider.getMaximum() ) { - yPos = yPositionForValue( value ); + int value = slider.getMinimum(); + + while (value <= slider.getMaximum()) { + int yPos = yPositionForValue(value); paintMajorTickForVertSlider( g, tickBounds, yPos ); + + // Overflow checking + if (Integer.MAX_VALUE - slider.getMajorTickSpacing() < value) { + break; + } + value += slider.getMajorTickSpacing(); } @@ -1775,8 +1797,6 @@ public class BasicSliderUI extends SliderUI{ thumbMiddle = thumbLeft + halfThumbWidth; slider.setValue(valueForXPosition(thumbMiddle)); break; - default: - return; } } diff --git a/jdk/test/javax/swing/JSlider/6794831/bug6794831.java b/jdk/test/javax/swing/JSlider/6794831/bug6794831.java new file mode 100644 index 00000000000..a147987d417 --- /dev/null +++ b/jdk/test/javax/swing/JSlider/6794831/bug6794831.java @@ -0,0 +1,105 @@ +/* + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* @test + * @bug 6794831 + * @summary Infinite loop while painting ticks on Slider with maximum=MAX_INT + * @author Pavel Porvatov + @run main bug6794831 + */ + +import javax.swing.*; +import javax.swing.plaf.basic.BasicSliderUI; +import java.awt.image.BufferedImage; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +public class bug6794831 { + private final CountDownLatch countDownLatch = new CountDownLatch(1); + + public static void main(String args[]) throws InterruptedException { + new bug6794831().run(); + } + + private void run() throws InterruptedException { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + for (UIManager.LookAndFeelInfo lookAndFeelInfo : UIManager.getInstalledLookAndFeels()) { + try { + UIManager.setLookAndFeel(lookAndFeelInfo.getClassName()); + } catch (Exception e) { + fail(e.getMessage()); + } + + BufferedImage image = new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB); + + // Test 1 + JSlider slider = new JSlider(0, Integer.MAX_VALUE - 1, 0); + + slider.setMajorTickSpacing((Integer.MAX_VALUE - 1) / 4); + slider.setPaintTicks(true); + + ((BasicSliderUI) slider.getUI()).paintTicks(image.getGraphics()); + + // Test 2 + slider = new JSlider(0, Integer.MAX_VALUE - 1, 0); + + slider.setMinorTickSpacing((Integer.MAX_VALUE - 1) / 4); + slider.setPaintTicks(true); + + ((BasicSliderUI) slider.getUI()).paintTicks(image.getGraphics()); + + // Test 3 + slider = new JSlider(0, Integer.MAX_VALUE - 1, 0); + + slider.setOrientation(JSlider.VERTICAL); + slider.setMajorTickSpacing((Integer.MAX_VALUE - 1) / 4); + slider.setPaintTicks(true); + + ((BasicSliderUI) slider.getUI()).paintTicks(image.getGraphics()); + + // Test 4 + slider = new JSlider(0, Integer.MAX_VALUE - 1, 0); + + slider.setOrientation(JSlider.VERTICAL); + slider.setMinorTickSpacing((Integer.MAX_VALUE - 1) / 4); + slider.setPaintTicks(true); + + ((BasicSliderUI) slider.getUI()).paintTicks(image.getGraphics()); + + countDownLatch.countDown(); + } + } + }); + + if (countDownLatch.await(3000, TimeUnit.MILLISECONDS)) { + System.out.println("bug6794831 passed"); + } else { + fail("bug6794831 failed"); + } + } + + private static void fail(String msg) { + throw new RuntimeException(msg); + } +}