Skip to content

Make the shared Key.SDF date formatter thread-safe (CodeQL java/thread-unsafe-dateformat) - #228

Merged
vharseko merged 3 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-thread-unsafe-dateformat
Jul 16, 2026
Merged

vharseko merged 3 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-thread-unsafe-dateformat

Conversation

@vharseko

@vharseko vharseko commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Resolves the java/thread-unsafe-dateformat CodeQL alert on Key.SDF.

Key.SDF was a public final static SimpleDateFormat. SimpleDateFormat
is not thread-safe — concurrent format() / parse() calls mutate its
internal Calendar and can produce garbled text, wrong dates, or
ArrayIndexOutOfBoundsException. This single shared instance is reachable
from several classes that run on multiple threads:

  • Key.decodeDisplayable / Key.toString
  • Value.toString
  • KeyParser (parse, date keys)
  • com.persistit.ui.ValueInspectorTreeNode (Swing)

Fix

Wrap the formatter in a ThreadLocal so each thread gets its own instance:

public final static ThreadLocal<SimpleDateFormat> SDF = ThreadLocal
        .withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss.SSSZ"));

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.SDF is a public field, so its compile-time type changes from
SimpleDateFormat to ThreadLocal<SimpleDateFormat>. All in-repo callers
are updated in this PR. Any external caller would replace Key.SDF.format(d)
with Key.SDF.get().format(d).

The other, private static SimpleDateFormat fields in the tree
(JournalTool, VolumeHeader, CheckpointManager, and the AbstractSuite
test) were not flagged by CodeQL and are left unchanged.

Testing

mvn -o -pl persistit/core,persistit/ui -am compile — BUILD SUCCESS.

…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.
@vharseko vharseko added bug java codeql CodeQL static-analysis findings labels Jul 8, 2026
@vharseko
vharseko requested a review from maximthomas July 8, 2026 12:42
@vharseko vharseko removed the java label Jul 8, 2026
@vharseko
vharseko force-pushed the features/codeql-thread-unsafe-dateformat branch from ef15eb9 to ea9c6bd Compare July 10, 2026 18:18
@vharseko
vharseko merged commit 63bc6b8 into OpenIdentityPlatform:master Jul 16, 2026
4 of 13 checks passed
@vharseko
vharseko deleted the features/codeql-thread-unsafe-dateformat branch July 16, 2026 16:59
vharseko added a commit that referenced this pull request Jul 17, 2026
* Drop the stale animal-sniffer Java 1.7 API check from persistit

The persistit modules pinned animal-sniffer to the signature
org.codehaus.mojo.signature:java17:1.0 — the Java 1.7 API surface —
inherited from the upstream Akiban Persistit build. The project compiles
at maven.compiler.release=11, so the check enforced a baseline three
releases below the declared one, and javac already enforces the real
baseline natively via `release`. The check was redundant and wrong.

It never ran in CI until #270 switched the build from `mvn package` to
`mvn verify`: animal-sniffer binds to the verify phase. Once it started
running it failed on Java 8 APIs merged earlier under `mvn package` —
ThreadLocal.withInitial (#228) and Long.hashCode(long) (#232).

Remove the plugin declarations from the core and ui modules along with
the signature properties in the parent. Nothing else refers to it: there
are no @IgnoreJRERequirement annotations in the tree.

* Add the missing license headers to the persistit poms

The three persistit poms carried no license header. They are not
upstream Akiban files: they were added in #12 when the build artifacts
were reorganized, so there is no upstream owner to preserve.

Use the CDDL block of the root pom, which is their parent, with a
standalone 3A Systems copyright covering 2020 (when the files were
added) through the current year.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug codeql CodeQL static-analysis findings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants