Skip to content

ResponseTooLargeError#literal_size is false (not nil) when a line exceeds max_response_size #789

Open

Description

@znz

When a response exceeds max_response_size because of a long line (not a literal), Net::IMAP::ResponseTooLargeError#literal_size is false instead of nil since v0.6.4. Before v0.6.4 (and on the v0.5 / v0.4 branches) it is nil.

Reproduction

require "net/imap"
require "socket"

puts RUBY_DESCRIPTION
puts "net-imap #{Net::IMAP::VERSION}"

def try(label, payload)
  server = TCPServer.new("127.0.0.1", 0)
  th = Thread.new do
    sock = server.accept
    sock.write "* OK ready\r\n"
    sock.write payload
    sleep 1
    sock.close
  end
  imap = Net::IMAP.new("127.0.0.1", port: server.addr[1], max_response_size: 100)
  imap.noop
rescue Net::IMAP::ResponseTooLargeError => e
  printf "%-8s bytes_read=%p literal_size=%p max_response_size=%p\n",
         label, e.bytes_read, e.literal_size, e.max_response_size
ensure
  imap&.disconnect rescue nil
  th&.join
  server&.close
end

try "line",    "* " + "a" * 300 + "\r\n"
try "literal", "* 1 FETCH (BODY[] {200}\r\n" + "a" * 200 + ")\r\n"

Output

ruby 4.1.0dev (2026-11-09T07:44:48Z HEAD 4f0a0a0eca) +PRISM [x86_64-linux]
net-imap 0.6.8
line     bytes_read=100 literal_size=false max_response_size=100
literal  bytes_read=25 literal_size=200 max_response_size=100
ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [x86_64-linux]
net-imap 0.6.4.1
line     bytes_read=100 literal_size=false max_response_size=100
literal  bytes_read=25 literal_size=200 max_response_size=100
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [x86_64-linux]
net-imap 0.6.2
line     bytes_read=100 literal_size=nil max_response_size=100
literal  bytes_read=25 literal_size=200 max_response_size=100
ruby 3.4.12 (2026-10-23 revision 366f5fdc00) +PRISM [x86_64-linux]
net-imap 0.5.15
line     bytes_read=100 literal_size=nil max_response_size=100
literal  bytes_read=25 literal_size=200 max_response_size=100

Expected

literal_size is nil for the "line" case, as in v0.6.2 and v0.5.15 (and as the literal_size: nil default of ResponseTooLargeError#initialize suggests).

Cause

ResponseReader#get_literal_size returns false when the line does not end with a literal prefix:

https://github.com/ruby/net-imap/blob/v0.6.8/lib/net/imap/response_reader.rb#L50-L52

Since 6f82e28 ("Faster ResponseReader: parse literal from line", #642), read_line stores that value in @literal_size before guard_response_too_large! runs, so the false is passed to ResponseTooLargeError.new. On the v0.5 / v0.4 branches the same get_literal_size was backported, but there the error is raised before @literal_size is assigned, so they are not affected.

The error message is unaffected (response_size_msg treats false as falsy); only the attribute value differs.

Possible fix

       def get_literal_size(buff)
-        buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n) &&
-          coerce_number64($1)
+        return unless buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n)
+        coerce_number64($1)
       rescue DataFormatError

I can open a PR with this change and a test in test/net/imap/test_response_reader.rb if that helps.

🤖 Generated with Claude Code

Activity

  1. ihamzatarar commented on Nov 17, 2026

    @ihamzatarar

    I have opened a PR, using the guard you suggested.

    Checked what else reads the value, in case nil instead of false mattered anywhere: done? is line_done? && !literal_size and min_response_remaining uses literal_size || 0, so both treat false and nil the same, and response_size_msg already treats false as falsy. Only the attribute differs, which is what the literal_size: nil default on ResponseTooLargeError#initialize implies anyway.

    The test covers both halves, since the fix should not stop a real literal from being reported: an oversized line now gives literal_size nil, and an oversized literal still gives 200. It fails on master with <false> was expected to be nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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