Skip to content

Add ParseUtil decode methods and forbid direct JDK parse/decode calls - #3437

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:refactor/forbidden-parse-methods
Jul 8, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:refactor/forbidden-parse-methods

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 8, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add ParseUtil.decodeIntOrDefault(String, int) and ParseUtil.decodeLongOrDefault(String, long) — safe wrappers around Integer.decode/Long.decode with null handling and default values
  • Forbid direct use of Integer.parseUnsignedInt, Long.parseUnsignedLong, Integer.decode, and Long.decode via config/forbidden-apis.txt, enforcing that all number parsing goes through ParseUtil's safe wrappers
  • Refactor existing callers to use ParseUtil methods

Test plan

  • New unit tests for decodeIntOrDefault (hex, decimal, octal, null, invalid)
  • New unit tests for decodeLongOrDefault (same cases)
  • forbiddenapis:check passes across all modules
  • Full install passes (all existing tests green)

Summary by CodeRabbit

  • New Features

    • Added more resilient numeric parsing helpers for decoded values, improving handling of decimal, hex, and octal formats.
  • Bug Fixes

    • Made processor information detection more fault-tolerant on FreeBSD when reading system data.
    • Reduced parsing failures by falling back to safe default values when numeric conversion is invalid or missing.
  • Tests

    • Expanded coverage for numeric decoding and processor-ID parsing edge cases.

Add decodeIntOrDefault and decodeLongOrDefault to ParseUtil, wrapping
Integer.decode/Long.decode with null/exception safety and a default value.

Forbid direct use of Integer.parseUnsignedInt, Long.parseUnsignedLong,
Integer.decode, and Long.decode via forbidden-apis config, enforcing
that all parsing goes through ParseUtil's safe wrappers.

Refactor existing callers:
- FreeBsdCentralProcessor: Long.decode/Integer.decode → ParseUtil
- AbstractCentralProcessorTest: Long.parseUnsignedLong → ParseUtil
- ParseUtilTest: Integer.parseUnsignedInt → ParseUtil
@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 312ac1a0-0332-4c28-bfe9-f2432b176edc

📥 Commits

Reviewing files that changed from the base of the PR and between d22319e and 77e0e43.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • config/forbidden-apis.txt
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/test/java/oshi/hardware/common/AbstractCentralProcessorTest.java
  • oshi-common/src/test/java/oshi/util/ParseUtilTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.

Applied to files:

  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.

Applied to files:

  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
📚 Learning: 2026-04-12T19:34:56.879Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:34:56.879Z
Learning: In OSHI’s Java platform implementations, `PhysicalProcessor.getEfficiency()` / `EfficiencyClass` follow the same convention as Windows `PROCESSOR_RELATIONSHIP.EfficiencyClass`: a *higher* numeric efficiency class value represents *greater performance* and *less efficiency* (typically P-cores higher than E-cores). When reviewing, do not treat cases where P-cores are assigned higher efficiency/class values as a bug—this is intentional cross-platform consistency.

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
🔇 Additional comments (7)
oshi-common/src/main/java/oshi/util/ParseUtil.java (1)

530-563: LGTM!

oshi-common/src/test/java/oshi/util/ParseUtilTest.java (2)

749-752: LGTM!


994-1012: LGTM!

config/forbidden-apis.txt (1)

11-20: LGTM!

CHANGELOG.md (1)

14-14: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java (1)

80-88: LGTM!

oshi-common/src/test/java/oshi/hardware/common/AbstractCentralProcessorTest.java (1)

33-33: LGTM!

Also applies to: 130-131, 133-139, 146-146


📝 Walkthrough

Walkthrough

Adds ParseUtil.decodeIntOrDefault/decodeLongOrDefault helpers that safely decode strings with fallback defaults, forbids direct use of Integer/Long decode and unsigned-parsing APIs via forbidden-apis config, migrates FreeBsdCentralProcessor and tests to the new helpers, and updates the changelog.

Changes

Safe Decoding Utilities and Adoption

Layer / File(s) Summary
New decode helpers
oshi-common/src/main/java/oshi/util/ParseUtil.java, oshi-common/src/test/java/oshi/util/ParseUtilTest.java
Adds decodeIntOrDefault and decodeLongOrDefault, which decode strings and return a default on failure, with new unit tests covering hex, decimal, null, and overflow cases.
Forbidden API entries
config/forbidden-apis.txt
Adds forbidden-API entries for Integer.decode, Long.decode, Integer.parseUnsignedInt(...), and Long.parseUnsignedLong(...).
FreeBSD processor ID parsing
oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdCentralProcessor.java
queryProcessorId() uses the new decode-with-default helpers instead of throwing Integer.decode/Long.decode calls.
Existing test updates
oshi-common/src/test/java/oshi/hardware/common/AbstractCentralProcessorTest.java, oshi-common/src/test/java/oshi/util/ParseUtilTest.java
Replaces Long.parseUnsignedLong/Integer.parseUnsignedInt usage in tests with ParseUtil.hexStringToLong.
Changelog entry
CHANGELOG.md
Documents the new helpers and the restriction on direct decode/unsigned parsing methods for PR #3437.

Estimated code review effort: 2 (Simple) | ~12 minutes

Possibly related PRs

  • oshi/oshi#3380: Both PRs modify FreeBsdCentralProcessor.queryProcessorId()'s dmesg.boot bit-parsing logic.

Poem

A rabbit hopped through hex and dec,
No more crashes to inspect,
decodeOrDefault, safe and neat,
Parsing bits without defeat 🐇
Forbidden APIs now marked "no way",
Hop on, dear code, a safer day!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: new ParseUtil decode helpers plus forbidding direct JDK parse/decode calls.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@codecov

codecov Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.53846% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.13%. Comparing base (bff334f) to head (77e0e43).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...platform/unix/freebsd/FreeBsdCentralProcessor.java 0.00% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3437      +/-   ##
============================================
- Coverage     75.20%   75.13%   -0.08%     
  Complexity       76       76              
============================================
  Files           580      580              
  Lines         21609    21617       +8     
  Branches       3588     3588              
============================================
- Hits          16252    16242      -10     
- Misses         3329     3340      +11     
- Partials       2028     2035       +7     
Flag Coverage Δ
freebsd 26.97% <61.53%> (+0.02%) ⬆️
linux 31.57% <61.53%> (+0.01%) ⬆️
macos 31.79% <61.53%> (-0.02%) ⬇️
openbsd 26.45% <61.53%> (+0.02%) ⬆️
solaris 28.51% <61.53%> (+0.04%) ⬆️
windows 40.03% <61.53%> (-0.02%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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.

@dbwiddis
dbwiddis merged commit 7c8ad27 into oshi:master Jul 8, 2026
20 checks passed
@dbwiddis
dbwiddis deleted the refactor/forbidden-parse-methods branch July 8, 2026 20:57
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