Skip to content

Fix macOS process arguments corrupting after a non-ASCII entry - #3577

Merged
dbwiddis merged 4 commits into
oshi:masterfrom
dbwiddis:mac-procargs-utf8
Aug 4, 2026
Merged

dbwiddis merged 4 commits into
oshi:masterfrom
dbwiddis:mac-procargs-utf8

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Summary

macOS OSProcess#getArguments() and getEnvironmentVariables() corrupt every entry after one containing a non-ASCII character. Both backends walk the KERN_PROCARGS2 sysctl buffer advancing the offset by the decoded String's length — which counts UTF-16 chars — over a buffer of UTF-8 bytes. One multi-byte character makes the entry longer in bytes than in chars, so the next read starts mid-character and everything downstream is misaligned.

Found while sweeping the MacOSProcess twin pair, continuing the normalized JNA/FFM diff that produced #3569. Notably this is not a divergence — the twins are wrong in agreement (JNA advances by arg.length(), FFM by arg.length() + 1, which lands on the null terminator only for single-byte entries), so a twin comparison could not have caught it. It showed up because reading the two side by side made the offset arithmetic the thing under scrutiny.

Reproduction

Against /bin/sh -c 'sleep 400; true' 日本語テスト café plain-ascii-tail on macOS 15 (Apple Silicon):

before after
[3] 日本語テスト 日本語テスト
[4] 語テスト ✗ café
[5] <28><28>スト ✗ plain-ascii-tail

café and plain-ascii-tail were dropped entirely rather than merely mangled. Both backends were damaged identically in kind. An ASCII-only control process with the same shape parsed correctly on both, confirming the trigger.

The fix

Hoist the parse into MacOSProcess.parseProcArgs(byte[], int) in oshi-common, per the "extract the parsing from the acquisition" guidance — each backend keeps only its sysctl call plus a byte-array copy of the returned length, and the shared parser advances to the next null byte and decodes each entry as UTF-8 over its own byte range. That also removes the "skip exec_command by string length" step, which had the same flaw at the head of the buffer.

Only the sysctl-reported size is copied, not the full ARGMAX allocation.

Tests

MacOSProcessTest covers multi-byte arguments and multi-byte environment values, null padding between sections, the exec_path entry that the first argument repeats, the size bound, and the unusable-buffer cases (short buffer, zero/negative/oversized nargs).

Reverting the parser to the char-length advance fails testMultiByteArgsDoNotShiftLaterEntries, so the test is a real guard rather than a passing decoration.

Verification

Full ./mvnw clean spotless:apply sortpom:sort checkstyle:check install forbiddenapis:check javadoc:javadoc reactor build green on JDK 26. Live on an Apple Silicon host, both backends now report arguments identical to ps and to each other, and the self-process environment read is unchanged at 54 entries.

One thing checked and ruled out: the environment map is empty for those child processes, but ps -Eww shows no environment for them either, so that is a macOS restriction OSHI is correctly matching, not a second bug.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed macOS process information handling for command-line arguments and environment variables containing non-ASCII characters.
    • Prevented corruption and incorrect parsing of multibyte UTF-8 text.
    • Improved handling of incomplete, invalid, or unavailable process data with consistent empty results.
    • Preserved argument and environment variable ordering during parsing.
  • Documentation

    • Added a changelog entry for the macOS process parsing fix in version 7.4.4.

getArguments() and getEnvironmentVariables() corrupted every entry after
one containing a non-ASCII character. Both backends walked the sysctl
buffer advancing the offset by the decoded String's length, which counts
UTF-16 chars, over a buffer of UTF-8 bytes. A multi-byte character makes
the entry longer in bytes than in chars, so the next read starts
mid-character and every subsequent entry is misaligned.

Observed on macOS 15 against /bin/sh -c 'sleep 400; true' with the
arguments 日本語テスト, café, plain-ascii-tail. Both backends reported the
first correctly, then "語テスト" and a replacement-character string in
place of the last two, which were dropped entirely. An ASCII-only control
process parsed correctly on both.

