enasequence / sequencetools

Webin sequence validation API.
Apache License 2.0
10 stars 3 forks source link

FlatFileUtils SimpleDateFormat not thread safe #43

Closed nbuso closed 1 year ago

nbuso commented 6 years ago

The usage of SimpleDateFormat is not thread safe, easily generating Exceptions of the type:

    java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Long.parseLong(Long.java:601)
        at java.lang.Long.parseLong(Long.java:631)
        at java.text.DigitList.getLong(DigitList.java:195)
        at java.text.DecimalFormat.parse(DecimalFormat.java:2084)
        at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
        at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
        at java.text.DateFormat.parse(DateFormat.java:364)
        at uk.ac.ebi.embl.flatfile.FlatFileUtils.getDay(FlatFileUtils.java:239)
        at uk.ac.ebi.embl.flatfile.FlatFileUtilsTest$1.run(FlatFileUtilsTest.java:75)
        at uk.ac.ebi.embl.flatfile.FlatFileUtilsTest$3.run(FlatFileUtilsTest.java:122)

or

java.lang.ArrayIndexOutOfBoundsException: 56
    at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:453)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2397)
    at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2818)
    at java.util.Calendar.updateTime(Calendar.java:3393)
    at java.util.Calendar.getTimeInMillis(Calendar.java:1782)
    at java.util.Calendar.getTime(Calendar.java:1755)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1532)
    at java.text.DateFormat.parse(DateFormat.java:364)
    at uk.ac.ebi.embl.flatfile.FlatFileUtils.getDay(FlatFileUtils.java:239)

or

java.lang.NumberFormatException: For input string: "26986986E..26986986269869864EE44"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at java.text.DigitList.getDouble(DigitList.java:169)
    at java.text.DecimalFormat.parse(DecimalFormat.java:2089)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
    at java.text.DateFormat.parse(DateFormat.java:364)
    at uk.ac.ebi.embl.flatfile.FlatFileUtils.getDay(FlatFileUtils.java:239)
raskoleinonen commented 1 year ago

The code has been changed to use DateTimeFormatter (threadsafe): https://github.com/enasequence/sequencetools/commit/dfbc6cd10083b6fa109bb3aec977ef13d6cec0b5