Copilot commented on code in PR #435:
URL: https://github.com/apache/commons-cli/pull/435#discussion_r3637518186
##########
src/main/java/org/apache/commons/cli/Converter.java:
##########
@@ -82,15 +83,24 @@ public interface Converter<T, E extends Exception> {
final SimpleDateFormat format = new SimpleDateFormat(pattern);
// reject out-of-range fields (for example "Feb 30") instead of
silently rolling them over.
format.setLenient(false);
- try {
- return format.parse(s);
- } catch (final java.text.ParseException e) {
+ // SimpleDateFormat.parse(String) stops at the first character it
cannot use and ignores any
+ // trailing text, so "<valid date> garbage" would be accepted. Parse
from an explicit position
+ // and reject the value unless the whole string is consumed.
+ 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.
Review Comment:
The fallback to `Locale.ENGLISH` is currently triggered both when the
default-locale parse fails (`date == null`) and when it succeeds but does not
consume the entire input (`pos.getIndex() != s.length()`). In the latter case
(e.g., a non-English locale input with trailing garbage), the English parse can
fail at index 0 and the thrown `ParseException` will report a misleading error
position. Consider only falling back to English when the initial parse returns
`null`, and otherwise throw using the initial `ParsePosition` when parsing
stops before the end of the string.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]