The twins were wrong in agreement rather than divergent -- JNA advanced by
arg.length() and FFM by arg.length() + 1, the latter landing on the null
terminator only for single-byte entries -- so a JNA/FFM comparison could
not have caught it.

Hoist the parse into MacOSProcess.parseProcArgs(byte[], int) in
oshi-common, leaving each backend the sysctl call and a byte-array copy of
the returned length. Offsets now advance to the next null byte and each
entry is decoded as UTF-8 over its own byte range, which also removes the
"skip exec_command by string length" step that had the same flaw. Only the
sysctl-reported size is copied, not the full ARGMAX buffer.

Adds MacOSProcessTest covering multi-byte arguments and environment
values, null padding between sections, the exec_path entry, the size
bound, and the unusable-buffer cases. Reverting the parser to the
char-length advance fails the multi-byte test.

Verified on an Apple Silicon host: both backends now report arguments
identical to ps and to each other, and the self-process environment read
is unchanged at 54 entries.
@coderabbitai

coderabbitai Bot commented Aug 4, 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 Plus

Run ID: 9e5c41d0-4247-4443-a38f-23889f1c06d5

📥 Commits

Reviewing files that changed from the base of the PR and between a333bd9 and 6af6f84.

📒 Files selected for processing (1)
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (8)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 21, ubuntu-latest

📝 Walkthrough

Walkthrough

Changes

The macOS KERN_PROCARGS2 parser now uses bounded UTF-8 byte offsets. Shared parsing handles arguments, ordered environment entries, padding, and invalid buffers. JNA and FFM callers use the shared parser. Tests cover multibyte and malformed data.

macOS process argument parsing

Layer / File(s) Summary
Central procargs parser
oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java, CHANGELOG.md
Adds bounded UTF-8 parsing with byte-based offsets, ordered environment results, immutable collections, and the 7.4.4 changelog entry.
Parser test coverage
oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
Tests ASCII and multibyte entries, padding, invalid assignments, size limits, and unusable buffers.
JNA and FFM integration
oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java, oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
Delegates successful procargs reads to parseProcArgs and returns immutable empty results after failures.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • oshi/oshi#3376: Both changes modify shared MacOSProcess parsing and its JNA and FFM subclasses.

Poem

A rabbit found bytes in a macOS stream,
And kept UTF-8 entries clear and clean.
Arguments hop in, environments align,
Nulls stay bounded, results stay fine.
JNA and FFM share the trail—
Safe little parses in every detail.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the macOS process argument corruption fixed by the changeset.
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 Aug 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.78261% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.98%. Comparing base (4227f13) to head (6af6f84).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ain/java/oshi/software/os/mac/MacOSProcessFFM.java 40.00% 3 Missing ⚠️
...ain/java/oshi/software/os/mac/MacOSProcessJNA.java 50.00% 2 Missing and 1 partial ⚠️
...java/oshi/software/common/os/mac/MacOSProcess.java 97.14% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3577      +/-   ##
============================================
- Coverage     80.00%   79.98%   -0.03%     
  Complexity     3846     3846              
============================================
  Files           709      709              
  Lines         24207    24206       -1     
  Branches       4049     4048       -1     
============================================
- Hits          19366    19360       -6     
- Misses         2687     2690       +3     
- Partials       2154     2156       +2     
Flag Coverage Δ
linux 37.58% <73.91%> (+0.14%) ⬆️
macos 38.41% <84.78%> (-0.02%) ⬇️
windows 45.36% <73.91%> (+0.15%) ⬆️

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 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.

