From 52272e856732390175b906ddf132015eb1c1d133 Mon Sep 17 00:00:00 2001 From: jsxs Date: Thu, 5 Jun 2025 15:36:49 +0900 Subject: [PATCH 1/3] fix: handle UTF-8 history with empty LANG env --- lib/debug/console.rb | 46 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/debug/console.rb b/lib/debug/console.rb index b228086d9..dadc53363 100644 --- a/lib/debug/console.rb +++ b/lib/debug/console.rb @@ -167,7 +167,22 @@ def history_file def read_history_file if history && File.exist?(path = history_file) f = (['', 'DAI-', 'CHU-', 'SHO-'].map{|e| e+'KICHI'}+['KYO']).sample - ["#{FH}#{f}".dup] + File.readlines(path) + begin + # Force UTF-8 encoding to handle history files with Unicode characters + lines = File.readlines(path, encoding: 'UTF-8') + ["#{FH}#{f}".dup] + lines + rescue ArgumentError, Encoding::UndefinedConversionError + # If UTF-8 reading fails, try with binary mode and force UTF-8 + begin + lines = File.readlines(path, mode: 'rb').map do |line| + line.force_encoding('UTF-8').scrub('?') + end + ["#{FH}#{f}".dup] + lines + rescue + # If all encoding attempts fail, return empty history to avoid crash + ["#{FH}#{f}".dup] + end + end else [] end @@ -191,10 +206,20 @@ def deactivate if !added_records.empty? && !path.empty? orig_records = read_history_file - open(history_file, 'w'){|f| + open(history_file, 'w', encoding: 'UTF-8'){|f| (orig_records + added_records).last(max).each{|line| - if !line.start_with?(FH) && !line.strip.empty? - f.puts line.strip + begin + # Ensure proper encoding before calling strip + if line.encoding != Encoding::UTF_8 + line = line.encode('UTF-8', invalid: :replace, undef: :replace) + end + stripped_line = line.strip + if !line.start_with?(FH) && !stripped_line.empty? + f.puts stripped_line + end + rescue Encoding::CompatibilityError, ArgumentError + # Skip lines that cannot be properly encoded to avoid crashes + next end } } @@ -204,8 +229,17 @@ def deactivate def load_history read_history_file.each{|line| - line.strip! - history << line unless line.empty? + begin + # Ensure proper encoding before calling strip! + if line.encoding != Encoding::UTF_8 + line = line.encode('UTF-8', invalid: :replace, undef: :replace) + end + line.strip! + history << line unless line.empty? + rescue Encoding::CompatibilityError, ArgumentError + # Skip lines that cannot be properly encoded to avoid crashes + next + end } if history.empty? history.count end From 4d09cc1515cddcd3c49cebffb4b079c49dea29c1 Mon Sep 17 00:00:00 2001 From: jsxs Date: Thu, 5 Jun 2025 15:53:26 +0900 Subject: [PATCH 2/3] fix: use String#scrub to handle encoding gracefully --- lib/debug/console.rb | 51 ++++++++++---------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) diff --git a/lib/debug/console.rb b/lib/debug/console.rb index dadc53363..20eb7818e 100644 --- a/lib/debug/console.rb +++ b/lib/debug/console.rb @@ -167,22 +167,9 @@ def history_file def read_history_file if history && File.exist?(path = history_file) f = (['', 'DAI-', 'CHU-', 'SHO-'].map{|e| e+'KICHI'}+['KYO']).sample - begin - # Force UTF-8 encoding to handle history files with Unicode characters - lines = File.readlines(path, encoding: 'UTF-8') - ["#{FH}#{f}".dup] + lines - rescue ArgumentError, Encoding::UndefinedConversionError - # If UTF-8 reading fails, try with binary mode and force UTF-8 - begin - lines = File.readlines(path, mode: 'rb').map do |line| - line.force_encoding('UTF-8').scrub('?') - end - ["#{FH}#{f}".dup] + lines - rescue - # If all encoding attempts fail, return empty history to avoid crash - ["#{FH}#{f}".dup] - end - end + # Read history file and scrub invalid characters to prevent encoding errors + lines = File.readlines(path).map(&:scrub) + ["#{FH}#{f}".dup] + lines else [] end @@ -206,20 +193,12 @@ def deactivate if !added_records.empty? && !path.empty? orig_records = read_history_file - open(history_file, 'w', encoding: 'UTF-8'){|f| + open(history_file, 'w'){|f| (orig_records + added_records).last(max).each{|line| - begin - # Ensure proper encoding before calling strip - if line.encoding != Encoding::UTF_8 - line = line.encode('UTF-8', invalid: :replace, undef: :replace) - end - stripped_line = line.strip - if !line.start_with?(FH) && !stripped_line.empty? - f.puts stripped_line - end - rescue Encoding::CompatibilityError, ArgumentError - # Skip lines that cannot be properly encoded to avoid crashes - next + # Use scrub to handle encoding issues gracefully + scrubbed_line = line.scrub.strip + if !line.start_with?(FH) && !scrubbed_line.empty? + f.puts scrubbed_line end } } @@ -229,17 +208,9 @@ def deactivate def load_history read_history_file.each{|line| - begin - # Ensure proper encoding before calling strip! - if line.encoding != Encoding::UTF_8 - line = line.encode('UTF-8', invalid: :replace, undef: :replace) - end - line.strip! - history << line unless line.empty? - rescue Encoding::CompatibilityError, ArgumentError - # Skip lines that cannot be properly encoded to avoid crashes - next - end + # Use scrub to handle encoding issues gracefully, then strip + line.scrub.strip! + history << line unless line.empty? } if history.empty? history.count end From 209f4927119c4ae93e3ca683af540c44c34dfe41 Mon Sep 17 00:00:00 2001 From: jsxs Date: Thu, 5 Jun 2025 16:15:03 +0900 Subject: [PATCH 3/3] Fix load_history method --- lib/debug/console.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/debug/console.rb b/lib/debug/console.rb index 20eb7818e..c49324c13 100644 --- a/lib/debug/console.rb +++ b/lib/debug/console.rb @@ -209,7 +209,8 @@ def deactivate def load_history read_history_file.each{|line| # Use scrub to handle encoding issues gracefully, then strip - line.scrub.strip! + line.scrub! + line.strip! history << line unless line.empty? } if history.empty? history.count