Skip to content

Commit

Permalink
8255405: sun/net/ftp/imp/FtpClient uses SimpleDateFormat in not threa…
Browse files Browse the repository at this point in the history
…d-safe manner

Reviewed-by: chegar, ryadav, dfuchs
  • Loading branch information
iignatev committed Oct 28, 2020
1 parent d82a6dc commit 7e305ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
42 changes: 14 additions & 28 deletions src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions test/jdk/sun/net/ftp/TestFtpTimeValue.java
Expand Up @@ -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;
Expand Down Expand Up @@ -61,23 +62,24 @@ private enum TestCase {
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();
Expand Down

1 comment on commit 7e305ad

@bridgekeeper
Copy link

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.