]> git.ipfire.org Git - thirdparty/git.git/commitdiff
chainlint.pl: make CPU count computation more robust
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 20 May 2024 19:01:29 +0000 (15:01 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 20 May 2024 19:36:41 +0000 (12:36 -0700)
There have been reports[1,2] of chainlint.pl failing to produce output
when output is expected. In fact, the underlying problem is more severe:
in these cases, it isn't doing any work at all, thus not checking Git
tests for semantic problems. In the reported cases, the problem was
tracked down to ncores() returning 0 for the CPU count, which resulted
in chainlint.pl not performing any work (since it thought it had no
cores on which to process).

In the reported cases, the reason for the failure was that the regular
expression counting the number of processors reported by /proc/cpuinfo
failed to find any matches, hence it counted 0 processors. Although
fixing each case as it is reported allows chaining.pl to work correctly
on that architecture, it does nothing to improve the overall robustness
of the core count computation which may still return 0 on some yet
untested architecture.

Address this shortcoming by ensuring that ncores() returns a sensible
fallback value in all cases.

[1]: https://lore.kernel.org/git/pull.1385.git.git.1669148861635.gitgitgadget@gmail.com/
[2]: https://lore.kernel.org/git/8baa12f8d044265f1ddeabd64209e7ac0d3700ae.camel@physik.fu-berlin.de/

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/chainlint.pl

index e966412999ac9e4d7279505275ed1480e04f5b2a..28e2506032af8f2b0247f4f5e35ade6e041c643c 100755 (executable)
@@ -707,11 +707,22 @@ sub fd_colors {
 
 sub ncores {
        # Windows
-       return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS});
+       if (exists($ENV{NUMBER_OF_PROCESSORS})) {
+               my $ncpu = $ENV{NUMBER_OF_PROCESSORS};
+               return $ncpu > 0 ? $ncpu : 1;
+       }
        # Linux / MSYS2 / Cygwin / WSL
-       do { local @ARGV='/proc/cpuinfo'; return scalar(grep(/^processor[\s\d]*:/, <>)); } if -r '/proc/cpuinfo';
+       if (open my $fh, '<', '/proc/cpuinfo') {
+               my $cpuinfo = do { local $/; <$fh> };
+               close($fh);
+               my @matches = ($cpuinfo =~ /^processor[\s\d]*:/mg);
+               return @matches ? scalar(@matches) : 1;
+       }
        # macOS & BSD
-       return qx/sysctl -n hw.ncpu/ if $^O =~ /(?:^darwin$|bsd)/;
+       if ($^O =~ /(?:^darwin$|bsd)/) {
+               my $ncpu = qx/sysctl -n hw.ncpu/;
+               return $ncpu > 0 ? $ncpu : 1;
+       }
        return 1;
 }