Make the shared Key.SDF date formatter thread-safe (CodeQL java/thread-unsafe-dateformat)#228
Open
vharseko wants to merge 2 commits into
Conversation
…d-unsafe-dateformat)
Key.SDF was a public static SimpleDateFormat shared across all threads.
SimpleDateFormat is not thread-safe: concurrent format()/parse() calls
corrupt its internal Calendar state, producing garbled output, wrong
dates, or ArrayIndexOutOfBoundsException. Key.SDF is reachable from
Key.decodeDisplayable/toString, Value.toString, KeyParser and the
Swing ValueInspectorTreeNode, all of which can run on multiple threads.
Wrap the formatter in a ThreadLocal so each thread gets its own
SimpleDateFormat instance. The pattern ("yyyyMMddHHmmss.SSSZ") and thus
the formatting/parsing semantics are unchanged; only the shared mutable
state is removed. Update the four call sites to Key.SDF.get().
The remaining private static SimpleDateFormat fields (JournalTool,
VolumeHeader, CheckpointManager, and the AbstractSuite test) were not
flagged by CodeQL and are left unchanged.
maximthomas
approved these changes
Jul 8, 2026
ef15eb9 to
ea9c6bd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves the
java/thread-unsafe-dateformatCodeQL alert onKey.SDF.Key.SDFwas apublic final static SimpleDateFormat.SimpleDateFormatis not thread-safe — concurrent
format()/parse()calls mutate itsinternal
Calendarand can produce garbled text, wrong dates, orArrayIndexOutOfBoundsException. This single shared instance is reachablefrom several classes that run on multiple threads:
Key.decodeDisplayable/Key.toStringValue.toStringKeyParser(parse, date keys)com.persistit.ui.ValueInspectorTreeNode(Swing)Fix
Wrap the formatter in a
ThreadLocalso each thread gets its own instance:and update the four call sites to
Key.SDF.get().format(...)/Key.SDF.get().parse(...).The date pattern is unchanged, so formatting and parsing behaviour is
identical for every input; only the shared mutable state is removed.
Note
Key.SDFis apublicfield, so its compile-time type changes fromSimpleDateFormattoThreadLocal<SimpleDateFormat>. All in-repo callersare updated in this PR. Any external caller would replace
Key.SDF.format(d)with
Key.SDF.get().format(d).The other,
privatestaticSimpleDateFormatfields in the tree(
JournalTool,VolumeHeader,CheckpointManager, and theAbstractSuitetest) were not flagged by CodeQL and are left unchanged.
Testing
mvn -o -pl persistit/core,persistit/ui -am compile— BUILD SUCCESS.