On Wed, 4 Oct 2017, Junio C Hamano wrote:
> Rats indeed.  Let's go incremental as promised, perhaps like this
> (but please supply a better description if you have one).

I think you'll also want the following squashed into 5c8cdcfd8 and
def437671:

-- >8 --
>From 445d45027bb5b7823338cf111910d2884af6318b Mon Sep 17 00:00:00 2001
From: Alex Vandiver <ale...@dropbox.com>
Date: Tue, 3 Oct 2017 23:27:46 -0700
Subject: [PATCH] fsmonitor: Read entirety of watchman output

In perl, setting $/ sets the string that is used as the "record
separator," which sets the boundary that the `<>` construct reads to.
Setting `local $/ = 0666;` evaluates the octal, getting 438, and
stringifies it.  Thus, the later read from `<CHLD_OUT>` stops as soon
as it encounters the string "438" in the watchman output, yielding
invalid JSON; repositories containing filenames with SHA1 hashes are
able to trip this easily.

Set `$/` to undefined, thus slurping all output from watchman.  Also
close STDIN which is provided to watchman, to better guarantee that we
cannot deadlock with watchman while both attempting to read.

Signed-off-by: Alex Vandiver <ale...@dropbox.com>
---
 t/t7519/fsmonitor-watchman                 | 6 ++----
 templates/hooks--fsmonitor-watchman.sample | 6 ++----
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/t/t7519/fsmonitor-watchman b/t/t7519/fsmonitor-watchman
index 7ceb32dc1..7d6aef635 100755
--- a/t/t7519/fsmonitor-watchman
+++ b/t/t7519/fsmonitor-watchman
@@ -50,9 +50,6 @@ launch_watchman();
 
 sub launch_watchman {
 
-       # Set input record separator
-       local $/ = 0666;
-
        my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j')
            or die "open2() failed: $!\n" .
            "Falling back to scanning...\n";
@@ -83,7 +80,8 @@ sub launch_watchman {
        close $fh;
 
        print CHLD_IN $query;
-       my $response = <CHLD_OUT>;
+       close CHLD_IN;
+       my $response = do {local $/; <CHLD_OUT>};
 
        open ($fh, ">", ".git/watchman-response.json");
        print $fh $response;
diff --git a/templates/hooks--fsmonitor-watchman.sample 
b/templates/hooks--fsmonitor-watchman.sample
index 870a59d23..1b8ed173e 100755
--- a/templates/hooks--fsmonitor-watchman.sample
+++ b/templates/hooks--fsmonitor-watchman.sample
@@ -49,9 +49,6 @@ launch_watchman();
 
 sub launch_watchman {
 
-       # Set input record separator
-       local $/ = 0666;
-
        my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j')
            or die "open2() failed: $!\n" .
            "Falling back to scanning...\n";
@@ -78,7 +75,8 @@ sub launch_watchman {
        END
 
        print CHLD_IN $query;
-       my $response = <CHLD_OUT>;
+       close CHLD_IN;
+       my $response = do {local $/; <CHLD_OUT>};
 
        die "Watchman: command returned no output.\n" .
            "Falling back to scanning...\n" if $response eq "";
-- 
2.14.2.959.g6663358d3

Reply via email to