diff --git a/lib/debug/console.rb b/lib/debug/console.rb index b228086d9..c49324c13 100644 --- a/lib/debug/console.rb +++ b/lib/debug/console.rb @@ -167,7 +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 - ["#{FH}#{f}".dup] + File.readlines(path) + # Read history file and scrub invalid characters to prevent encoding errors + lines = File.readlines(path).map(&:scrub) + ["#{FH}#{f}".dup] + lines else [] end @@ -193,8 +195,10 @@ def deactivate orig_records = read_history_file open(history_file, 'w'){|f| (orig_records + added_records).last(max).each{|line| - if !line.start_with?(FH) && !line.strip.empty? - f.puts line.strip + # 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 } } @@ -204,6 +208,8 @@ def deactivate def load_history read_history_file.each{|line| + # Use scrub to handle encoding issues gracefully, then strip + line.scrub! line.strip! history << line unless line.empty? } if history.empty?