From: "karinamendez132@... (Karina Mendez) via ruby-core" Date: 2025-12-17T01:19:42+00:00 Subject: [ruby-core:124267] [Ruby Bug#21786] Net::HTTP::Response#read_body crashes on nil body when response_body_encoding is a String Issue #21786 has been reported by karinamendez132@gmail.com (Karina Mendez). ---------------------------------------- Bug #21786: Net::HTTP::Response#read_body crashes on nil body when response_body_encoding is a String https://bugs.ruby-lang.org/issues/21786 * Author: karinamendez132@gmail.com (Karina Mendez) * Status: Open * ruby -v: ruby 3.2.0p0 (2022-12-25 revision a528908271) [x86_64-linux] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- When response_body_encoding is set to a String value (e.g., 'UTF-8'), and the HTTP response body is nil due to network failures, read_body attempts to call force_encoding on nil at line 360, raising NoMethodError. The issue occurs because String values fall through to the else branch which calls detect_encoding(@body) without checking if @body is nil. Line 360 should verify @body is not nil before calling force_encoding. Category: lib Target version:current Reproducible script: require 'net/http' # Monkey patch to simulate nil body scenario class Net::HTTP::Response alias_method :original_reading_body, :reading_body def reading_body(sock, reqmethodallowbody) @socket = sock @body_exist = false # Force nil body begin yield self.body # Triggers the bug ensure @socket = nil end end end # Trigger the bug http = Net::HTTP.new('example.com', 443) http.use_ssl = true http.response_body_encoding = 'UTF-8' # String triggers else branch begin http.request(Net::HTTP::Get.new('/')) rescue NoMethodError => e puts "BUG: #{e.message}" puts "Location: #{e.backtrace.first}" end # Expected: Graceful handling # Actual: NoMethodError: undefined method `force_encoding' for nil:NilClass # from lib/net/http/response.rb:360:in `read_body' Suggested fix: In lib/net/http/response.rb line 360, change: @body.force_encoding(enc) if enc to: @body.force_encoding(enc) if enc && @body -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/