Repository navigation
Fix WrongMethodTypeException in BStrFFM.free void invokeExact (#3422) - #3424
Conversation
SysFreeString is a void downcall, so its handle type is (MemorySegment)void. MethodHandle.invokeExact is signature-polymorphic and infers its return type from the call site; in an expression lambda passed to the void-returning runOrLog(ThrowingRunnable), javac infers Object instead of void, so the call site's (MemorySegment)Object does not match the handle and throws WrongMethodTypeException at runtime (swallowed at DEBUG, leaking the BSTR). Convert to a block lambda so invokeExact is in a statement context and its return type is inferred as void. This reinstates the fix from #3302, which was lost when the FFM code moved from oshi.ffm.windows.com to oshi.ffm.platform.windows.com; a comment now marks why the block form matters. Fixes #3422
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
📜 Recent review details⏰ Context from checks skipped due to timeout. (14)
📝 WalkthroughWalkthrough
ChangesFFM void
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3424 +/- ##
============================================
- Coverage 75.20% 75.15% -0.05%
Complexity 76 76
============================================
Files 580 580
Lines 21589 21589
Branches 3583 3583
============================================
- Hits 16235 16226 -9
- Misses 3328 3336 +8
- Partials 2026 2027 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The WrongMethodTypeException from a void invokeExact in an expression lambda only surfaces on the native platform and is swallowed by runOrLog, so no ordinary unit test can observe it -- and it has already regressed twice when the FFM code was moved between packages (#3301, #3422). VoidInvokeExactSourceTest scans oshi-core-ffm's main sources for the expression-lambda form and fails with the offending file:line, so the regression is caught on every build (including non-Windows CI). Verified it fails on the pre-fix form and passes on the block-lambda fix.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
oshi-core-ffm/src/test/java/oshi/ffm/VoidInvokeExactSourceTest.java (1)
41-59: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winRegex misses multi-line expression lambdas.
The scan processes each line independently (
lines.get(i)matched individually), so the pattern in Line 43 only catches offenders where->andinvokeExactappear on the same line. An expression lambda split across lines, e.g.:runOrLog(() -> SysFreeString.invokeExact(bstr), LOG, "msg");would not be flagged, even though it reproduces the exact
WrongMethodTypeExceptionthis test is meant to guard against. Given the stated purpose — catching a regression that has already recurred twice — this is a meaningful blind spot.Consider joining continuation lines (or matching against the whole file content with
Pattern.DOTALL) before scanning, so line-wrapped expression lambdas are also detected.♻️ Possible approach: scan whole-file content instead of per-line
- List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8); - for (int i = 0; i < lines.size(); i++) { - if (EXPRESSION_LAMBDA_INVOKE_EXACT.matcher(lines.get(i)).find()) { - offenders.add(p + ":" + (i + 1) + " " + lines.get(i).strip()); - } - } + String content = Files.readString(p, StandardCharsets.UTF_8); + Matcher m = EXPRESSION_LAMBDA_INVOKE_EXACT.matcher(content); + while (m.find()) { + int line = (int) content.substring(0, m.start()).chars().filter(c -> c == '\n').count() + 1; + offenders.add(p + ":" + line + " " + m.group().strip()); + }(pattern would also need
Pattern.DOTALLso./[^{]can span newlines)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-core-ffm/src/test/java/oshi/ffm/VoidInvokeExactSourceTest.java` around lines 41 - 59, The source scan in VoidInvokeExactSourceTest only checks each line separately, so EXPRESSION_LAMBDA_INVOKE_EXACT misses expression lambdas split across multiple lines. Update noVoidInvokeExactInExpressionLambda to scan multi-line content instead of lines.get(i), for example by reading each file into a single string or joining continuation lines and using a DOTALL-capable pattern. Keep the detection anchored around EXPRESSION_LAMBDA_INVOKE_EXACT and the per-file loop in noVoidInvokeExactInExpressionLambda so wrapped runOrLog/runSilently lambdas with invokeExact are still flagged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@oshi-core-ffm/src/test/java/oshi/ffm/VoidInvokeExactSourceTest.java`:
- Around line 41-59: The source scan in VoidInvokeExactSourceTest only checks
each line separately, so EXPRESSION_LAMBDA_INVOKE_EXACT misses expression
lambdas split across multiple lines. Update noVoidInvokeExactInExpressionLambda
to scan multi-line content instead of lines.get(i), for example by reading each
file into a single string or joining continuation lines and using a
DOTALL-capable pattern. Keep the detection anchored around
EXPRESSION_LAMBDA_INVOKE_EXACT and the per-file loop in
noVoidInvokeExactInExpressionLambda so wrapped runOrLog/runSilently lambdas with
invokeExact are still flagged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 35eda9dc-e719-4e32-a4de-26f7a423aef5
📒 Files selected for processing (1)
oshi-core-ffm/src/test/java/oshi/ffm/VoidInvokeExactSourceTest.java
📜 Review details
⏰ Context from checks skipped due to timeout. (15)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, macos-latest
- GitHub Check: Test JDK 21, ubuntu-latest
- GitHub Check: Test JDK 21, windows-latest
- GitHub Check: Analyze (java)
- GitHub Check: Test JDK 21, macos-latest
- GitHub Check: Test JDK 26, windows-latest
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 26, ubuntu-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
- GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
🔇 Additional comments (1)
oshi-core-ffm/src/test/java/oshi/ffm/VoidInvokeExactSourceTest.java (1)
1-40: LGTM!Also applies to: 60-70
- Add // NOSONAR (java:S1602) on the BStrFFM.free block lambda so a future Sonar S1602 sweep can't collapse it back to the expression form that reintroduced #3422. It is the only single-statement void invokeExact block lambda; the other runOrLog/runSilently blocks are multi-statement, and Ole32FFM.CoUninitialize is a safe no-arg method reference. - VoidInvokeExactSourceTest now scans whole-file content (bounded to a single statement with [^{};]*) so an expression lambda wrapped across lines is also caught, not just single-line occurrences. - Add CHANGELOG entry.
Problem
Fixes #3422. On the Windows FFM path,
BStrFFM.freethrows at runtime:SysFreeStringis a void downcall (handle type(MemorySegment)void).MethodHandle.invokeExactis signature-polymorphic and infers its return type from the call site. In an expression-bodied lambda passed to the void-returningrunOrLog(ThrowingRunnable), javac infersObjectrather thanvoid, so the call site's(MemorySegment)Objectmismatches the handle and throws. It's caught and logged at DEBUG byrunOrLog, so it doesn't crash — but the BSTR is never freed, leaking memory on every WMI query.Regression note
This is a regression of #3302 (which fixed #3301). #3302 fixed this exact line with a block lambda. It was not lost in the later FFM package move (#3356 preserved it); it was re-broken by
1d0cb9cf97"Collapse single-statement block lambdas to expression form (S1602)", a Sonar mechanical sweep that collapsed() -> { invokeExact(); }back to() -> invokeExact()— reintroducing the footgun. S1602 (and its autofix) will do this again to any such line.Fix
invokeExactis in a statement context where its return type is correctly inferred asvoid, with a comment marking why the block form matters.VoidInvokeExactSourceTest, a source-policy test that scansoshi-core-ffmmain sources for the expression-lambda form and fails with the offendingfile:line. Because the runtimeWrongMethodTypeExceptiononly fires on the native platform and is swallowed byrunOrLog, no ordinary unit test can observe it — the source scan runs on every build (incl. non-Windows CI) and is the guard against the next S1602-style collapse. Verified it fails on the pre-fix form and passes on the fix.I scanned the whole
oshi-core-ffmtree —BStrFFM.freewas the only remaining reintroduction (sibling methods pin the type with casts;Ole32FFMuses a::invokeExactmethod reference where the void SAM pins it).Pre-commit gate (spotless, checkstyle, install, forbiddenapis, javadoc) passes on
oshi-core-ffm.Summary by CodeRabbit
Bug Fixes
WrongMethodTypeExceptionwhen releasing Windows WMI BSTR strings, improving stability on affected systems.Tests