Skip to content

Fix misc. SonarQube and Coverity findings - #3157

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:coverity-fix
Apr 14, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:coverity-fix

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 14, 2026 •

Copy link
Copy Markdown
Member

Coverity:

  • MacInstalledApps: handle return value of FileInputStream.read() to avoid potential out-of-range buffer access (CWE-252)
  • SystemInfoHelper: null-check getProcess() return before dereferencing in printProcesses() (CWE-476)
  • PrivilegedUtil: add comment that default FileSystem is a JVM singleton that does not need closing (CWE-404 false positive)

SonarQube:

  • LinuxOperatingSystem: group regex alternatives with anchors to make precedence explicit (S5850)
  • PrivilegedUtil: replace volatile Supplier fields with AtomicReference (S3077)
  • NvmlUtilFFM, NvmlUtilJNA, AdlUtil: replace volatile Set/Map fields with AtomicReference (S3077)
  • GuidFFM, MacCentralProcessorFFM, LinuxNetworkParamsFFM, MacNetworkParamsFFM: cast int operands to long before assignment to long parameters (S2184)

Summary by CodeRabbit

  • Bug Fixes
    • Avoided null-reference crashes when printing process information.
    • Corrected partial-file-read handling when detecting installed apps.
    • Fixed buffer/allocation sizing for hostname and memory reads.
    • Adjusted string parsing to handle surrounding double quotes reliably.
    • Improved thread-safety of GPU device and internal cache management.

@coderabbitai

coderabbitai Bot commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6990b055-025f-4031-8c58-e37bd4a4585d

📥 Commits

Reviewing files that changed from the base of the PR and between 75ea0c4 and f02b296.

📒 Files selected for processing (11)
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
  • oshi-common/src/main/java/oshi/util/PrivilegedUtil.java
  • oshi-common/src/main/java/oshi/util/SystemInfoHelper.java
  • oshi-core-java25/src/main/java/oshi/ffm/windows/com/GuidFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.java
  • oshi-core-java25/src/main/java/oshi/software/os/linux/LinuxNetworkParamsFFM.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java
  • oshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.java
  • oshi-core/src/main/java/oshi/util/gpu/AdlUtil.java
  • oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java
✅ Files skipped from review due to trivial changes (6)
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java
  • oshi-core-java25/src/main/java/oshi/ffm/windows/com/GuidFFM.java
  • oshi-core/src/main/java/oshi/util/gpu/AdlUtil.java
  • oshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.java
🚧 Files skipped from review as they are similar to previous changes (4)
  • oshi-core-java25/src/main/java/oshi/software/os/linux/LinuxNetworkParamsFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.java
  • oshi-common/src/main/java/oshi/util/SystemInfoHelper.java
  • oshi-common/src/main/java/oshi/util/PrivilegedUtil.java
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2026-04-11T05:51:49.185Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3139
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.java:36-58
Timestamp: 2026-04-11T05:51:49.185Z
Learning: In OSHI's `LinuxGpuStats` (oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java), the abstract `nvml*` methods (nvmlGetVramUsed, nvmlGetCoreClockMhz, nvmlGetMemoryClockMhz, nvmlGetTemperature, nvmlGetPowerDraw, nvmlGetFanSpeedPercent) are contractually expected to return `-1` (long or double) on failure. The base class applies `if (val >= 0)` guards before using any NVML result and falls through to sysfs/hwmon paths when negative. Subclasses (LinuxGpuStatsJNA, LinuxGpuStatsFFM) correctly pass through the -1 sentinel unchanged — do not flag this as a missing clamp or contract violation.

Applied to files:

  • oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java
📚 Learning: 2026-04-05T05:40:45.797Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:45.797Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java
📚 Learning: 2026-04-06T21:48:11.921Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3122
File: oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java:85-111
Timestamp: 2026-04-06T21:48:11.921Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java` (and `MacUsbDeviceFFM.java`), the `id = 0L` sentinel is intentional when `device.getParentEntry(IOSERVICE)` returns null. It acts as a catch-all controller group for devices whose controller cannot be identified (an edge case that "should never happen" given the IORegistry format). Do not flag this as a bug or suggest skipping such devices; the design intentionally avoids both silently dropping orphaned devices and placing them at the top level without a controller context.

Applied to files:

  • oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java
📚 Learning: 2026-04-06T22:30:07.083Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3122
File: oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java:215-234
Timestamp: 2026-04-06T22:30:07.083Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`, the `found` flag in `getControllerIdByLocation` is intentionally set to `true` as soon as either `vendor-id` or `device-id` is found in a matching service's parent. This is a deliberate design choice: a partial match (one of the two IDs) is preferred over continuing to iterate, because iterating further risks pairing IDs from two different and potentially mismatched services. Do not flag this as a bug or suggest splitting into separate `foundVendor`/`foundProduct` flags.

