Skip to content

ThreadFactoryProvider.getJavaVersion() mis-parses bare major version strings (e.g. "21", "22") #211

Description

@mvallim

Bug

ThreadFactoryProvider.getJavaVersion() incorrectly parses java.version when the value has no dot and no dash — which happens for bare major-version strings such as "21" or "22" (a legitimate value of java.version, e.g. right at a JDK major GA release before the first patch update).

Current code (amazon-sns-java-messaging-lib-template/src/main/java/com/amazon/sns/messaging/lib/concurrent/ThreadFactoryProvider.java):

final int dotPos = version.indexOf('.');
final int dashPos = version.indexOf('-');
final int endIndex = dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1;

return Integer.parseInt(version.substring(0, endIndex));

When neither a dot nor a dash is present, endIndex falls back to the literal 1,
so only the first character of the version string is parsed.

Reproduction

System.out.println(getJavaVersion("21"));   // expected 21, actual 2
System.out.println(getJavaVersion("22"));   // expected 22, actual 2
System.out.println(getJavaVersion("21.0.1")); // 21, correct (has a dot)

Impact

getJavaVersion() >= 21 gates whether ThreadFactoryProvider uses the virtual-thread factory (Java 21+) or falls back to Executors.defaultThreadFactory(). On a JVM whose java.version is a bare "21" or "22" (no . or - suffix), the check silently
resolves to false, so the library falls back to platform threads instead of virtual threads — with no error or exception, just a slightly misleading log line ("Java version is 2, using default thread factory").

Fix

Use version.length() instead of the literal 1 as the fallback end index, so the whole remaining string is parsed when there's no dot or dash:

final int endIndex = dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : version.length();

Verification

input before after (fixed)
"21" 2 21
"22" 2 22
"21.0.1" 21 21
"17.0.9" 17 17
"1.8.0_302" 8 8
"11-ea" 11 11
"9" 9 9

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions