Skip to content
Permalink
Browse files
8255405: sun/net/ftp/imp/FtpClient uses SimpleDateFormat in not threa…
…d-safe manner

Reviewed-by: chegar, ryadav, dfuchs
  • Loading branch information
iignatev committed Oct 28, 2020
1 parent d82a6dc commit 7e305ad1d405a7c7bd8edcb49fc105fdfafeb323
Showing with 21 additions and 33 deletions.
  1. +14 −28 src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java
  2. +7 −5 test/jdk/sun/net/ftp/TestFtpTimeValue.java
@@ -29,15 +29,16 @@
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 long getSize(String path) throws sun.net.ftp.FtpProtocolException, IOExce
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 long getSize(String path) throws sun.net.ftp.FtpProtocolException, IOExce
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 @@
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();

1 comment on commit 7e305ad

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on 7e305ad Oct 28, 2020

Please sign in to comment.