Skip to content

Reject trailing text after the date in Converter.DATE#435

Open
farkhalit wants to merge 1 commit into
apache:masterfrom
farkhalit:date-trailing-text
Open

Reject trailing text after the date in Converter.DATE#435
farkhalit wants to merge 1 commit into
apache:masterfrom
farkhalit:date-trailing-text

Conversation

@farkhalit

Copy link
Copy Markdown

Converter.DATE parses with SimpleDateFormat.parse(String), which stops at the first character it cannot use and ignores whatever follows, so a Date-typed option value like Fri Jun 07 03:18:57 IST 2002 rm -rf / returns a valid Date and the trailing text is dropped. That defeats the setLenient(false) strictness added in #430. Parse from an explicit ParsePosition and reject the value unless the whole string is consumed, keeping the Locale.ENGLISH fallback.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This pull request tightens Converter.DATE parsing so it rejects inputs where a valid Date prefix is followed by extra trailing text, preventing partial parses that could undermine the strictness added in #430.

Changes:

  • Switch Converter.DATE to parse via ParsePosition and require full-string consumption (including Locale.ENGLISH fallback).
  • Add a regression test ensuring trailing text after a valid formatted date is rejected.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/main/java/org/apache/commons/cli/Converter.java Enforces full consumption during date parsing (and preserves English fallback) to reject trailing garbage.
src/test/java/org/apache/commons/cli/ConverterTests.java Adds a test case covering rejection of trailing text after an otherwise valid date.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +89 to 93
final ParsePosition pos = new ParsePosition(0);
Date date = format.parse(s, pos);
if (date == null || pos.getIndex() != s.length()) {
// Date.toString() always emits English month/day names, so fall back to Locale.ENGLISH
// when the default locale rejects the documented format.

@garydgregory garydgregory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@farkhalit
Please review the co-pilot comments. Add another unit test if additional coverage is needed.

@garydgregory garydgregory changed the title reject trailing text after the date in Converter.DATE Reject trailing text after the date in Converter.DATE Jul 23, 2026
@garydgregory

Copy link
Copy Markdown
Member

@farkhalit ping 🔔

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