Skip to content

Remove useless null checks (CodeQL java/useless-null-check)#236

Open
vharseko wants to merge 3 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-useless-null-check
Open

Remove useless null checks (CodeQL java/useless-null-check)#236
vharseko wants to merge 3 commits into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-useless-null-check

Conversation

@vharseko

@vharseko vharseko commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Resolves all seven open java/useless-null-check CodeQL alerts. Each site tested
a value that provably cannot be null at that point, so the guarded branch (or the
null arm of a ternary) was dead.

File Line(s) Value Why it can't be null
Persistit.java 1739, 1760 _journalManager, _bufferPoolTable final fields initialised inline to new …()
Cookie.java 288, 491 port final List<Integer> = new ArrayList<>(), never reassigned
Info.java 51, 53 ip branch already guarded by ip != null
OSGiFrameworkService.java 340 input freshly assigned new BufferedReader(...)

Notes

  • Persistit.crash — the two != null guards are always true; the bodies now
    run unconditionally.
  • CookiehashCode() uses port.hashCode() directly and toString()
    appends port unconditionally (it was already appended on every call, since the
    guard could never be false).
  • Info.toString — the ip != null ? ip.getHostAddress() : ipString
    ternaries are inside if (ip != null && …) branches, so they always take the
    non-null arm. The other, unguarded ternaries further down are left untouched.
  • OSGiFrameworkService — this one hid a real bug. The check was on the
    BufferedReader, which a constructor never returns as null, so the intended
    failure mode (a missing /launcher.json on the classpath) was never caught:
    getResourceAsStream returns null and new InputStreamReader(null) then
    throws NullPointerException. The null check now guards the resource stream
    before it is wrapped, so the intended IllegalArgumentException is thrown.

Behaviour is unchanged for every existing input; the OSGi case additionally
reports the missing-resource error cleanly instead of as an NPE.

Testing

mvn -o -pl commons/geo,persistit/core,commons/http-framework/core,commons/launcher/launcher compile — each module builds (BUILD SUCCESS).

Seven null checks tested a value that provably cannot be null at that point, so
the guarded branch (or the null arm of a ternary) was dead code. Simplify each:

  - Persistit.crash: _journalManager and _bufferPoolTable are final fields
    initialised inline to new instances, so they are never null. Drop the
    always-true "!= null" guards and run the bodies unconditionally.
  - Cookie.hashCode/toString: the port field is a final, inline-initialised
    ArrayList that is never reassigned, so it is never null. Use
    port.hashCode() directly and append it unconditionally.
  - Info.toString: the two "ip != null ? ip.getHostAddress() : ipString"
    ternaries sit inside branches already guarded by "ip != null", so they
    always take the non-null arm; replace them with ip.getHostAddress().
  - OSGiFrameworkService.loadConfiguration: the check on the freshly created
    BufferedReader was useless (a constructor never returns null), and it left
    the real failure mode - a missing /launcher.json on the classpath -
    unhandled: getResourceAsStream returns null and new InputStreamReader(null)
    then throws NullPointerException. Null-check the resource stream before
    wrapping it, so the intended IllegalArgumentException is thrown instead.

Behaviour is unchanged for every existing input; the OSGi case additionally
reports the missing-resource error cleanly rather than as an NPE.
@vharseko vharseko requested a review from maximthomas July 8, 2026 13:49
@vharseko vharseko added bug codeql CodeQL static-analysis findings refactoring Code cleanup / refactoring, no behavior change labels Jul 8, 2026
The missing-volume alert asserted after startup is posted by
JournalManager.writeForCopy when the journal copier copies a page back to a
now-deleted volume. For the non-transactional pages this test writes, that copy
runs on the background JOURNAL_COPIER thread, so the assertion immediately after
new Persistit(_config) races it: on a slow/loaded runner no alert has been
posted yet and getHistory(MISSING_VOLUME_CATEGORY) returns null ("Startup with
missing volumes should have generated alerts", seen on macos-latest JDK 26).

Force the copy-back with copyBackPages() before the assertion so the alert is
generated deterministically. ignoreMissingVolumes is still false at that point,
so the missing-volume pages are retained and the later
ignoreMissingVolumes-behavior check is unaffected.
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 refactoring Code cleanup / refactoring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants