threads / patch / 56992

patchadd -p: avoid use of undefined $key when ReadKey -> EOF

Subject: [PATCH] add -p: avoid use of undefined $key when ReadKey -> EOF

## tl;dr

4 messages between Nov 28, 2021 and Nov 28, 2021. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Carlo Marcelo Arenas Belón· Nov 28, 2021, 17:49 UTC · lore

b5cc003253 (add -i: ignore terminal escape sequences, 2011-05-17) add an additional check to the original code to better handle keys for escape sequences, but failed to account for the possibility the first ReadKey call returned undef (ex: stdin closes) and that was being handled fine by the original code in ca6ac7f135 (add -p: prompt for single characters, 2009-02-05)

Add a test for undefined and encapsulate the loop and the original print that relied on it within it.

After this, the following command (in a suitable repository state) wouldn't print any error:

  $ git -c interactive.singleKey add -p </dev/null
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 git-add--interactive.perl | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
Show changes to git-add--interactive.perl +9 −7
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index bc3a1e8eff..95887fd8e5 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -1175,15 +1175,17 @@ sub prompt_single_character {
 		ReadMode 'cbreak';
 		my $key = ReadKey 0;
 		ReadMode 'restore';
-		if ($use_termcap and $key eq "\e") {
-			while (!defined $term_escapes{$key}) {
-				my $next = ReadKey 0.5;
-				last if (!defined $next);
-				$key .= $next;
+		if (defined $key) {
+			if ($use_termcap and $key eq "\e") {
+				while (!defined $term_escapes{$key}) {
+					my $next = ReadKey 0.5;
+					last if (!defined $next);
+					$key .= $next;
+				}
+				$key =~ s/\e/^[/;
 			}
-			$key =~ s/\e/^[/;
+			print "$key";
 		}
-		print "$key" if defined $key;
 		print "\n";
 		return $key;
 	} else {
-- 
2.34.0.352.g07dee3c5e1
Junio C Hamano· Nov 28, 2021, 19:44 UTC · re: Carlo Marcelo Arenas Belón · lore

Re: [PATCH] add -p: avoid use of undefined $key when ReadKey -> EOF

Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:
Show 9 quoted lines
> b5cc003253 (add -i: ignore terminal escape sequences, 2011-05-17)
> add an additional check to the original code to better handle keys
> for escape sequences, but failed to account for the possibility
> the first ReadKey call returned undef (ex: stdin closes) and that
> was being handled fine by the original code in ca6ac7f135 (add -p:
> prompt for single characters, 2009-02-05)
>
> Add a test for undefined and encapsulate the loop and the original
> print that relied on it within it.

I wondered where a change to t/ directory was, but we are not talking about that kind of test ;-) OK.

Show 36 quoted lines
> After this, the following command (in a suitable repository state)
> wouldn't print any error:
>
>   $ git -c interactive.singleKey add -p </dev/null
>
> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
> ---
>  git-add--interactive.perl | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/git-add--interactive.perl b/git-add--interactive.perl
> index bc3a1e8eff..95887fd8e5 100755
> --- a/git-add--interactive.perl
> +++ b/git-add--interactive.perl
> @@ -1175,15 +1175,17 @@ sub prompt_single_character {
>  		ReadMode 'cbreak';
>  		my $key = ReadKey 0;
>  		ReadMode 'restore';
> -		if ($use_termcap and $key eq "\e") {
> -			while (!defined $term_escapes{$key}) {
> -				my $next = ReadKey 0.5;
> -				last if (!defined $next);
> -				$key .= $next;
> +		if (defined $key) {
> +			if ($use_termcap and $key eq "\e") {
> +				while (!defined $term_escapes{$key}) {
> +					my $next = ReadKey 0.5;
> +					last if (!defined $next);
> +					$key .= $next;
> +				}
> +				$key =~ s/\e/^[/;
>  			}
> -			$key =~ s/\e/^[/;
> +			print "$key";
>  		}
> -		print "$key" if defined $key;

This was from the very original and was a strong clue that $key could be undef; well spotted.

>  		print "\n";
>  		return $key;
>  	} else {

Essentially, the code added by b5cc0032 (add -i: ignore terminal escape sequences, 2011-05-17) wanted to say

    The original code
    - called ReadKey and took its output in $key, 
    - echoed it if it was defined (otherwise $key wasn't echoed),
    - emitted a line feed,
    - and returned $key.
    We deal with the case where a single keystroke of some keys is
    delivered to us as an escape sequence of multiple "key"s from
    ReadKey, and the way we do so is to replace that single ReadKey
    with the new code that accumulates these multiple "key"s in
    $key.

I am undecided, but the minimum patch below seems to makes the above intention in the updated code clearer.

Show changes to diff +1 −1
diff --git i/git-add--interactive.perl w/git-add--interactive.perl
index bc3a1e8eff..c459c675e7 100755
--- i/git-add--interactive.perl
+++ w/git-add--interactive.perl
@@ -1175,7 +1175,7 @@ sub prompt_single_character {
 		ReadMode 'cbreak';
 		my $key = ReadKey 0;
 		ReadMode 'restore';
-		if ($use_termcap and $key eq "\e") {
+		if (defined $key && $use_termcap and $key eq "\e") {
 			while (!defined $term_escapes{$key}) {
 				my $next = ReadKey 0.5;
 				last if (!defined $next);
Carlo Arenas· Nov 28, 2021, 20:52 UTC · re: Junio C Hamano · lore

Re: [PATCH] add -p: avoid use of undefined $key when ReadKey -> EOF

On Sun, Nov 28, 2021 at 11:44 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 6 quoted lines
> Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:
> > Add a test for undefined and encapsulate the loop and the original
> > print that relied on it within it.
>
> I wondered where a change to t/ directory was, but we are not
> talking about that kind of test ;-)  OK.

Will change the wording in v2 with something less ambiguous like "Check for undefined.."

Show 16 quoted lines
> I am undecided, but the minimum patch below seems to makes the above
> intention in the updated code clearer.
>
> diff --git i/git-add--interactive.perl w/git-add--interactive.perl
> index bc3a1e8eff..c459c675e7 100755
> --- i/git-add--interactive.perl
> +++ w/git-add--interactive.perl
> @@ -1175,7 +1175,7 @@ sub prompt_single_character {
>                 ReadMode 'cbreak';
>                 my $key = ReadKey 0;
>                 ReadMode 'restore';
> -               if ($use_termcap and $key eq "\e") {
> +               if (defined $key && $use_termcap and $key eq "\e") {
>                         while (!defined $term_escapes{$key}) {
>                                 my $next = ReadKey 0.5;
>                                 last if (!defined $next);

The issue I had with that was that the order of the checks is slightly awkward and inefficient (ex: $use_termcap = false probably should short circuit, and both checks for $key make more sense next to each other) and doing it outside avoids the extra !defined check for print.

It would also look cleaner IMHO if the expression would be rewritten to use '&&' instead of 'and', which smells like unnecessary refactoring that I intentionally tried to avoid by creating a precondition instead so `diff -w` is really small and straight forward.

I have no strong opinions eitherway, so let me know what you would prefer and will get a v2 with it; thanks for reviewing.

Carlo

PS. list email is for whatever reason taking a long time to arrive from vger, hopefully not many are affected, but I might be slow to respond

Junio C Hamano· Nov 28, 2021, 23:15 UTC · re: Carlo Arenas · lore

Re: [PATCH] add -p: avoid use of undefined $key when ReadKey -> EOF

Carlo Arenas <carenas@gmail.com> writes:
> I have no strong opinions eitherway, so let me know what you would
> prefer and will get a v2 with it; thanks for reviewing.

I have no strong preference either way, so I just queued what we reviewed as-is.

Thanks for the fix.

← back to recent threads