Skip to content

Buf::get_int() implementation for Bytes returns positive number instead of negative when nbytes < 8. #730

Closed
@petriek

Description

@petriek

Steps to reproduce:

Run the following program, using bytes version 1.7.1

use bytes::{BytesMut, Buf, BufMut};

fn main() {
    const SOME_NEG_NUMBER: i64 = -42;
    let mut buffer = BytesMut::with_capacity(8);

    buffer.put_int(SOME_NEG_NUMBER, 1);
    println!("buffer = {:?}", &buffer);
    assert_eq!(buffer.freeze().get_int(1), SOME_NEG_NUMBER);
}

Expected outcome:
Assertion passes and program terminates successfully, due to the symmetry of the put_int and get_int calls.

Actual outcome:
Assertion fails:

buffer = b"\xd6"
thread 'main' panicked at src/main.rs:9:5:
assertion `left == right` failed
  left: 214
 right: -42
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Additional information:

  1. Only happens with negative numbers; positive numbers are always OK.
  2. Only happens when nbytes parameter is < 8.
  3. Other combos like put_i8()/get_i8() or put_i16()/get_i16() work as intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions