* [PATCH storage] fix #5805: plugin: file size info: handle missing fields in qemu-img output
@ 2026-12-02 14:18 Michal Fox
0 siblings, 0 replies; only message in thread
From: Michal Fox @ 2026-12-02 14:18 UTC (permalink / raw)
To: pve-devel
If qemu-img info works in general but does not report e.g. the actual
size of a partially broken image, the untaint checks run on undef and
emit a series of "use of uninitialized value" warnings before dying,
which are a red herring and do not even name the affected file.
Check the required fields first and go through the existing error
handling, so the warning (or the error for untrusted images) names the
missing field and the file. Add tests for that.
Signed-off-by: Michal Fox <me@dualfroz.com>
---
tested with the new src/test/file_size_info_test.pm (fails on master with the uninitialized warnings, passes with the patch); run_plugin_tests.pl gives the same result before and after in my container
src/PVE/Storage/Plugin.pm | 5 +++
src/test/file_size_info_test.pm | 62 +++++++++++++++++++++++++++++++++
src/test/run_plugin_tests.pl | 1 +
3 files changed, 68 insertions(+)
create mode 100644 src/test/file_size_info_test.pm
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index a97f361..7690f24 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -1444,6 +1444,11 @@ sub file_size_info {
my ($size, $format, $used, $parent) =
$info->@{qw(virtual-size format actual-size backing-filename)};
+ for my $key (qw(virtual-size format actual-size)) {
+ return $handle_error->("qemu-img info did not report '$key' for '$filename'\n")
+ if !defined($info->{$key});
+ }
+
die "backing file not allowed for untrusted image '$filename'!\n" if $untrusted && $parent;
($size) = ($size =~ /^(\d+)$/); # untaint
diff --git a/src/test/file_size_info_test.pm b/src/test/file_size_info_test.pm
new file mode 100644
index 0000000..48840b8
--- /dev/null
+++ b/src/test/file_size_info_test.pm
@@ -0,0 +1,62 @@
+package PVE::Storage::TestFileSizeInfo;
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use File::Temp;
+use JSON qw(encode_json);
+use PVE::Storage::Plugin;
+use Test::MockModule;
+use Test::More;
+
+my $image = File::Temp->new();
+my $filename = $image->filename;
+
+my $qemu_img_info;
+my $mock_common = Test::MockModule->new('PVE::Storage::Common');
+$mock_common->redefine(qemu_img_info => sub { return encode_json($qemu_img_info); });
+
+my $complete_info = {
+ 'virtual-size' => 4194304,
+ 'actual-size' => 4096,
+ format => 'raw',
+};
+
+my sub run_file_size_info {
+ my ($info, $untrusted) = @_;
+
+ $qemu_img_info = $info;
+ my @warnings;
+ local $SIG{__WARN__} = sub { push @warnings, $_[0]; };
+ my @res = eval { PVE::Storage::Plugin::file_size_info($filename, undef, 'raw', $untrusted) };
+
+ return (\@res, $@, \@warnings);
+}
+
+{
+ my ($res, $err, $warnings) = run_file_size_info($complete_info);
+ is($err, '', 'complete info - no error');
+ is_deeply([$res->@[0 .. 2]], [4194304, 'raw', 4096], 'complete info - size, format and used');
+ is_deeply($warnings, [], 'complete info - no warnings');
+}
+
+for my $key (qw(actual-size virtual-size format)) {
+ my $info = { $complete_info->%* };
+ delete $info->{$key};
+ my $expected_msg = "qemu-img info did not report '$key' for '$filename'\n";
+
+ my ($res, $err, $warnings) = run_file_size_info($info);
+ is($err, '', "missing $key - no error for trusted image");
+ ok(!defined($res->[0]), "missing $key - no size returned");
+ is_deeply($warnings, [$expected_msg], "missing $key - single warning naming the file");
+
+ ($res, $err, $warnings) = run_file_size_info($info, 1);
+ is($err, $expected_msg, "missing $key - untrusted image dies naming the file");
+ is_deeply($warnings, [], "missing $key - no warnings for untrusted image");
+}
+
+done_testing();
+
+1;
diff --git a/src/test/run_plugin_tests.pl b/src/test/run_plugin_tests.pl
index da0d0de..029b668 100755
--- a/src/test/run_plugin_tests.pl
+++ b/src/test/run_plugin_tests.pl
@@ -21,6 +21,7 @@ my $res = $harness->runtests(
"free_image_test.pm",
"zfs_list_zvol_test.pm",
"content_types_test.pm",
+ "file_size_info_test.pm",
"plugin_metadata_test.pm",
"plugin_isolation_test.pm",
"plugin_load_test.pm",
--
2.43.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-02 14:21 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 14:18 [PATCH storage] fix #5805: plugin: file size info: handle missing fields in qemu-img output Michal Fox
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox