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

Aperta Adatta ai principianti
#789 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
ruby

Direzione di ricerca

Inizia con get_literal_size in lib/net/imap/response_reader.rb e con i test del lettore di risposte in test/net/imap/test_response_reader.rb, entrambi indicati nell’issue. Riproduci il caso della riga lunga e verifica come il valore restituito arriva a ResponseTooLargeError#literal_size. Il lavoro è completato quando il caso della riga restituisce nil, mentre le risposte literal continuano a indicare la propria dimensione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Ruby
Stelle
121
Fork
47
Merge medio
19h 25m
PR unite (30g)
3

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ruby/net-imap

Tutte le issue di ruby/net-imap

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.