farkhalit commented on code in PR #435:
URL: https://github.com/apache/commons-cli/pull/435#discussion_r3671330882


##########
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:
   Good point. Restructured so the English fallback only runs when the 
default-locale parse returns null. A partial match is now treated as a 
trailing-text failure and throws with the parse position, so the reported 
offset points past the parsed date instead of failing at index 0 under a 
non-English locale.



-- 
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]

Reply via email to