-
Bug
-
Resolution: Fixed
-
P3
-
6u1
-
b73
-
generic
-
solaris_7
The j2se spec 4524710 (spec for java.awt.FontMetrics methods is silent when args are incorrect) has been fixed in j2se 1.6 and now the spec for FontMetrics.charsWidth(char[], int, int) states that method will throw IndexOutOfBoundsException - if the off and len arguments index characters outside the bounds of the data.
But when running the following code on jdk 1.6 on Soloari (I didn't try on linux and window), the test doesn't throw expected IndexOutOfBoundException when the length is invalid.
See the following code:
===============================================================================
import java.awt.*;
public class MyFontMetricsTest {
public void test1() {
char[] data = {'t', 'e', 's', 't', 'a', 'r', 'r', 'a', 'y'};
Container container = new Container();
Font font = new Font("Dialog", Font.PLAIN, 12);
FontMetrics fm = container.getFontMetrics(font);
try {
fm.charsWidth(data, 1, -1);
System.out.println("Failed. IndexOutOfBoundsExceptionis expected.");
} catch (IndexOutOfBoundsException e) {
System.out.println("OK, throw IndexOutOfBoundsException");
}
}
public static void main(String[] args) {
MyFontMetricsTest t = new MyFontMetricsTest();
t.test1();
}
}
=============================================================================
This seems an implementation bug as it doesn't consistent with the spec.
Just want to add that other method such as FontMetrics.bytesWidth(byte[], int, int) behave consistent with the spec. The spec for FontMetrics.bytesWidth(byte[], int, int)
also states that method will throw IndexOutOfBoundsException - if the off and len arguments index bytes outside the bounds of the data array, and jdk 1.4, jdk 1.5 and jdk 1.6 all throw correct exception in the case when the length is invalid.
But when running the following code on jdk 1.6 on Soloari (I didn't try on linux and window), the test doesn't throw expected IndexOutOfBoundException when the length is invalid.
See the following code:
===============================================================================
import java.awt.*;
public class MyFontMetricsTest {
public void test1() {
char[] data = {'t', 'e', 's', 't', 'a', 'r', 'r', 'a', 'y'};
Container container = new Container();
Font font = new Font("Dialog", Font.PLAIN, 12);
FontMetrics fm = container.getFontMetrics(font);
try {
fm.charsWidth(data, 1, -1);
System.out.println("Failed. IndexOutOfBoundsExceptionis expected.");
} catch (IndexOutOfBoundsException e) {
System.out.println("OK, throw IndexOutOfBoundsException");
}
}
public static void main(String[] args) {
MyFontMetricsTest t = new MyFontMetricsTest();
t.test1();
}
}
=============================================================================
This seems an implementation bug as it doesn't consistent with the spec.
Just want to add that other method such as FontMetrics.bytesWidth(byte[], int, int) behave consistent with the spec. The spec for FontMetrics.bytesWidth(byte[], int, int)
also states that method will throw IndexOutOfBoundsException - if the off and len arguments index bytes outside the bounds of the data array, and jdk 1.4, jdk 1.5 and jdk 1.6 all throw correct exception in the case when the length is invalid.
- relates to
-
JDK-4524710 spec for java.awt.FontMetrics methods is silent when args are incorrect
-
- Closed
-