Skip to content

Commit 7447276

Browse files
committed
8360045: StringTokenizer.hasMoreTokens() throws NPE after nextToken(null)
Reviewed-by: liach, alanb
1 parent c90c31b commit 7447276

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/java.base/share/classes/java/util/StringTokenizer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 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
@@ -368,6 +368,8 @@ public String nextToken() {
368368
* @throws NullPointerException if delim is {@code null}
369369
*/
370370
public String nextToken(String delim) {
371+
Objects.requireNonNull(delim);
372+
371373
delimiters = delim;
372374

373375
/* delimiter string specified, so set the appropriate flag. */
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8360045
27+
* @summary Verify StringTokenizer.nextToken(null) does not alter the
28+
* existing delimiter
29+
* @run junit NextTokenWithNullDelimTest
30+
*/
31+
32+
import java.util.StringTokenizer;
33+
import org.junit.jupiter.api.Test;
34+
35+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
37+
38+
public class NextTokenWithNullDelimTest {
39+
@Test
40+
void testNextTokenWithNullDelim() {
41+
StringTokenizer st = new StringTokenizer("test");
42+
assertThrows(NullPointerException.class, () -> st.nextToken(null));
43+
assertDoesNotThrow(st::hasMoreTokens);
44+
}
45+
}

0 commit comments

Comments
 (0)