Inline comments:
In `@oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java`:
- Around line 250-252: Remove the fixed 1,024-argument cap in MacOSProcess
argument parsing by validating nargs against a bound derived from the available
procargs buffer size, while retaining the positive-count and impossible-count
safeguards. In
oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java lines
250-252, update the guard accordingly; in
oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
lines 111-112, preserve the impossible-count test and add a valid fixture
containing more than 1,024 arguments.
- Around line 255-276: The argument parser in MacOSProcess must preserve empty
argv entries instead of treating all NUL bytes as padding. After the initial
padding following exec_path, consume exactly nargs null-terminated entries,
including zero-length entries, before parsing environment entries; update the
related coverage in MacOSProcessTest.java lines 77-83 to verify this behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 72ee1d6b-7e97-4003-a2c8-ad83b9b355d5

📥 Commits

Reviewing files that changed from the base of the PR and between 96ee7cf and ff61bbd.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
📜 Review details
⏰ Context from checks skipped due to timeout. (8)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🧰 Additional context used
📓 Path-based instructions (10)
**/*.{java,xml,properties,md}

📄 CodeRabbit inference engine (AGENTS.md)

Do not commit generated output, target/, or IDE files.

Files:

  • CHANGELOG.md
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
CHANGELOG.md

📄 CodeRabbit inference engine (AGENTS.md)

Add a Next Release changelog entry only for user-visible changes; omit pure refactors, deduplication, test-only changes, and CI configuration changes.

Files:

  • CHANGELOG.md
**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/*.java: Do not use banned numeric parsing APIs such as Integer.parseInt, Long.parseLong, Double.parseDouble, or Integer.decode; use the corresponding tolerant ParseUtil methods.
Do not import org.slf4j.event.**; use oshi.util.LogLevel instead.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
**/src/test/java/**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Write tests against parsing and transformation methods using fixture data; keep acquisition methods thin. Do not add Mockito, and use JUnit 5 with Hamcrest.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Test hardware-dependent behavior using invariants, plausible ranges, correct types, and ordering rather than fixed machine-specific values; treat zero and empty results as valid where applicable.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
oshi-common/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

oshi-common/src/main/java/**/*.java: Keep oshi-common pure Java: do not reference JNA, FFM (java.lang.foreign), or System.load/loadLibrary; place parsing, conversion, validation, caching, and shared implementation logic here when it is not native-API-specific.
Before adding a general parsing helper, check oshi.util.ParseUtil; use its existing parsing methods or add shared primitives there rather than duplicating parsers in platform drivers.
Read runtime configuration from oshi-common/src/main/resources/oshi.properties through GlobalConfig or Java system properties; do not introduce a separate configuration mechanism.

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
{oshi-common,oshi-core}/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

Use Java 8 language and API compatibility: do not use var, records, switch expressions, text blocks, List.of, Stream.toList(), or other post-Java-8 features.

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
**/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/src/main/java/**/*.java: Keep external imports synchronized with config/import-control.xml, the module's module-info.java, and, when necessary, its Maven configuration.
Provide Javadoc for every public and protected type and method; use imported simple class names rather than fully qualified names in code and Javadoc links.
Use the repository's concurrency annotation (@ThreadSafe, @Immutable, @NotThreadSafe, or @GuardedBy) on new classes, matching the modeled class where appropriate.
Memoize constant or expensive, slowly changing system values with oshi.util.Memoizer.memoize, generally using a Supplier<T> and defaultExpiration() when refresh is required.
Do not duplicate non-native logic between platform implementations; hoist parsing, conversion, filtering, caching, validation, and sentinel handling into oshi-common where possible.

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
oshi-core/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

For changes to an oshi-core implementation, apply the equivalent behavior change to its corresponding oshi-core-ffm twin, or explain why the implementations intentionally differ.

Files:

  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
oshi-core-ffm/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

oshi-core-ffm/src/main/java/**/*.java: For changes to an oshi-core-ffm implementation, apply the equivalent behavior change to its corresponding oshi-core twin, or explain why the implementations intentionally differ.
Do not reference com.sun.jna from FFM implementation sources.

Files:

  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
