Skip to content

get_offset_reader_file_id is incorrect in ledger.rs #25440

Description

@sujeet

A note for the community

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Problem

Current impl:

    fn get_offset_reader_file_id(&self, offset: u16) -> u16 {
        self.get_current_reader_file_id().wrapping_add(offset) % MAX_FILE_ID
    }

Consider:

current = 65534, offset = 1, returns 0
65534.wrapping_add(1) % 65535
65535                 % 65535
0

current = 65534, offset = 2, still returns 0
65534.wrapping_add(2) % 65535
0                     % 65535
0

One way to fix:

    fn get_offset_reader_file_id(&self, offset: u16) -> u16 {
        let curr = u32::from(self.get_current_reader_file_id());
        let off = u32::from(offset);
        u16::try_from((curr + off) % u32::from(MAX_FILE_ID))
            .expect("(_ % MAX_FILE_ID) always fits in u16")
    }

This is also a likely root-cause of #19759

Configuration


Version

latest (v0.55.0)

Debug Output


Example Data

No response

Additional Context

No response

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    domain: buffersAnything related to Vector's memory/disk buffers

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions