Skip to content

Return nil, not false, when a line carries no literal - #790

Open
ihamzatarar wants to merge 1 commit into
ruby:masterfrom
ihamzatarar:literal-size-nil-for-long-lines
Open

ihamzatarar wants to merge 1 commit into
ruby:masterfrom
ihamzatarar:literal-size-nil-for-long-lines

Conversation

@ihamzatarar

@ihamzatarar ihamzatarar commented Nov 17, 2026 •

Copy link
Copy Markdown

Fixes #789.

get_literal_size ends in a && chain, so it returns false rather than nil when the line does not end with a literal prefix:

buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n) &&
  coerce_number64($1)

read_line assigns that to @literal_size before guard_response_too_large! runs, so when a long line exceeds max_response_size, ResponseTooLargeError#literal_size is false instead of nil. The v0.5 and v0.4 branches are not affected, because there the error is raised before the assignment.

Nothing else changes: done? and min_response_remaining both treat false and nil alike, and response_size_msg already treats false as falsy, so only the attribute value differs. That matches the literal_size: nil default on ResponseTooLargeError#initialize.

The guard is the one @znz suggested on the issue.

Tests

One test in test/net/imap/test_response_reader.rb, covering both halves: an oversized line reports no literal size, and an oversized literal still reports 200. It fails on master with <false> was expected to be nil.

Suite on Ruby 3.4.6, run standalone:

1747 tests, 16957 assertions, 0 failures, 0 errors

test/net/imap/test_stringprep_tables.rb is excluded from that run because it needs rfcs/rfc3454-stringprep_tables.json, which is generated by a rake task rather than checked in. It does not touch this code.

Assisted-by: Claude Code

@nevans nevans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix looks good, with just a few nitpicks around the comments and test.

Also, this PR smells like it was substantially generated. If so, please add something about that to the commit message, such as an Assisted-by: trailer.

Thanks!

Comment thread lib/net/imap/response_reader.rb Outdated
def get_literal_size(buff)
buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n) &&
coerce_number64($1)
# Returning nil rather than false keeps ResponseTooLargeError#literal_size

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is superfluous, IMO. No justification is needed here.

Comment thread test/net/imap/test_response_reader.rb Outdated
client = FakeClient.new
client.config.max_response_size = 10

# The line is what exceeds the limit, so there is no literal size to report.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment isn't so bad. But it feels slightly redundant, with the test name just a few lines above it. And, IMO, the variable name and assertions below already make it fairly obvious too.

If you think the comment is worth keeping, maybe split this into two tests instead?

Comment thread test/net/imap/test_response_reader.rb Outdated
end
end

test "#read_response_buffer literal_size is only set by an oversized literal" do

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. literal_size doesn't belong to ResponseReader.
  2. setting it to nil is (arguably) still setting it.

So this would be more clear and accurate, IMO:

Suggested change
test "#read_response_buffer literal_size is only set by an oversized literal" do
test "#read_response_buffer sets ResponseTooLargeError#literal_size" do

Splitting it into two tests would also be fine, but I don't think that's necessary.

get_literal_size ends in a && chain, so it returned false rather than
nil when the line does not end with a literal prefix. read_line stores
that in @literal_size before guard_response_too_large! runs, so a line
that exceeded max_response_size produced a ResponseTooLargeError whose
literal_size was false instead of nil.

Nothing else changes: done? and min_response_remaining treat false and
nil alike, and response_size_msg already treats false as falsy, so only
the attribute value differs.

Assisted-by: Claude Code <noreply@anthropic.com>
@ihamzatarar
ihamzatarar force-pushed the literal-size-nil-for-long-lines branch from 56d6b1e to 81d5af9 Compare November 19, 2026 20:25
@ihamzatarar

Copy link
Copy Markdown
Author

Thanks, all applied. I kept it as one test rather than splitting it, since both halves share the max_response_size setup.

And yes, it was. I have added an Assisted-by: trailer.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants