Skip to content

Decode /proc/net addresses in the host's byte order on Linux - #3852

Merged
dbwiddis merged 1 commit into
masterfrom
linux-big-endian-addresses
Oct 31, 2026
Merged

dbwiddis merged 1 commit into
masterfrom
linux-big-endian-addresses

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Problem

Linux prints IPv4 addresses in /proc/net/route, and each 32-bit word of the addresses in /proc/net/tcp, tcp6, udp and udp6, with %08X of a __be32. The digits therefore appear in the host's byte order. OSHI's parsers assumed a little-endian host, so on big-endian Linux every address came out byte-reversed. Measured on an s390x host (cfarm191, Debian 13):

Kernel says OSHI reported
Default gateway 148.100.74.1 1.74.100.148
148.100.74.0/23 /23 0.74.100.148/-1
Loopback listener 127.0.0.1 1.0.0.127
IPv6 loopback ::1 ::100:0

/proc/net/ipv6_route is printed byte by byte, so IPv6 routes were already correct.

Fix

  • New ParseUtil.hexWordsToByteArray(String, ByteOrder) decodes hex text of 32-bit words printed in a given byte order.
  • RouteTable.parseIpv4Routes and a new LinuxInternetProtocolStats.parseConnections seam take a ByteOrder; production passes ByteOrder.nativeOrder(). The previous unconditional per-word reversal is gone.
  • Fixtures in both byte orders, each checked against ip route / ss -tna from the same moment: an aarch64 container (with 2001:db8:1122:3344::1 added so the byte order is unambiguous) and the s390x host. The existing route fixtures now state their byte order explicitly.

The other parseIntToIP callers are Windows and macOS, which are always little-endian, and are unchanged.

Found by running the suite on s390x

  • Util.is64Bit() was false on s390x and riscv64 under HotSpot: it read only sun.cpu.bits, which HotSpot does not set, and its os.arch fallback list lacked both. It now reads sun.arch.data.model (set by HotSpot and OpenJ9) first. getBitness() has its own fallback, so no caller's result changed visibly.
  • AuxvTest guessed the native long size from its own os.arch list; it now uses Util.is64Bit().
  • SysfsTest assumed every Linux machine has DMI; it now skips when /sys/devices/virtual/dmi/id is absent (s390x, many ARM boards).

Verification

  • Locally: gate (spotless, checkstyle, install with tests, forbidden-apis, javadoc) and clean install -Perror-prone.
  • On cfarm191 (s390x, Temurin 25.0.5): ./mvnw test green across the reactor (oshi-common 1946 tests, oshi-core, oshi-core-ffm). Live getConnections() now matches ss.

Not addressed here, found by -Paggregate-coverage on the same host: the native-free backend has no HWCAP reader, so getProcessorID() differs from JNA on non-x86/non-ARM CPUs, and it reports "" rather than unknown for a virtio disk with no model. Separate follow-ups.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed IPv4 and IPv6 addresses being reported with reversed bytes when reading Linux network routes and connections on big-endian systems, including s390x.
    • Network address reporting now handles byte order correctly across supported Linux architectures, improving the accuracy of route and connection details.

Linux prints an IPv4 address in /proc/net/route, and each 32-bit word of an
address in /proc/net/tcp, tcp6, udp and udp6, with %08X of a __be32, so the
digits come out in the host's byte order. Both parsers assumed a little-endian
host, so on big-endian Linux such as s390x every route destination, gateway
and mask, and every connection address, was byte-reversed: 127.0.0.1 read as
1.0.0.127, and a /23 mask as prefix length -1.

Add ParseUtil.hexWordsToByteArray(String, ByteOrder) and pass the native byte
order from RouteTable and from a new LinuxInternetProtocolStats.parseConnections
seam, with fixtures captured on an aarch64 container and an s390x host.
/proc/net/ipv6_route prints byte by byte and was already correct.

Running the suite on s390x found three more problems. Util.is64Bit() read only
sun.cpu.bits, which HotSpot does not set, and its os.arch fallback lacked s390x
and riscv64; it now reads sun.arch.data.model first. AuxvTest guessed the word
size from its own os.arch list, and SysfsTest assumed every Linux machine has
DMI. Verified on cfarm191 (s390x, Debian 13): all oshi-common, oshi-core and
oshi-core-ffm tests pass, and getConnections() matches ss.
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4a19c285-fa99-493c-9a9c-97ea049c3744
Commits

Reviewing files that changed from the base of the PR and between 9480cbc and 267fb35.

Files selected for processing: 10
  • CHANGELOG.md
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/main/java/oshi/util/Util.java
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
  • oshi-common/src/test/java/oshi/util/ParseUtilTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Recent review details
Context from checks skipped due to timeout: 16
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, DragonFly BSD
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Test JDK 25, NetBSD
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Error Prone
  • GitHub Check: Analyze (java-kotlin)
Additional context used
Code guidelines: 2
AGENTS.md — auto-discovered
.github/CONTRIBUTING.md — auto-discovered
Path-based instructions: 8
Source excerpt: **Language level.**

CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/main/java/oshi/util/Util.java
  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any `System.load`/`loadLibrary`.

CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-common/src/test/java/oshi/util/ParseUtilTest.java
  • oshi-common/src/main/java/oshi/util/Util.java
  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
  • oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `Integer.parseInt`, `Long.parseLong`, `Double.parseDouble`, `Integer.decode`, and relatives are banned.

CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/main/java/oshi/util/Util.java
  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
Source excerpt: **Their `src/test` compiles at `release 17`**, set by `maven.compiler.testRelease` in the root `pom.xml`.

CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-common/src/test/java/oshi/util/ParseUtilTest.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java
Source excerpt: Text blocks strip incidental leading and trailing whitespace, and spotless applies `trimTrailingWhitespace` to Java sources, so a fixture whose real output carries trailing spaces or tab alignment needs explicit `\s` or `\t`...

CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-common/src/test/java/oshi/util/ParseUtilTest.java
  • oshi-common/src/main/java/oshi/util/Util.java
  • oshi-common/src/main/java/oshi/util/ParseUtil.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
  • oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
Source excerpt: **Adding a test in a package that no existing test uses means adding a line to that module's `module-info.test`:**

CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
  • oshi-common/src/test/java/oshi/util/ParseUtilTest.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java
  • oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java
Applicability reference: AGENTS.md: # AGENTS.md Instructions for AI coding agents working in the OSHI repository.

CodeRabbit inference engine (.github/CONTRIBUTING.md)

Files:

  • CHANGELOG.md
Source excerpt: An entry is one bullet, on **one line however long it runs** — the longest in the file is over a thousand characters.

CodeRabbit inference engine (AGENTS.md)

Files:

  • CHANGELOG.md
Learnings: 5
Learning: 2026-10-31T02:28:12.554Z
Learnt from: CR
Repo: oshi/oshi PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-31T02:28:12.554Z
Learning: Applies to CHANGELOG.md : Source excerpt:
# AGENTS.md

## Pull request conventions

### The CHANGELOG

An entry is one bullet, on **one line however long it runs** — the longest in the file is over a
thousand characters. Spotless formats `.md` only by trimming trailing whitespace and normalizing
indentation to two spaces, so nothing wraps it for you and nothing stops you wrapping it by hand.
Do not: a hard-wrapped entry reads as a diff against every neighbour.

```markdown
* [#3837](https://github.com/oshi/oshi/pull/3837): Reject IPv6 address text with a dotted IPv4 component before compressed zero groups - [PHJ2000](https://github.com/PHJ2000).
```

Applied to files:

  • CHANGELOG.md
Learning: 2026-10-31T02:28:12.554Z
Learnt from: CR
Repo: oshi/oshi PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-31T02:28:12.554Z
Learning: Applies to {oshi-common,oshi-core,oshi-core-ffm,oshi-metrics,oshi-benchmark}/src/main/java/**/*.java : Source excerpt:
# AGENTS.md

## Hard rules that CI enforces

**Language level.** `oshi-common` and `oshi-core` compile **main** sources with `release 8`. No
`var`, no `List.of`, no records, no switch expressions, no text blocks, no `Stream.toList()`. Only
`oshi-core-ffm` (25), `oshi-metrics` (17), and `oshi-benchmark` (25) get modern Java in main
sources. Agents get this wrong constantly.

Applied to files:

  • oshi-common/src/main/java/oshi/util/ParseUtil.java
Learning: 2026-08-15T04:17:23.599Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3620
File: oshi-common/src/test/java/oshi/util/driver/linux/DmidecodeTest.java:65-66
Timestamp: 2026-08-15T04:17:23.599Z
Learning: In OSHI Java fixture parser tests that use fixed command-output constants, exact expected-value assertions are appropriate because they verify the parser contract. The test guideline to assert invariants rather than specific values applies to host-dependent live readings, not deterministic fixture parsing.

Applied to files:

  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
Learning: 2026-10-31T02:28:12.554Z
Learnt from: CR
Repo: oshi/oshi PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-31T02:28:12.554Z
Learning: Applies to **/src/main/java/**/*.java : Source excerpt:
# AGENTS.md

## Hard rules that CI enforces

**Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`):

- `Integer.parseInt`, `Long.parseLong`, `Double.parseDouble`, `Integer.decode`, and relatives are
  banned. Use `oshi.util.ParseUtil` — `parseIntOrDefault`, `parseLongOrDefault`,
  `parseHyphenatedIntList`, `parseDHMSOrDefault`, and so on. They fail soft on the malformed input
  that real system output regularly produces.

Applied to files:

  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
Learning: 2026-10-31T02:28:12.554Z
Learnt from: CR
Repo: oshi/oshi PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-31T02:28:12.554Z
Learning: Applies to oshi-common/src/**/*.java : Source excerpt:
# AGENTS.md

## Hard rules that CI enforces

**Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`):

- `oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any
  `System.load`/`loadLibrary`. It must stay pure JDK.

Applied to files:

  • oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java
Additional comments: 10
oshi-common/src/main/java/oshi/util/Util.java: 1

34-35: LGTM!

Also applies to: 42-43

oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java: 1

18-18: LGTM!

Also applies to: 23-24

oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java: 1

11-13: LGTM!

Also applies to: 19-19, 45-46

oshi-common/src/main/java/oshi/util/ParseUtil.java: 1

343-369: LGTM!

oshi-common/src/test/java/oshi/util/ParseUtilTest.java: 1

22-22: LGTM!

Also applies to: 188-205

oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java: 1

7-7: LGTM!

Also applies to: 103-105, 108-118, 120-120, 124-125, 129-130, 137-137, 140-141

oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java: 1

12-12: LGTM!

Also applies to: 15-15, 21-22, 25-25, 66-95, 157-197

CHANGELOG.md: 1

8-8: LGTM!

oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java: 1

7-7: LGTM!

Also applies to: 22-23, 46-46, 53-54, 58-58, 61-61, 74-74, 77-78, 118-118

oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java: 1

11-11: LGTM!

Also applies to: 30-30, 39-50, 73-73, 95-118, 175-175, 181-182, 184-185

Walkthrough

Linux route and connection parsing now decodes IPv4 and IPv6 address words according to byte order. The change adds tests for little- and big-endian data, updates 64-bit architecture detection, and adds a DMI-directory assumption to a sysfs test.

Changes: Linux address parsing

Layer / File(s) Summary
Connection address decoding
oshi-common/src/main/java/oshi/util/ParseUtil.java, oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java, oshi-common/src/test/java/oshi/util/ParseUtilTest.java, oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
hexWordsToByteArray decodes hexadecimal words using the supplied byte order. Linux connection parsing passes native byte order through address parsing. Tests cover IPv4 and IPv6 data in both byte orders, including connection fields and process lookup.
Route address decoding
oshi-common/src/main/java/oshi/util/driver/linux/proc/RouteTable.java, oshi-common/src/test/java/oshi/util/driver/linux/proc/RouteTableTest.java, CHANGELOG.md
IPv4 route parsing receives native byte order and decodes destination, mask, and gateway addresses accordingly. Tests cover little-endian and big-endian routes. The changelog records the address-reporting fix.
64-bit architecture detection
oshi-common/src/main/java/oshi/util/Util.java, oshi-common/src/test/java/oshi/util/driver/linux/proc/AuxvTest.java
Bitness detection checks sun.arch.data.model before sun.cpu.bits and recognizes s390x and riscv64 when those properties are absent. The Auxv test selects its reader through Util.is64Bit().

Sysfs test assumption

Layer / File(s) Summary
DMI directory check
oshi-common/src/test/java/oshi/util/driver/linux/SysfsTest.java
The non-null sysfs query test is skipped when SysPath.DMI_ID is not a directory.

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

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Minimal · up to 267fb

The address decoding change has no identified merge-blocking issue.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed 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 states the main change: Linux /proc/net address decoding now uses the host's byte order, which matches the changeset. The raw summary shows ParseUtil.hexWordsToByteArray(String, ByteOrder), …
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.
Full details: Title check

Explanation

The title states the main change: Linux /proc/net address decoding now uses the host's byte order, which matches the changeset. The raw summary shows ParseUtil.hexWordsToByteArray(String, ByteOrder), native-order use in LinuxInternetProtocolStats and RouteTable, and tests for both little- and big-endian fixtures. The title is specific, concise, and not vague. It does not mention the Util.is64Bit or SysfsTest changes, which is acceptable because they are secondary.

Full details: Docstring Coverage

Explanation

Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 7 functions across 9 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit reads the routes with care,
And sorts each byte with endian flair.
IPv4 and IPv6 now align,
While tests check every word and sign.
I nibble greens and hop along.

@dbwiddis
dbwiddis merged commit 5154264 into master Oct 31, 2026
22 of 23 checks passed
@dbwiddis
dbwiddis deleted the linux-big-endian-addresses branch October 31, 2026 05:09
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