git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Nov 28, 2021, 17:49 UTC
Message-ID
<20211128174903.5623-1-carenas@gmail.com>

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(-)
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
Next: Junio C Hamano
Message 1 of 4 in “add -p: avoid use of undefined $key when ReadKey -> EOF”
  1. add -p: avoid use of undefined $key when ReadKey -> EOFCarlo Marcelo Arenas Belón, Nov 28, 2021
  2. Junio C HamanoNov 28, 2021
  3. Carlo ArenasNov 28, 2021
  4. Junio C HamanoNov 28, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.