mirror of
https://github.com/openjdk/jdk.git
synced 2026-03-07 22:50:49 +00:00
8255405: sun/net/ftp/imp/FtpClient uses SimpleDateFormat in not thread-safe manner
Reviewed-by: chegar, ryadav, dfuchs
This commit is contained in:
parent
d82a6dcfb9
commit
7e305ad1d4
@ -29,15 +29,16 @@ import java.io.*;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
import java.util.Vector;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -1739,19 +1740,8 @@ public class FtpClient extends sun.net.ftp.FtpClient {
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static final SimpleDateFormat[] dateFormats;
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
private static final DateTimeFormatter RFC3659_DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss[.SSS]")
|
||||
.withZone(ZoneOffset.UTC);
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
issueCommandCheck("MDTM " + path);
|
||||
if (lastReplyCode == FtpReplyCode.FILE_STATUS) {
|
||||
String s = getResponseString().substring(4);
|
||||
return parseRfc3659TimeValue(s);
|
||||
String s = getResponseString();
|
||||
return parseRfc3659TimeValue(s.substring(4, s.length() - 1));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Date parseRfc3659TimeValue(String s) {
|
||||
Date d = null;
|
||||
for (SimpleDateFormat dateFormat : dateFormats) {
|
||||
try {
|
||||
d = dateFormat.parse(s);
|
||||
} catch (ParseException ex) {
|
||||
}
|
||||
if (d != null) {
|
||||
return d;
|
||||
}
|
||||
Date result = null;
|
||||
try {
|
||||
var d = ZonedDateTime.parse(s, RFC3659_DATETIME_FORMAT);
|
||||
result = Date.from(d.toInstant());
|
||||
} catch (DateTimeParseException ex) {
|
||||
}
|
||||
return d;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -28,7 +28,8 @@
|
||||
* @library /test/lib
|
||||
* @modules java.base/sun.net.ftp
|
||||
* @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;
|
||||
@ -61,23 +62,24 @@ public class TestFtpTimeValue {
|
||||
calendar.set(year, month - 1, day, hrs, min, sec);
|
||||
calendar.set(Calendar.MILLISECOND, milliseconds);
|
||||
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) {
|
||||
s += "." + String.format("%3d", milliseconds);
|
||||
s += "." + String.format("%03d", milliseconds);
|
||||
}
|
||||
create = s;
|
||||
|
||||
calendar.add(GregorianCalendar.SECOND, 1);
|
||||
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) {
|
||||
s += "." + String.format("%3d", milliseconds);
|
||||
s += "." + String.format("%03d", milliseconds);
|
||||
}
|
||||
modify = s;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("user.timezone: " + System.getProperty("user.timezone"));
|
||||
try (FtpServer server = new FtpServer();
|
||||
FtpClient client = FtpClient.create()) {
|
||||
(new Thread(server)).start();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user