From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <c.heiss@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 9FC07B783
 for <pve-devel@lists.proxmox.com>; Wed,  9 Aug 2023 15:45:43 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 81D7D15D60
 for <pve-devel@lists.proxmox.com>; Wed,  9 Aug 2023 15:45:13 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Wed,  9 Aug 2023 15:45:12 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 0F47543C85
 for <pve-devel@lists.proxmox.com>; Wed,  9 Aug 2023 15:45:12 +0200 (CEST)
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed,  9 Aug 2023 15:44:24 +0200
Message-ID: <20230809134426.1009504-6-c.heiss@proxmox.com>
X-Mailer: git-send-email 2.41.0
In-Reply-To: <20230809134426.1009504-1-c.heiss@proxmox.com>
References: <20230809134426.1009504-1-c.heiss@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.045 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH installer 5/6] sys: block: fix possible use of
 `undef`-value when detecting disk sizes
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 09 Aug 2023 13:45:43 -0000

`$size` and `$logical_bsize` might get unset if there invalid, but then
are unconditionally converted to an int - which throws an error.
This was reported on the forum by a user [0].

Fix it by changing the check a bit to skip the disk immediately if
detecting either of those two values are invalid or simply not present.
The fix for `$logical_bsize` simply avoids trying to convert to value to
an int if invalid.

tl;dr: Should have no impact at all, in the end. The same
`run-env-info.json` is generated w/ and w/o the patch if all disks are
fine. I then also hacked up the script a bit to actually have an invalid
size, this resulted in the "affected" disk simply missing from the disk
array - the expected behavior in this case.

[0] https://forum.proxmox.com/threads/error-installing-proxmox-8.131921/

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
 Proxmox/Sys/Block.pm | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/Proxmox/Sys/Block.pm b/Proxmox/Sys/Block.pm
index 26085e3..f76e0f1 100644
--- a/Proxmox/Sys/Block.pm
+++ b/Proxmox/Sys/Block.pm
@@ -90,10 +90,10 @@ my sub hd_list {
 	}
 
 	my $size = file_read_firstline("$bd/size");
+	next if !$size;
 	chomp $size;
-	$size = undef if !($size && $size =~ m/^\d+$/);
+	next if $size !~ m/^\d+$/;
 	$size = int($size);
-	next if !$size;
 
 	my $model = file_read_firstline("$bd/device/model") || '';
 	$model =~ s/^\s+//;
@@ -104,8 +104,11 @@ my sub hd_list {
 
 	my $logical_bsize = file_read_firstline("$bd/queue/logical_block_size") // '';
 	chomp $logical_bsize;
-	$logical_bsize = undef if !($logical_bsize && $logical_bsize =~ m/^\d+$/);
-	$logical_bsize = int($logical_bsize);
+	if ($logical_bsize && $logical_bsize !~ m/^\d+$/) {
+	    $logical_bsize = int($logical_bsize);
+	} else {
+	    $logical_bsize = undef;
+	}
 
 	push @$res, [$count++, $dev_path, $size, $model, $logical_bsize, "/sys/block/$name"];
     }
-- 
2.41.0