Scrub past history input before split #795
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.But it does not work correctly between encodings where
"\n"
is represented by different byte sequences like below.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.