Skip to content

Fix native library loading failure due to buffer comparison bug - #318

Merged
gnodet merged 1 commit into
masterfrom
fix-contentsEquals-buffer-comparison
Mar 27, 2026
Merged

Fix native library loading failure due to buffer comparison bug#318
gnodet merged 1 commit into
masterfrom
fix-contentsEquals-buffer-comparison

Conversation

@gnodet

@gnodet gnodet commented Mar 27, 2026

Copy link
Copy Markdown
Member

Summary

  • Fix contentsEquals() in JansiLoader to compare only the bytes actually read from each stream, instead of comparing the entire 8192-byte buffer via Arrays.equals()
  • On the final chunk of a stream, bytes beyond the read count contain stale data from the previous iteration, causing a false "Content differs" mismatch that prevents the native library from loading
  • Uses a JDK 8-compatible byte-by-byte loop instead of the JDK 9+ Arrays.equals(byte[], int, int, byte[], int, int) overload

Fixes #317

Test plan

  • Verified the project compiles successfully
  • Test on JDK 21 + Fedora/Linux to confirm native library loads and ANSI color output works
  • Test on JDK 8 to confirm backwards compatibility

🤖 Generated with Claude Code

Claude Code on behalf of Guillaume Nodet

…son (#317)

Compare only the bytes actually read from each stream instead of the
entire 8192-byte buffer in contentsEquals(). When the final chunk is
smaller than the buffer, stale bytes from the previous iteration could
cause a false mismatch, preventing the native library from loading.

Uses a byte-by-byte loop instead of Arrays.equals() to maintain JDK 8
compatibility.

Fixes #317

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet
gnodet merged commit 300aa78 into master Mar 27, 2026
6 checks passed
@gnodet
gnodet deleted the fix-contentsEquals-buffer-comparison branch March 27, 2026 12:23
sarutak pushed a commit to apache/spark that referenced this pull request Apr 18, 2026
### What changes were proposed in this pull request?

This PR upgrades `Maven` to 3.9.15.

### Why are the changes needed?

To use the latest Maven version.
- https://maven.apache.org/docs/3.9.15/release-notes.html
  - apache/maven#11876
    - codehaus-plexus/plexus-utils#304
  - apache/maven#11865
    - fusesource/jansi#318
    - fusesource/jansi#317
- https://github.com/apache/maven/releases/tag/maven-3.9.15

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Pass the CIs and check the log.
- https://github.com/dongjoon-hyun/spark/actions/runs/24586452868/job/71897297495

```
Using `mvn` from path: /home/runner/work/spark/spark/build/apache-maven-3.9.15/bin/mvn
```

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (claude-opus-4-7)

Closes #55398 from dongjoon-hyun/SPARK-56530.

Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: Kousuke Saruta <sarutak@apache.org>
breskeby added a commit to elastic/elasticsearch that referenced this pull request Aug 4, 2026
JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes #154510
breskeby added a commit to elastic/elasticsearch that referenced this pull request Aug 4, 2026
…ux (#155853)

JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes #154510
breskeby added a commit to elastic/elasticsearch that referenced this pull request Aug 4, 2026
…ux (#155853) (#155893)

JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes #154510

(cherry picked from commit 8a98623)

# Conflicts:
#	gradle/verification-metadata.xml
#	muted-tests.yml
breskeby added a commit to elastic/elasticsearch that referenced this pull request Aug 4, 2026
…ux (#155853) (#155882)

JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes #154510

(cherry picked from commit 8a98623)

# Conflicts:
#	muted-tests.yml
breskeby added a commit to elastic/elasticsearch that referenced this pull request Aug 4, 2026
…ux (#155853) (#155884)

JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes #154510

(cherry picked from commit 8a98623)

# Conflicts:
#	gradle/verification-metadata.xml
#	muted-tests.yml
breskeby added a commit to elastic/elasticsearch that referenced this pull request Aug 4, 2026
…ux (#155853) (#155886)

JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes #154510

(cherry picked from commit 8a98623)

# Conflicts:
#	gradle/verification-metadata.xml
#	muted-tests.yml
tveasey pushed a commit to tveasey/elasticsearch that referenced this pull request Aug 6, 2026
…ux (elastic#155853)

JANSI 2.4.0's JansiLoader.extractAndLoadLibraryFile verifies the native
library it extracts by comparing the jar resource stream against the
extracted file using Arrays.equals on reused 8192-byte buffers without
limiting the comparison to the bytes actually read. On Linux systems
where the JDK dynamically links the system zlib (the openjdk.org GA
builds bundled with Elasticsearch for new major JDK versions), this
latent bug is triggered by zlib-ng: EL10-family distros (RHEL 10,
AlmaLinux 10, Rocky Linux 10, Oracle Linux 10) ship zlib-ng-compat as
libz.so.1, whose SIMD-optimized inflate writes scratch bytes beyond the
reported read count. The stale byte in the reused buffer then falsely
signals corruption, JANSI throws a RuntimeException that is silently
swallowed (jansi.graceful=true), CLibrary.LOADED stays false, and
AnsiConsole reports AnsiType.Unsupported with terminal width 0.
AnsiConsoleLoader consequently returns null instead of a valid console,
and InitialNodeSecurityAutoConfiguration skips credential generation at
first startup.

JANSI 2.4.3 fixes the comparison to only consider bytes actually read
(fusesource/jansi#318).

Closes elastic#154510
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.

No color output in Fedora and JDK21

1 participant