Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/net/imap/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ def response
CRLF!
EOF!
resp
rescue SystemStackError => error
raise exception(error.message), cause: error
Comment on lines +694 to +695
Copy link
Copy Markdown
Collaborator

@nevans nevans Mar 26, 2026

Choose a reason for hiding this comment

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

Note that cause is automatically set to $! (which is set to the current exception being handled). So, generally we don't assign it explicitly unless we want a different exception (e.g: raising from a different thread).

So I think this should be fine:

Suggested change
rescue SystemStackError => error
raise exception(error.message), cause: error
rescue SystemStackError => error
raise exception(error.message)

Alternatively, this should also capture the #cause, and I think I'd prefer to use our own custom error message:

Suggested change
rescue SystemStackError => error
raise exception(error.message), cause: error
rescue SystemStackError
parse_error("response recursion too deep")

end

# RFC3501 & RFC9051:
Expand Down
20 changes: 20 additions & 0 deletions test/net/imap/test_response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ def test_fetch_binary_and_binary_size
end
end

test "deeply nested BODYSTRUCTURE raises ResponseParseError instead of SystemStackError" do
parser = Net::IMAP::ResponseParser.new

def parser.body
raise SystemStackError, "stack level too deep"
end unless ENV["TEST_RESPONSE_PARSER_STACK_LEVEL"] == "original"

depth = 10_000
leaf = '("TEXT" "PLAIN" NIL NIL NIL "7BIT" 1 1)'
bodystructure = depth.times.reduce(leaf) { |part, _| "(#{part} \"MIXED\")" }
response = "* 1 FETCH (BODYSTRUCTURE #{bodystructure})\r\n"

err = assert_raise(Net::IMAP::ResponseParseError) do
parser.parse(response)
end

assert_match(/stack level too deep/i, err.message)
assert_instance_of(SystemStackError, err.cause)
end

def assert_deprecated_appenduid_data_warning
assert_warn(/#{__FILE__}.*warning.*parser_use_deprecated_uidplus_data is ignored/) do
result = yield
Expand Down