🧠 Learnings (6)
📚 Learning: 2026-08-03T00:46:14.953Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3566
File: oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdNetworkParams.java:18-22
Timestamp: 2026-08-03T00:46:14.953Z
Learning: In the OSHI Java codebase, do not require duplicate Javadoc on public methods annotated with Override when the inherited Javadoc from the parent declaration adequately documents the method. The general public-member Javadoc requirement should not flag these overrides solely because they lack redundant documentation.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
📚 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/software/common/os/mac/MacOSProcess.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/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
📚 Learning: 2026-07-30T00:35:22.638Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3547
File: oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdFirmware.java:82-82
Timestamp: 2026-07-30T00:35:22.638Z
Learning: In OSHI, treat changes as “API-breaking” only when they affect classes (or relevant API surface) annotated with `PublicAPI`. Public classes/members that are not annotated with `PublicAPI` should be reviewed as internal implementation details, even if they are public (e.g., classes like `oshi.hardware.common.platform.unix.BsdFirmware`). Also, respect intentional visibility decisions used for tests—e.g., methods like `BsdFirmware.parseDmesg(List<String>)` may be intentionally package-private to support tests within the same package; avoid widening visibility or altering signatures unless the related `PublicAPI` contract requires it.

Applied to files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
📚 Learning: 2026-04-06T04:45:47.644Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:47.644Z
Learning: In the oshi/oshi project, only interfaces and classes in the root packages `oshi`, `oshi.hardware`, and `oshi.software.os` are considered the public API. Code in subpackages that represent specific implementations/platforms (e.g., `oshi.software.os.linux`, `oshi.hardware.platform.linux`, and similar subpackages) is implementation detail. When reviewing, do not treat changes to classes in these platform-specific subpackages as public API breaks (for example: making `LinuxOperatingSystem` abstract, narrowing constructor visibility, or adding/removing abstract methods should not be flagged as breaking public API).

Applied to files:

  • oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java
📚 Learning: 2026-08-02T23:56:23.866Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3565
File: oshi-core-ffm/src/main/java/oshi/ffm/platform/unix/PosixLibcFunctions.java:51-56
Timestamp: 2026-08-02T23:56:23.866Z
Learning: For Java FFM bindings in the oshi-core-ffm module, assume a 64-bit JVM and 64-bit operating system. Do not flag 32-bit native ABI or memory-layout compatibility concerns, consistent with the project's JDK 25 support policy, unless that policy changes.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
🪛 ast-grep (0.45.0)
oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java

[warning] 48-48: Use a randomly-generated IV
Context: byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
Note: [CWE-329] Generation of Predictable IV with CBC Mode.

(random-iv)

🔇 Additional comments (3)
CHANGELOG.md (1)

13-13: LGTM!

oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java (1)

144-152: LGTM!

oshi-core/src/main/java/oshi/software/os/mac/MacOSProcessJNA.java (1)

100-110: LGTM!

Two problems in the KERN_PROCARGS2 parser, both confirmed against live
processes on macOS 15.

An empty string is a legal argv entry, but the parser treated every null
as padding between entries, so it skipped over one. A process launched
with argv ["/bin/sh", "-c", "sleep 300; true", "first", "", "third"]
reported five arguments with the empty one missing. Worse than the drop:
nargs is never decremented for the skipped entry, so the argument count
runs one past the end of argv and pulls the first environment entry into
the argument list.

The arguments are contiguous, so consume exactly nargs of them after the
padding that follows exec_path, allowing zero-length entries, and only
then treat nulls as separators while reading the environment.

Separately, the inherited "nargs <= 1024" sanity check discarded the whole
buffer for any process with more arguments, which is not unusual: a shell
glob or a compiler invocation reaches thousands. `/bin/sh -c ... $(seq 1
1200)` reported zero arguments and zero environment variables. Bound nargs
by the bytes actually available instead, since every argument occupies at
least its own null terminator. That still rejects an impossible count from
a corrupt buffer, which the existing test covers.

Adds coverage for an empty argument between two populated ones, for 1999
arguments, and for padding between exec_path and argv[0], which the
fixtures did not previously model.

