[pve-devel] applied: [PATCH 1/2] run command: avoid using 1 as special value

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Feb 26 15:10:19 CET 2024


In Perl, the last expression of a block (e.g. of a method, eval) gets
returned if there's no explicit return statement. Quite often that is
truthy, i.e., 1.

As that was chosen as the special value for the CMD_FINISHED flag it
had quite a few false positives, causing weird effects and
installation failure.

Reserve that overly problematic value and chose 2 as new CMD_FINISHED
value, albeit it could be better to signal this even more explicitly,
like with a structured hash reference, but for now this is a good stop
gap.

Fixes: 23c5fbe ("sys: command: allow terminating the process early from log subroutine")
Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 Proxmox/Sys/Command.pm | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/Proxmox/Sys/Command.pm b/Proxmox/Sys/Command.pm
index cb5fe76..ac505c4 100644
--- a/Proxmox/Sys/Command.pm
+++ b/Proxmox/Sys/Command.pm
@@ -17,7 +17,10 @@ use Proxmox::UI;
 use base qw(Exporter);
 our @EXPORT_OK = qw(run_command syscmd CMD_FINISHED);
 
-use constant CMD_FINISHED => 1;
+use constant {
+    CMD_RESERVED => 1<<0, # reserve 1 as it's often the default return value of closures
+    CMD_FINISHED => 1<<1,
+};
 
 my sub shellquote {
     my $str = shift;
@@ -78,8 +81,9 @@ sub syscmd {
 #
 # Arguments:
 # * $cmd - The command to run, either a single string or array with individual arguments
-# * $func - Logging subroutine to call, receives both stdout and stderr. Might return CMD_FINISHED
-#           to exit early and ignore the rest of the process output.
+# * $func - Logging subroutine to call, receives both stdout and stderr.
+#           Can return CMD_FINISHED to exit early and ignore the rest of the process output.
+#           Should use an explicit  `return;` to avoid misinterpretation of return value.
 # * $input - Stdin contents for the spawned subprocess
 # * $noout - Whether to append any process output to the return value
 # * $noprint - Whether to print any process output to the parents stdout
-- 
2.39.2





More information about the pve-devel mailing list