Skip to content

Commit becc35f

Browse files
author
Justin Lu
committed
8366400: JCK test api/java_text/DecimalFormat/Parse.html fails after JDK-8363972
Reviewed-by: naoto
1 parent 431f467 commit becc35f

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/java.base/share/classes/java/text/DecimalFormat.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3517,13 +3517,14 @@ boolean matchAffix(String text, int position, String affix) {
35173517
var alen = affix.length();
35183518
var tlen = text.length();
35193519

3520+
// Verify position can fit length wise before checking char by char
3521+
if (position + alen > tlen || position < 0) {
3522+
return false;
3523+
}
35203524
if (alen == 0) {
35213525
// always match with an empty affix, as affix is optional
35223526
return true;
35233527
}
3524-
if (position >= tlen) {
3525-
return false;
3526-
}
35273528
if (parseStrict) {
35283529
return text.regionMatches(position, affix, 0, alen);
35293530
}

test/jdk/java/text/Format/NumberFormat/PositionTest.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,12 +21,6 @@
2121
* questions.
2222
*/
2323

24-
/**
25-
* @test
26-
* @bug 4109023 4153060 4153061
27-
* @summary test ParsePosition and FieldPosition
28-
* @run junit PositionTest
29-
*/
3024
/*
3125
(C) Copyright Taligent, Inc. 1996 - All Rights Reserved
3226
(C) Copyright IBM Corp. 1996 - All Rights Reserved
@@ -39,15 +33,71 @@
3933
Taligent is a registered trademark of Taligent, Inc.
4034
*/
4135

42-
import java.text.*;
43-
import java.io.*;
36+
/*
37+
* @test
38+
* @bug 4109023 4153060 4153061 8366400
39+
* @summary test ParsePosition and FieldPosition
40+
* @run junit PositionTest
41+
*/
4442

4543
import org.junit.jupiter.api.Test;
4644

45+
import java.text.DecimalFormat;
46+
import java.text.FieldPosition;
47+
import java.text.NumberFormat;
48+
import java.text.ParsePosition;
49+
50+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
51+
import static org.junit.jupiter.api.Assertions.assertEquals;
52+
import static org.junit.jupiter.api.Assertions.assertNull;
4753
import static org.junit.jupiter.api.Assertions.fail;
4854

4955
public class PositionTest {
5056

57+
// Parsing text which contains un-parseable data, but the index
58+
// begins at the valid portion. Ensure PP is properly updated.
59+
@Test
60+
public void modifiedPositionTest() {
61+
var df = new DecimalFormat("YY#");
62+
df.setStrict(false); // Lenient by default, set for test explicitness
63+
var pp = new ParsePosition(9);
64+
assertEquals(123L, assertDoesNotThrow(() -> df.parse("FOOBARBAZYY123", pp)));
65+
assertEquals(-1, pp.getErrorIndex());
66+
assertEquals(14, pp.getIndex());
67+
}
68+
69+
// Clearly invalid index value that could not work under any scenarios
70+
// Specifically, ensuring no SIOOBE during affix matching
71+
@Test
72+
public void invalidPositionParseTest() {
73+
var df = new DecimalFormat();
74+
df.setStrict(false); // Lenient by default, set for test explicitness
75+
assertNull(assertDoesNotThrow(() -> df.parse("1", new ParsePosition(-1))));
76+
assertNull(assertDoesNotThrow(() -> df.parse("1", new ParsePosition(Integer.MAX_VALUE))));
77+
}
78+
79+
// When prefix matching, position + affix length is greater than parsed String length
80+
// Ensure we do not index out of bounds of the length of the parsed String
81+
@Test
82+
public void prefixMatchingTest() {
83+
var df = new DecimalFormat("ZZZ#;YYY#");
84+
df.setStrict(false); // Lenient by default, set for test explicitness
85+
// 0 + 3 > 2 = (pos + prefix > text)
86+
assertNull(assertDoesNotThrow(() -> df.parse("Z1", new ParsePosition(0))));
87+
assertNull(assertDoesNotThrow(() -> df.parse("Y1", new ParsePosition(0))));
88+
}
89+
90+
// When suffix matching, position + affix length is greater than parsed String length
91+
// Ensure we do not index out of bounds of the length of the parsed String
92+
@Test
93+
public void suffixMatchingTest() {
94+
var df = new DecimalFormat("#ZZ;#YY");
95+
df.setStrict(false); // Lenient by default, set for test explicitness
96+
// Matches prefix properly first. Then 3 + 2 > 4 = (pos + suffix > text)
97+
assertNull(assertDoesNotThrow(() -> df.parse("123Z", new ParsePosition(0))));
98+
assertNull(assertDoesNotThrow(() -> df.parse("123Y", new ParsePosition(0))));
99+
}
100+
51101
@Test
52102
public void TestParsePosition() {
53103
ParsePosition pp1 = new ParsePosition(0);

0 commit comments

Comments
 (0)