Struts 2
  1. Struts 2
  2. WW-2567

DateTimePicker makes non-thread-safe use of SimpleDateFormat

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 2.0.11.1
    • Fix Version/s: 2.0.12
    • Component/s: Core Actions
    • Labels:
      None
    • Flags:
      Patch

      Description

      DateTimePicker has an internal static final instance of a SimpleDateFormat it uses to format date strings.

      However, according to the SimpleDateFormat JavaDoc:
      Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

      Easy fix is to synchronize around all uses of the class.

      Index: src/main/java/org/apache/struts2/components/DateTimePicker.java
      ===================================================================
      — src/main/java/org/apache/struts2/components/DateTimePicker.java (revision 641326)
      +++ src/main/java/org/apache/struts2/components/DateTimePicker.java (working copy)
      @@ -294,20 +294,25 @@
      return null;

      if(obj instanceof Date) {
      + synchronized (RFC3339_FORMAT)

      { return RFC3339_FORMAT.format((Date) obj); + }

      } else {
      // try to parse a date
      String dateStr = obj.toString();
      if(dateStr.equalsIgnoreCase("today"))
      + synchronized (RFC3339_FORMAT)

      { return RFC3339_FORMAT.format(new Date()); - + }

      try {
      Date date = null;
      if(this.displayFormat != null) {
      SimpleDateFormat format = new SimpleDateFormat(
      (String) getParameters().get("displayFormat"));
      date = format.parse(dateStr);

      • return RFC3339_FORMAT.format(date);
        + synchronized (RFC3339_FORMAT) { + return RFC3339_FORMAT.format(date); + }

        } else {
        // last resource to assume already in correct/default format
        return dateStr;

        Activity

          People

          • Assignee:
            James Mitchell
            Reporter:
            Mike Calmus
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development