ruby/rubygems · error · Gem::SafeMarshal::Reader::LengthTooLongError

expected #{count} elements, but only #{remaining} bytes rema

Error message

expected #{count} elements, but only #{remaining} bytes remain

What it means

Error "expected #{count} elements, but only #{remaining} bytes remain" thrown in ruby/rubygems.

Source

Thrown at lib/rubygems/safe_marshal/reader.rb:107

        else
          signed = (b ^ 128) - 128
          if b >= 128
            signed + 5
          else
            signed - 5
          end
        end
      end

      # Reads an element count and validates it against the number of bytes
      # remaining in the input, since each element to be read consumes at
      # least one byte. This prevents allocating huge backing stores for
      # maliciously crafted lengths that could never be satisfied.
      def read_count
        count = read_integer
        raise NegativeLengthError if count < 0
        remaining = @io.size - @io.pos
        raise LengthTooLongError, "expected #{count} elements, but only #{remaining} bytes remain" if count > remaining
        count
      end

      def read_element
        type = read_byte
        case type
        when 34 then read_string # ?"
        when 48 then read_nil # ?0
        when 58 then read_symbol # ?:
        when 59 then read_symbol_link # ?;
        when 64 then read_object_link # ?@
        when 70 then read_false # ?F
        when 73 then read_object_with_ivars # ?I
        when 84 then read_true # ?T
        when 85 then read_user_marshal # ?U
        when 91 then read_array # ?[
        when 102 then read_float # ?f
        when 105 then Elements::Integer.new(read_integer) # ?i

View on GitHub (pinned to 86cbb817a3)

Solutions

  1. The marshal data is truncated mid-structure; re-download the gem or metadata file

When it happens

Trigger: Thrown at lib/rubygems/safe_marshal/reader.rb:107 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ruby/rubygems@86cbb817a3 (2026-08-23). Data as JSON: /api/errors/2dbc746bbd1c7b8f. Report an issue: GitHub.