Skip to content

Commit

Permalink
8319640: ClassicFormat::parseObject (from DateTimeFormatter) does not…
Browse files Browse the repository at this point in the history
… conform to the javadoc and may leak DateTimeException

Backport-of: fe0ccdf
  • Loading branch information
Xiaolong Peng authored and Paul Hohensee committed Nov 22, 2024
1 parent a44a35e commit e0dd517
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2240,29 +2240,23 @@ public Object parseObject(String text, ParsePosition pos) {
DateTimeParseContext context;
try {
context = formatter.parseUnresolved0(text, pos);
} catch (IndexOutOfBoundsException ex) {
if (pos.getErrorIndex() < 0) {
pos.setErrorIndex(0);
}
return null;
}
if (context == null) {
if (pos.getErrorIndex() < 0) {
pos.setErrorIndex(0);
if (context == null) {
if (pos.getErrorIndex() < 0) {
pos.setErrorIndex(0);
}
return null;
}
return null;
}
try {
TemporalAccessor resolved = context.toResolved(formatter.resolverStyle, formatter.resolverFields);
if (parseType == null) {
return resolved;
}
return resolved.query(parseType);
} catch (RuntimeException ex) {
pos.setErrorIndex(0);
if (pos.getErrorIndex() < 0) {
pos.setErrorIndex(0);
}
return null;
}
}
}

}
36 changes: 34 additions & 2 deletions test/jdk/java/time/test/java/time/format/TestDateTimeParsing.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. 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
Expand Down Expand Up @@ -62,15 +62,19 @@
import static java.time.temporal.ChronoField.AMPM_OF_DAY;
import static java.time.temporal.ChronoField.EPOCH_DAY;
import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
import static java.time.temporal.ChronoField.SECOND_OF_DAY;
import static java.util.Locale.US;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

import java.text.ParsePosition;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
Expand All @@ -80,15 +84,17 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.SignStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
* @test
* @summary Test parsing of edge cases.
* @bug 8223773 8272473
* @bug 8223773 8272473 8319640
*/
public class TestDateTimeParsing {

Expand Down Expand Up @@ -237,4 +243,30 @@ public void test_validateHourOfAmPm() {
}
}
}

// Checks ::toFormat().parseObject(text, pos) do not throw DateTimeException
@Test
public void test_toFormat_2arg_null_return_on_DateTimeException() {
var f = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
.optionalStart()
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
.optionalEnd()
.optionalStart()
.appendOffset("+HHmm", "Z")
.optionalEnd()
.toFormatter(Locale.ROOT)
.toFormat();
assertNull(f.parseObject("17-30", new ParsePosition(0)));
}

// Checks ::toFormat().parseObject(text, pos) do not throw IOOBE
@Test
public void test_toFormat_2arg_null_return_on_IOOBE() {
var date = "2023-11-13";
assertNull(DateTimeFormatter.ISO_LOCAL_DATE
.toFormat()
.parseObject(date, new ParsePosition(date.length() + 1)));
}
}

0 comments on commit e0dd517

Please sign in to comment.