Verified on an Apple Silicon host: both backends now report six arguments
with the empty slot intact, 1203 arguments for the seq case, and the
non-ASCII case from the previous commit still parses correctly.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java`:
- Around line 263-280: Update the argument and environment parsing in
MacOSProcess so entries whose nextNull result equals limit are not decoded or
added; stop parsing before decode in both loops. Add coverage for a size limit
that truncates the middle of an argument and an environment value, ensuring
neither partial entry is returned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 130db232-24b7-4343-81b1-31d335bce46a

📥 Commits

Reviewing files that changed from the base of the PR and between ff61bbd and c0b5805.

📒 Files selected for processing (2)
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
📜 Review details
⏰ Context from checks skipped due to timeout. (8)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🧰 Additional context used
📓 Path-based instructions (7)
**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/*.java: Do not use banned numeric parsing APIs such as Integer.parseInt, Long.parseLong, Double.parseDouble, or Integer.decode; use the corresponding tolerant ParseUtil methods.
Do not import org.slf4j.event.**; use oshi.util.LogLevel instead.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
**/src/test/java/**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Write tests against parsing and transformation methods using fixture data; keep acquisition methods thin. Do not add Mockito, and use JUnit 5 with Hamcrest.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Test hardware-dependent behavior using invariants, plausible ranges, correct types, and ordering rather than fixed machine-specific values; treat zero and empty results as valid where applicable.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
**/*.{java,xml,properties,md}

📄 CodeRabbit inference engine (AGENTS.md)

Do not commit generated output, target/, or IDE files.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
oshi-common/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

oshi-common/src/main/java/**/*.java: Keep oshi-common pure Java: do not reference JNA, FFM (java.lang.foreign), or System.load/loadLibrary; place parsing, conversion, validation, caching, and shared implementation logic here when it is not native-API-specific.
Before adding a general parsing helper, check oshi.util.ParseUtil; use its existing parsing methods or add shared primitives there rather than duplicating parsers in platform drivers.
Read runtime configuration from oshi-common/src/main/resources/oshi.properties through GlobalConfig or Java system properties; do not introduce a separate configuration mechanism.

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
{oshi-common,oshi-core}/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

Use Java 8 language and API compatibility: do not use var, records, switch expressions, text blocks, List.of, Stream.toList(), or other post-Java-8 features.

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
**/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/src/main/java/**/*.java: Keep external imports synchronized with config/import-control.xml, the module's module-info.java, and, when necessary, its Maven configuration.
Provide Javadoc for every public and protected type and method; use imported simple class names rather than fully qualified names in code and Javadoc links.
Use the repository's concurrency annotation (@ThreadSafe, @Immutable, @NotThreadSafe, or @GuardedBy) on new classes, matching the modeled class where appropriate.
Memoize constant or expensive, slowly changing system values with oshi.util.Memoizer.memoize, generally using a Supplier<T> and defaultExpiration() when refresh is required.
Do not duplicate non-native logic between platform implementations; hoist parsing, conversion, filtering, caching, validation, and sentinel handling into oshi-common where possible.

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
🧠 Learnings (4)
📚 Learning: 2026-08-03T00:46:14.953Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3566
File: oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdNetworkParams.java:18-22
Timestamp: 2026-08-03T00:46:14.953Z
Learning: In the OSHI Java codebase, do not require duplicate Javadoc on public methods annotated with Override when the inherited Javadoc from the parent declaration adequately documents the method. The general public-member Javadoc requirement should not flag these overrides solely because they lack redundant documentation.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
📚 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/software/common/os/mac/MacOSProcess.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/software/common/os/mac/MacOSProcess.java
📚 Learning: 2026-07-30T00:35:22.638Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3547
File: oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdFirmware.java:82-82
Timestamp: 2026-07-30T00:35:22.638Z
Learning: In OSHI, treat changes as “API-breaking” only when they affect classes (or relevant API surface) annotated with `PublicAPI`. Public classes/members that are not annotated with `PublicAPI` should be reviewed as internal implementation details, even if they are public (e.g., classes like `oshi.hardware.common.platform.unix.BsdFirmware`). Also, respect intentional visibility decisions used for tests—e.g., methods like `BsdFirmware.parseDmesg(List<String>)` may be intentionally package-private to support tests within the same package; avoid widening visibility or altering signatures unless the related `PublicAPI` contract requires it.

Applied to files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
🔇 Additional comments (2)
oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java (1)

289-296: LGTM!

oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java (1)

13-13: LGTM!

Also applies to: 37-53, 90-128, 156-159

Both parse loops decoded whatever bytes remained when nextNull found no
terminator before the limit, so a size that cut through the middle of an
entry yielded a fragment: a buffer truncated two bytes into "kept"
returned "kep" as an argument, and one cut inside an environment value
returned a short value under the right key. A truncated value is worse
than a missing one, since a caller cannot tell it is incomplete.

Stop before decoding in both loops when the entry has no terminator.

This is defensive rather than a live fix: the sysctl reports the length it
wrote and the kernel writes whole null-terminated strings, so a
well-formed buffer ends every entry before the limit. Verified on an Apple
Silicon host that real processes are unaffected -- the non-ASCII and empty
argument cases and the self-process environment read are all unchanged on
both backends.

Adds coverage truncating the middle of an argument and the middle of an
environment value; both fail without the guard.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java`:
- Around line 153-155: Update the truncation boundary argument in
MacOSProcess.parseProcArgs so it uses argBuf.length - 7, matching the comment’s
intended two-byte fragment of the "kept" entry; leave the surrounding test setup
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dcab13ac-12b5-424a-afbd-ffa6e430f57e