Applied to files:

  • oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java
🔇 Additional comments (4)
oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java (4)

11-11: AtomicReference import is correct and needed.

Line 11 cleanly introduces the concurrency primitive now used by the cache field and access paths.


74-74: Cache holder refactor looks solid.

Line 74’s AtomicReference<Set<String>> with an empty-set default is a safe non-null publication pattern.


120-123: Enumeration publish path is consistent.

Lines 120–123 correctly publish IDs through the atomic holder and log from the same source of truth.


290-290: Lookup path migration is correct.

Line 290 updates iteration to the atomic cache reference and keeps lookup semantics unchanged.


📝 Walkthrough

Walkthrough

Refactors several cached fields to use AtomicReference holders, fixes partial-file-read handling, tightens null checks for process printing, adjusts FFM address/size numeric types, and tweaks a regex grouping for stripping double quotes. No public APIs were changed.

Changes

Cohort / File(s) Summary
Regex & I/O
oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOperatingSystem.java, oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
Reordered regex grouping for DOUBLE_QUOTES; readFirstBytes(File) now returns a trimmed byte[] when fewer than 8 bytes are read.
AtomicReference Cache Refactor
oshi-common/src/main/java/oshi/util/PrivilegedUtil.java, oshi-core/src/main/java/oshi/util/gpu/AdlUtil.java, oshi-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java, oshi-core-java25/src/main/java/oshi/util/gpu/NvmlUtilFFM.java
Replaced volatile mutable caches with AtomicReference<T> holders initialized to memoized/empty values; getters and cache-clearing use .get()/.set().
Null-safety in process printing
oshi-common/src/main/java/oshi/util/SystemInfoHelper.java
Added early-return checks when current process or re-fetched process is null to avoid dereferences.
FFM numeric-type adjustments
oshi-core-java25/src/main/java/oshi/ffm/windows/com/GuidFFM.java, oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.java, oshi-core-java25/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java, oshi-core-java25/src/main/java/oshi/software/os/linux/LinuxNetworkParamsFFM.java
Changed integer offset/size expressions to use long (casts or L suffix) for memory offsets and buffer sizes in FFM calls.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hopped through bytes and quoted seams,

trimmed the crumbs from tiny streams.
Fields once volatile, now held with care,
Offsets grown long to fit their share.
A little rabbit cheers the safe new lair ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix misc. SonarQube and Coverity findings' accurately describes the primary changes—addressing multiple code quality and security findings from both SonarQube and Coverity across various files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Apr 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 59.45946% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.32%. Comparing base (5d8b429) to head (f02b296).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...mmon/src/main/java/oshi/util/SystemInfoHelper.java 0.00% 2 Missing and 2 partials ⚠️
...ava25/src/main/java/oshi/util/gpu/NvmlUtilFFM.java 0.00% 4 Missing ⚠️
oshi-core/src/main/java/oshi/util/gpu/AdlUtil.java 40.00% 3 Missing ⚠️
...-core/src/main/java/oshi/util/gpu/NvmlUtilJNA.java 25.00% 3 Missing ⚠️
...java/oshi/software/os/mac/MacNetworkParamsFFM.java 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3157      +/-   ##
==========================================
+ Coverage   56.19%   56.32%   +0.13%     
==========================================
  Files         212      212              
  Lines       13485    13491       +6     
  Branches     2193     2195       +2     
==========================================
+ Hits         7578     7599      +21     
+ Misses       4751     4738      -13     
+ Partials     1156     1154       -2     
Flag Coverage Δ
linux ?
macos ?
macos-latest 30.47% <32.43%> (?)
ubuntu-latest 18.01% <35.13%> (?)
windows ?
windows-latest 28.31% <29.72%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Coverity:
- MacInstalledApps: handle return value of FileInputStream.read()
  to avoid potential out-of-range buffer access (CWE-252)
- SystemInfoHelper: null-check getProcess() return before
  dereferencing in printProcesses() (CWE-476)
- PrivilegedUtil: add comment that default FileSystem is a JVM
  singleton that does not need closing (CWE-404 false positive)

SonarQube:
- LinuxOperatingSystem: group regex alternatives with anchors to
  make precedence explicit (S5850)
- PrivilegedUtil: replace volatile Supplier fields with
  AtomicReference (S3077)
- NvmlUtilFFM, NvmlUtilJNA, AdlUtil: replace volatile Set/Map
  fields with AtomicReference (S3077)
- GuidFFM, MacCentralProcessorFFM, LinuxNetworkParamsFFM,
  MacNetworkParamsFFM: cast int operands to long before assignment
  to long parameters (S2184)
@dbwiddis
dbwiddis merged commit 8fa7005 into oshi:master Apr 14, 2026
16 of 17 checks passed
@dbwiddis
dbwiddis deleted the coverity-fix branch April 14, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant