Skip to content

Add checked SLEB128 encoding and decoding for byte cursors #718

Closed

Description

@LunaStev

Motivation

The completed #674 added unsigned LEB128 to std/bytes/leb128.wave. Binary formats using signed LEB128 still require callers to implement sign extension and overflow checks themselves.

Scope

Add bytes_reader_read_sleb128_i64 and bytes_writer_write_sleb128_i64 using the existing cursor and error types. Read at most ten bytes, accepting terminating non-minimal encodings only when they represent an i64. Writers emit the shortest encoding.

Return BYTES_ERROR_INVALID for null pointers or invalid cursor state, BYTES_ERROR_EOF for input ending before termination within the ten-byte bound, BYTES_ERROR_OVERFLOW for an unrepresentable value or a continuation beyond that bound, and BYTES_ERROR_NO_SPACE for insufficient output capacity.

Reader failure must preserve position and output; writer failure must preserve position and bytes. Check tenth-byte sign extension before unsafe shifts. Keep ULEB128 behavior unchanged. This is SLEB128, not ZigZag encoding.

Completion criteria

  • Implement both operations without heap allocation, libc, or a new dependency.
  • Verify exact encodings for 0, -1, 63, 64, -64, -65, and both i64 extrema.
  • Cover truncation, unterminated/overflowing encodings, and invalid tenth-byte sign extension.
  • Cover invalid/null cursors and output pointers, valid non-minimal encodings, consecutive values from a nonzero position, and insufficient output capacity.
  • Verify error codes and state preservation on every failure path.
  • Run new and existing ULEB128 regressions at O0/O2.

Difficulty: medium.

Activity

  1. added
    enhancement
    help wanted
    needs testing
    on Sep 24, 2026
  2. changed the title [-][Proposal]: Add checked signed LEB128 byte-cursor operations[/-] [+]Add checked SLEB128 encoding and decoding for byte cursors[/+] on Sep 24, 2026
  3. self-assigned this
    on Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementhelp wantedneeds testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions