Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly check for errors when parsing text calendars #1035

Merged

Conversation

stevedlawrence
Copy link
Member

The SimpleDateFormat parse() method does not set the error index on the ParsePosition if there is an error. Instead, it only sets the normal index to the last successful parse position of the input string.

Daffodil currently relies on the error index, which means our error checking is incorrect.

Fortunately, in most cases, this doesn't matter because our error checking also requires that we parse the entire input string, so if the index is zero it wouldn't match the string length and we would still see it as an error. The one exception to this is if the input string is zero length, in which case we do not correctly error and just create a date/time with value zero.

This is fixed by correctly checking index for zero instead of inspecting the error index.

This also does a little refactoring to replace the very old cache implementation with the much newer Evaluatable for creating SimpleDateFormat instances.

DAFFODIL-2821

override def initialValue = {
val formatter = new SimpleDateFormat(pattern, locale)
formatter.setCalendar(calendar)
formatter.setLenient(true) // TODO: should this use calendarCheckPolicy?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this is wrong and makes it so calendar parsing is always lenient regardless of the dfdl:calendarCheckPolicy property, but this copies the existing logic from the old tlDataFormatter so at least doesn't cause any regressions and just fixes the one bug at hand.

I've attempted to make it correctly use calendarCheckPolicy, but it broke a number of tests, including some unexpected failures that I think need more investigation. Once I can create a reproducible test I'll open a ticket, but I think a fix might be best for the next release so we have time to deal with regressions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree we should fix the one problem, and defer the strict/lenient to next point release.

The SimpleDateFormat parse() method does not set the error index on the
ParsePosition if there is an error. Instead, it only sets the normal
index to the last successful parse position of the input string.

Daffodil currently relies on the error index, which means our error
checking is incorrect.

Fortunately, in most cases, this doesn't matter because our error
checking also requires that we parse the entire input string, so if the
index is zero it wouldn't match the string length and we would still see
it as an error. The one exception to this is if the input string is zero
length, in which case we do not correctly error and just create a
date/time with value zero.

This is fixed by correctly checking index for zero instead of inspecting
the error index.

This also does a little refactoring to replace the very old cache
implementation with the much newer Evaluatable for creating
SimpleDateFormat instances.

DAFFODIL-2821
Copy link
Contributor

@mbeckerle mbeckerle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

private lazy val dateTimeFormatterEv = {
val ev = new DateTimeFormatterEv(
calendarEv,
localeEv,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change may affect/fix https://issues.apache.org/jira/browse/DAFFODIL-2823 also, as the locale is being supplied differently now.

override def initialValue = {
val formatter = new SimpleDateFormat(pattern, locale)
formatter.setCalendar(calendar)
formatter.setLenient(true) // TODO: should this use calendarCheckPolicy?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree we should fix the one problem, and defer the strict/lenient to next point release.

Copy link
Contributor

@tuxji tuxji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 (others, please step up too)

@stevedlawrence stevedlawrence merged commit d670968 into apache:main Jun 15, 2023
@stevedlawrence stevedlawrence deleted the daffodil-2821-empty-dateTime branch June 15, 2023 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants