Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrub past history input before split #795

Merged
merged 2 commits into from
Dec 1, 2023
Merged

Conversation

hogelog
Copy link
Contributor

@hogelog hogelog commented Dec 1, 2023

This PR fixes #474.

As described in this #474, if there is a .irb_history from a startup with a different LANG setting, irb crashes when exiting and the .irb_history history is lost.

This problem is caused by the fact that past lines loaded from .irb_history (by irb launched with different encoding settings) become invalid strings from irb during launch.
String#split does not allow invalid strings.
https://github.com/ruby/ruby/blob/59f31a66180b8b118c93cdb3dad20e467b18f7fc/string.c#L8865

So I ran scrub before the split to remove the invalid string.
The contents of the original .irb_history are not preserved, but I believe it is a better solution than losing every history.

Solution that did not

I also considered the following methods, but they are all responses that would result in mixed strings of different encodings in .irb_history.
I did not choose such a response because I thought that creating a .irb_history that simply cannot be handled in such a way could produce unexpected bugs in irb's behavior.
What do you think?

Force split

How to forcibly split the past input. This may work if the "\n" is between character codes represented by the same byte sequence.

"😀\n😀".encode("utf-8").force_encoding("binary").split("\n".encode("us-ascii").force_encoding("binary")).join("\\\n").force_encoding("utf-8")
=> "😀\\\n😀"

But it does not work correctly between encodings where "\n" is represented by different byte sequences like below.

"***".encode("utf-16be").force_encoding("binary").split("\n".force_encoding("binary")).join("\\\n")
=> "\xFF\\\n\xFF\\\n\xFF"

Always open with append mode

I thought that if I always append with append mode, I can avoid mentioning past lines.
However, I have not thought about it specifically because I think it would not be a good solution since the same problem occurs in the history truncation process after all.

How to reproduce the problem

You can reproduce the problem with the irb code in the repository by following the steps below.
Note that the contents of .irb_history will be lost if you perform this procedure.

$ LANG=en_US.UTF-8 ./bin/console 
irb(main):001> "😀"
=> "😀"
irb(main):002> 
$ LANG=en_US.UTF-8 ./bin/console 
irb(main):001> "😀"
=> "😀"
irb(main):002> exit
$ LANG=C ./bin/console 
irb(main):001> exit
/Users/hogelog/repos/oss/ruby/irb/lib/irb/history.rb:63:in `split': invalid byte sequence in US-ASCII (ArgumentError)

          hist = history.map{ |l| l.split("\n").join("\\\n") }
                                          ^^^^
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb/history.rb:63:in `block (2 levels) in save_history'
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb/history.rb:63:in `map'
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb/history.rb:63:in `block in save_history'
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb/history.rb:62:in `open'
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb/history.rb:62:in `save_history'
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb.rb:500:in `run'
        from /Users/hogelog/repos/oss/ruby/irb/lib/irb.rb:396:in `start'
        from ./bin/console:6:in `<main>'

@hogelog
Copy link
Contributor Author

hogelog commented Dec 1, 2023

The problem with different encoding history is also on reline, so once the policy here is finalized, I would like to fix the reline as well.

@hogelog hogelog force-pushed the scrub-history branch 2 times, most recently from 88412d9 to be6bb20 Compare December 1, 2023 03:51
@hogelog
Copy link
Contributor Author

hogelog commented Dec 1, 2023

Based on @ima1zumi's advice that "rewriting ENV["LANG"] may have a negative impact on ruby/ruby testing," I tried to pass locale to assert_history.
e368f3e

Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

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

Thank you for the fix as well as the detailed explanation!

@st0012 st0012 added the bug Something isn't working label Dec 1, 2023
@st0012 st0012 merged commit 0f344f6 into ruby:master Dec 1, 2023
22 of 24 checks passed
matzbot pushed a commit to ruby/ruby that referenced this pull request Dec 1, 2023
(ruby/irb#795)

* Scrub past history input before split

* Don't rewrite ENV["LANG"]

ruby/irb@0f344f66d9
@hogelog hogelog deleted the scrub-history branch December 4, 2023 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

Different encoding corrupts the history file
2 participants