Thread 'main' panicked at 'index 252645158 out of range for slice of length 408',

#1

Flatbuffer version : Latest rust
OS : Ubuntu 14 trusty
RUST : 1.17

Hi. I have modified a rust sample code binary.rs given by a library ‘Flatbuffers’. I get this error while trying to access any value present in the schema.

./target/debug/
Encoding time: Duration { secs: 0, nanos: 56645 } // Encoding is successful
thread ‘main’ panicked at ‘index 252645158 out of range for slice of length 408’, /checkout/src/libcore/slice.rs:580
note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.
stack backtrace:

0: core::slice::slice_index_len_fail
at /checkout/src/libcore/slice.rs:580
1: <core::ops::Range as core::slice::SliceIndex>::index
at /checkout/src/libcore/slice.rs:711
2: core::slice::<impl core::ops::Index for [T]>::index
at /checkout/src/libcore/slice.rs:562
3: flatbuffers::endian_scalar::read_scalar_at
at flatbuffers/rust/flatbuffers/src/endian_scalar.rs:172
4: flatbuffers::vtable::VTable::num_bytes
at flatbuffers/rust/flatbuffers/src/vtable.rs:46
5: flatbuffers::vtable::VTable::get
at flatbuffers/rust/flatbuffers/src/vtable.rs:64
6: flatbuffers::table::Table::get
at flatbuffers/rust/flatbuffers/src/table.rs:42
7: individual_messages::initial_ue_message_generated::InitialUEMessage::procedure_code
at ./src/initial_ue_message_generated.rs:513
8: individual_messages::main
at ./src/main.rs:176

Apparently it cannot access the index. I am assuming that the buf or message that i received in encoded form is of length 408. I am sure that this has to do with the rust version as this error does not persist on the latest version.

I checked a related issue https://github.com/rust-lang/rust/issues/28230

So my question is how can i debug this?

please note that updating to the latest rust is not an option for me.

#2

The panic doesn’t look like a bug in Rust, or even a old-rust-specific issue. It’s a standard behavior you get every time you use [x] with an invalid x.

Look through the stack trace, find where your code is, and review whether you’re passing the right arguments. You can add assert!() or debug_assert!() to check things individually. For example, ensure you’re not passing negative numbers to slice indexing (or subtracting too much from unsigned numbers).

If you’re comfortable with debuggers, set a breakpoint on rust_panic and inspect the program from there.

#3

When I see a strange number like that, I always try converting to hex.

Seems that number is 0x0f0f0f0f + 0x17. Suggests to me that an array of 0000111100001111... been cast to a u32, offset by 0x17 (23), and used as an index.

1 Like