question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ByteBuf.order() seems to have wrong behavior

See original GitHub issue

Found unexpected behavior when calling

Unpooled.buffer(1024).order(ByteOrder.LITTLE_ENDIAN).writeShortLE(message)

since netty 4.1.54.Final.

The order() method has already been deprecated, but nevertheless it shouldn’t be broken.

Expected behavior

Test passes

Actual behavior

Test failed

expected: -12208
but was : 20688
	at NettyEndianTest.endianTestCallingOrder(NettyEndianTest.java:26)

Steps to reproduce

Create the following project

./build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'com.google.truth:truth:1.0.1'
    testImplementation 'io.netty:netty-buffer:4.1.68.Final'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

./src/NettyEndianTest.java

import static com.google.common.truth.Truth.assertThat;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.ByteOrder;
import org.junit.jupiter.api.Test;

class NettyEndianTest {

  // Test will fail for netty 4.1.54.Final and above 
  @SuppressWarnings("deprecation")
  @Test
  void endianTestCallingOrder() {
    short message = (short) 0xD050;
    short reverse = (short) 0x50D0;
    ByteBuf b = Unpooled.buffer(1024).order(ByteOrder.LITTLE_ENDIAN).writeShortLE(message);

    assertThat(b.getShortLE(0)).isEqualTo(message);
    assertThat(b.getShort(0)).isEqualTo(reverse);
  }

  // Test is passing
  @Test
  void endianTestWithoutCallingOrder() {
    short message = (short) 0xD050;
    short reverse = (short) 0x50D0;
    ByteBuf b = Unpooled.buffer(1024).writeShortLE(message);

    assertThat(b.getShortLE(0)).isEqualTo(message);
    assertThat(b.getShort(0)).isEqualTo(reverse);
  }
}

Run gradle test

Minimal yet complete reproducer code (or URL to code)

Netty version

4.1.54.Final - 4.1.68.Final

Earlier versions before 4.1.54 were good.

JVM version (e.g. java -version)

openjdk version “11.0.11” 2021-04-20 LTS OpenJDK Runtime Environment Zulu11.48+21-CA (build 11.0.11+9-LTS) OpenJDK 64-Bit Server VM Zulu11.48+21-CA (build 11.0.11+9-LTS, mixed mode)

Other JVMs the same.

OS version (e.g. uname -a)

Darwin Kernel Version 20.6.0 Other platforms the same.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisvestcommented, Sep 23, 2021

Opened #11706 to fix this.

@dapengzhang0 Note the tests I added to UnpooledTest are different from the ones you posted above. Specifically, the test you said was failing is now making the opposite assertion, namely that LE methods always perform little-endian accesses.

1reaction
ejona86commented, Sep 22, 2021

Looks like the entirety of SwappedByteBuf needs an audit. But it looks like it has always been broken https://github.com/netty/netty/commit/0f9492c9affc528c766f9677952412564d4a3f6d . It seems what changed is https://github.com/netty/netty/commit/96da45de2d53d74ad3a77e83c840410eb3f847c7 fixed many of the methods, but also missed many of them. Before the methods were wrong, but in agreement. Now some methods are just a bit more obviously broken.

https://github.com/netty/netty/blob/23405e2000427e9cad104913e7071d8d08e91a3c/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java#L247-L250

And getMediumLE, getIntLE, and getLongLE look busted. And then I see some other busted methods like:

https://github.com/netty/netty/blob/23405e2000427e9cad104913e7071d8d08e91a3c/buffer/src/main/java/io/netty/buffer/SwappedByteBuf.java#L402-L406

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Why is the first character in the CharBuffer returned by ...
I encountered the following behavior while using a ByteBuffer. It looks like a bug to me, but perhaps I'm using the libraries incorrectly....
Read more >
JDK-5043362 (bf) NewDirectByteBuffer always has order ...
nativeOrder(). Obvious, no? ACTUAL - ByteBuffer returned by NewDirectByteBuffer has order ByteOrder.BIG_ENDIAN, which is false, mis-leading, incorrect, etc.
Read more >
Pipeline handler ordering and other questions - SwiftNIO
First and foremost, I can't seem to get a handle on the order in which pipeline handlers are called. I'm currently setting up...
Read more >
ByteBuf (Netty API Reference (4.1.85.Final))
To determine if a buffer is backed by a byte array, hasArray() should be used. NIO Buffers. If a ByteBuf can be converted...
Read more >
News — JRuby.org
In order to match Ruby behavior and permit interrupting these ... ByteBuffer.clear(); #5418 - Date.today does not appear to take the local TZ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found