8255405: sun/net/ftp/imp/FtpClient uses SimpleDateFormat in not thread-safe manner

Reviewed-by: chegar, ryadav, dfuchs
This commit is contained in:
Igor Ignatyev 2020-10-28 23:53:51 +00:00
parent d82a6dcfb9
commit 7e305ad1d4
2 changed files with 21 additions and 33 deletions

View file

@ -29,15 +29,16 @@ import java.io.*;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.time.ZoneOffset;
import java.text.SimpleDateFormat; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import java.util.Vector; import java.util.Vector;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -1739,19 +1740,8 @@ public class FtpClient extends sun.net.ftp.FtpClient {
return -1; return -1;
} }
private static final SimpleDateFormat[] dateFormats; private static final DateTimeFormatter RFC3659_DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss[.SSS]")
.withZone(ZoneOffset.UTC);
static {
String[] formats = {
"yyyyMMddHHmmss.SSS",
"yyyyMMddHHmmss"
};
dateFormats = new SimpleDateFormat[formats.length];
for (int i = 0; i < formats.length; ++i) {
dateFormats[i] = new SimpleDateFormat(formats[i]);
dateFormats[i].setTimeZone(TimeZone.getTimeZone("GMT"));
}
}
/** /**
* Issues the MDTM [path] command to the server to get the modification * Issues the MDTM [path] command to the server to get the modification
@ -1768,24 +1758,20 @@ public class FtpClient extends sun.net.ftp.FtpClient {
public Date getLastModified(String path) throws sun.net.ftp.FtpProtocolException, IOException { public Date getLastModified(String path) throws sun.net.ftp.FtpProtocolException, IOException {
issueCommandCheck("MDTM " + path); issueCommandCheck("MDTM " + path);
if (lastReplyCode == FtpReplyCode.FILE_STATUS) { if (lastReplyCode == FtpReplyCode.FILE_STATUS) {
String s = getResponseString().substring(4); String s = getResponseString();
return parseRfc3659TimeValue(s); return parseRfc3659TimeValue(s.substring(4, s.length() - 1));
} }
return null; return null;
} }
private static Date parseRfc3659TimeValue(String s) { private static Date parseRfc3659TimeValue(String s) {
Date d = null; Date result = null;
for (SimpleDateFormat dateFormat : dateFormats) { try {
try { var d = ZonedDateTime.parse(s, RFC3659_DATETIME_FORMAT);
d = dateFormat.parse(s); result = Date.from(d.toInstant());
} catch (ParseException ex) { } catch (DateTimeParseException ex) {
}
if (d != null) {
return d;
}
} }
return d; return result;
} }
/** /**

View file

@ -28,7 +28,8 @@
* @library /test/lib * @library /test/lib
* @modules java.base/sun.net.ftp * @modules java.base/sun.net.ftp
* @build jdk.test.lib.Asserts * @build jdk.test.lib.Asserts
* @run main TestFtpTimeValue * @run main/othervm -Duser.timezone=UTC TestFtpTimeValue
* @run main/othervm -Duser.timezone=America/Los_Angeles TestFtpTimeValue
*/ */
import jdk.test.lib.Asserts; import jdk.test.lib.Asserts;
@ -61,23 +62,24 @@ public class TestFtpTimeValue {
calendar.set(year, month - 1, day, hrs, min, sec); calendar.set(year, month - 1, day, hrs, min, sec);
calendar.set(Calendar.MILLISECOND, milliseconds); calendar.set(Calendar.MILLISECOND, milliseconds);
expectedCreated = calendar.getTime(); expectedCreated = calendar.getTime();
var s = String.format("%4d%2d%2d%2d%2d%2d", year, month, day, hrs, min, sec); var s = String.format("%4d%02d%02d%02d%02d%02d", year, month, day, hrs, min, sec);
if (milliseconds != 0) { if (milliseconds != 0) {
s += "." + String.format("%3d", milliseconds); s += "." + String.format("%03d", milliseconds);
} }
create = s; create = s;
calendar.add(GregorianCalendar.SECOND, 1); calendar.add(GregorianCalendar.SECOND, 1);
expectedModified = calendar.getTime(); expectedModified = calendar.getTime();
s = String.format("%4d%2d%2d%2d%2d%2d", year, month, day, hrs, min, sec + 1); s = String.format("%4d%02d%02d%02d%02d%02d", year, month, day, hrs, min, sec + 1);
if (milliseconds != 0) { if (milliseconds != 0) {
s += "." + String.format("%3d", milliseconds); s += "." + String.format("%03d", milliseconds);
} }
modify = s; modify = s;
} }
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
System.out.println("user.timezone: " + System.getProperty("user.timezone"));
try (FtpServer server = new FtpServer(); try (FtpServer server = new FtpServer();
FtpClient client = FtpClient.create()) { FtpClient client = FtpClient.create()) {
(new Thread(server)).start(); (new Thread(server)).start();