Repository navigation
Return -1 from parseHertz instead of throwing on a non-decimal separator - #3581
Conversation
HERTZ_PATTERN's decimals group is "(.\d+)?" with an unescaped dot, so it matches any separator, not a decimal point. A frequency written with a comma -- which a localized string can carry -- put "2,40" in the value group, and Double.valueOf then threw NumberFormatException out of a method documented to return -1 when it cannot parse. parseHertz is reached from ProcessorIdentifier's vendor frequency, MacGlobalMemory's speed, and the Linux, NetBSD, OpenBSD and AIX CPU frequency parsers, all of which read strings OSHI does not control. Parse through ParseUtil.parseDoubleOrDefault, the existing soft-fail helper, and require the unit multiplier to be positive rather than inferring failure from a negative product. The unescaped dot is left as is, and a comment now says why: matching any separator pulls the malformed number into the value group whole, where the parse fails and the documented -1 is returned. Escaping it looks like a cleanup but is a regression -- the match re-anchors on a fragment, so "2 40GHz" is reported as a 40 GHz processor and "2X00MHz" as 0 rather than as unparseable. The inline comment claiming the pattern made a NumberFormatException impossible was simply wrong, and is gone. testParseHertzNonDecimalSeparator covers the comma, space and letter separators along with the decimal cases that must keep working. It fails both ways: restoring Double.valueOf throws, and escaping the dot returns 40000000000. Also narrows the #3579 changelog entry. It claimed the macOS FFM strings were mangled; probing every affected field on an Apple Silicon host before and after that change gives byte-identical output, because those fields are single-string and unpadded there. The entry now says it aligns the backends and describes the conditions under which the old decode would corrupt.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📜 Recent review details⏰ Context from checks skipped due to timeout. (8)
🧰 Additional context used📓 Path-based instructions (8)**/*.{java,xml,properties,md}📄 CodeRabbit inference engine (AGENTS.md)
Files:
CHANGELOG.md📄 CodeRabbit inference engine (AGENTS.md)
Files:
oshi-common/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
{oshi-common,oshi-core}/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/src/main/java/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/src/test/java/**/*Test.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*Test.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (4)📚 Learning: 2026-04-12T19:22:09.716ZApplied to files:
📚 Learning: 2026-07-06T19:25:59.225ZApplied to files:
📚 Learning: 2026-07-30T00:35:22.638ZApplied to files:
📚 Learning: 2026-08-03T00:46:14.953ZApplied to files:
🔇 Additional comments (3)
📝 WalkthroughWalkthrough
ChangesparseHertz parsing
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3581 +/- ##
============================================
- Coverage 80.00% 79.96% -0.05%
Complexity 3869 3869
============================================
Files 709 709
Lines 24206 24207 +1
Branches 4047 4047
============================================
- Hits 19366 19356 -10
- Misses 2689 2698 +9
- Partials 2151 2153 +2
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:
|
Summary
Fixes CodeQL Code Quality finding #632 (
java/uncaught-number-format-exception,ParseUtil.java:216) — which turns out to be real, having been dismissed as a false positive in two earlier triage passes on the strength of this comment:The comment is wrong. The pattern is
(\d+(.\d+)?) ?([kKMGT]?Hz).*and the dot is not escaped, so it matches any character rather than a decimal point:parseHertzis documented to return -1 when it cannot parse, so this throws where it promises a sentinel. It is reached fromProcessorIdentifier's vendor frequency,MacGlobalMemory's speed, and the Linux/NetBSD/OpenBSD/AIX CPU-frequency parsers — all reading strings OSHI does not control. A comma decimal separator is the realistic trigger.The fix, and the fix that was not made
Parse through
ParseUtil.parseDoubleOrDefault, the existing soft-fail helper, and require the unit multiplier to be positive instead of inferring failure from a negative product.The unescaped dot is deliberately left alone, with a comment explaining why. Escaping it is the obvious-looking cleanup and it is a regression. I wrote that version first and measured it:
.(kept)\."1.80GHz""2,40GHz""2 40GHz""2X00MHz"Matching any separator pulls the malformed number into the value group whole, where the parse fails and -1 comes back. Escaping lets
find()re-anchor on a fragment and return a plausible but wrong frequency — a 40 GHz processor. The accidental behavior is the correct one, so it is now pinned by tests and documented.Tests
testParseHertzNonDecimalSeparatorcovers comma, space, and letter separators plus the decimal cases that must keep working. It is a guard in both directions:Double.valueOf→NumberFormatException: For input string: "2,40"Expected: is <-1L> but: was <40000000000L>Also in this PR
Narrows the #3579 CHANGELOG entry. It claimed the macOS FFM firmware/baseboard/computer-system/CPU strings were mangled; probing every affected field on an Apple Silicon host at
bf7f1783ad^and at master gives byte-identical output, because those fields are single-string and unpadded there, and the one genuine multi-string property on that host ("compatible" = <"J416cAP","Mac14,6","AppleARM">) is read through the path #3579 deliberately left alone. The entry now says it aligns the backends and states the conditions under which the old decode would corrupt.Per maintainer,
Double.valueOfis not being added toforbidden-apis.txt— this was its only use in main code, and the site would have been exempted anyway.Verification
Full
./mvnw clean spotless:apply sortpom:sort checkstyle:check install forbiddenapis:check javadoc:javadocgreen on JDK 26: 1311/119/96 tests, 0 failures, 0 checkstyle violations.Summary by CodeRabbit
Bug Fixes
-1instead of causing parsing errors.Documentation