📥 Commits

Reviewing files that changed from the base of the PR and between c0b5805 and a333bd9.

📒 Files selected for processing (2)
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacOSProcess.java
📜 Review details
⏰ Context from checks skipped due to timeout. (8)
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
  • GitHub Check: Codacy Static Code Analysis
🧰 Additional context used
📓 Path-based instructions (4)
**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/*.java: Do not use banned numeric parsing APIs such as Integer.parseInt, Long.parseLong, Double.parseDouble, or Integer.decode; use the corresponding tolerant ParseUtil methods.
Do not import org.slf4j.event.**; use oshi.util.LogLevel instead.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
**/src/test/java/**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Write tests against parsing and transformation methods using fixture data; keep acquisition methods thin. Do not add Mockito, and use JUnit 5 with Hamcrest.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
**/*Test.java

📄 CodeRabbit inference engine (AGENTS.md)

Test hardware-dependent behavior using invariants, plausible ranges, correct types, and ordering rather than fixed machine-specific values; treat zero and empty results as valid where applicable.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
**/*.{java,xml,properties,md}

📄 CodeRabbit inference engine (AGENTS.md)

Do not commit generated output, target/, or IDE files.

Files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
🧠 Learnings (1)
📚 Learning: 2026-08-03T00:46:14.953Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3566
File: oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdNetworkParams.java:18-22
Timestamp: 2026-08-03T00:46:14.953Z
Learning: In the OSHI Java codebase, do not require duplicate Javadoc on public methods annotated with Override when the inherited Javadoc from the parent declaration adequately documents the method. The general public-member Javadoc requirement should not flag these overrides solely because they lack redundant documentation.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java
🔇 Additional comments (1)
oshi-common/src/test/java/oshi/software/common/os/mac/MacOSProcessTest.java (1)

1-64: LGTM!

Also applies to: 66-145, 147-152, 156-163, 165-182

The mid-argument truncation case cut 6 bytes from the end, leaving a
three-byte fragment "kep" where the comment claims two. Cut 7 so the
fragment is "ke", matching the description. Same code path either way;
removing the guard still fails the assertion, now with the fragment the
comment names.
@dbwiddis
dbwiddis merged commit b2f5ce3 into oshi:master Aug 4, 2026
16 checks passed
@dbwiddis
dbwiddis deleted the mac-procargs-utf8 branch August 4, 2026 04: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