Skip to content

Gracefully handle (ignore) null bytes in history lines - #1790

Merged
kyrylo merged 1 commit into
pry:masterfrom
bambooengineering:handle_null_bytes_in_history
Oct 12, 2018
Merged

kyrylo merged 1 commit into
pry:masterfrom
bambooengineering:handle_null_bytes_in_history

Conversation

@owst

@owst owst commented Oct 9, 2018

Copy link
Copy Markdown
Contributor

Fixes #1789.

Readline is unable to add lines to its history that contain a null byte;
we should therefore avoid saving such lines to the history file, and
ignore any such lines that are already present in the file.

Fixes #1789.

Readline is unable to add lines to its history that contain a null byte;
we should therefore avoid saving such lines to the history file, and
ignore any such lines that are already present in the file.
Comment thread lib/pry/history.rb
# @return [Integer] The number of lines loaded
def load
@loader.call do |line|
next if invalid_readline_line?(line)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we should substitute \0 with a blank instead? Like you said in #1789 you typed that by accident, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When you say "substitute \0 with a blank" do you mean "we could delete \0s" or do you mean "we could replace each \0 with a (a space)"? I think you might mean the latter, since in #1789 I said I typed ctrl-space instead of space to generate \0.

I see some issues:

  1. In general a null byte bears no relation to a space - ctrl-space is one keypress sequence to type a literal NULL byte, for me on Linux, the sequence ctrl-v, ctrl-shift-' also enters a NULL byte, and there's likely other ways too
  2. There's no guarantee that the NULL byte appears in a position where adding a space is syntactically valid (e.g. x\0y = 1)
  3. The simple symmetry between "don't save invalid lines" and "don't load existing invalid lines" would be broken

What do you think? I think the behaviour as-is in this PR is simple and likely to be most-often correct vs attempting to modify invalid lines

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you might mean the latter [...]

That's correct. This is what I meant.

Thanks for the analysis, I agree with this decision. I was just wondering about the "what-if" but your points sound very convincing.

@kyrylo
kyrylo merged commit 888e5e3 into pry:master Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants