Commit 3af4d0a059 for qemu.org
commit 3af4d0a0593dce167a1b1c53747a234f11828933
Author: Peter Maydell <peter.maydell@linaro.org>
Date: Thu Nov 19 14:03:38 2026 +0000
tests/qemu-iotest: Add test for qcow2 image with empty reftable
This test creates a qcow2 image, manually zeroes out its entire
reftable, and then runs 'qemu-img amend -o refcount_bits=64" on it.
This used to cause qemu-img to crash; now we will report that the
image can't be opened because it's corrupted.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Message-ID: <20261119140338.2918386-3-peter.maydell@linaro.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
diff --git a/tests/qemu-iotests/315 b/tests/qemu-iotests/315
new file mode 100755
index 0000000000..ff1a22bddd
--- /dev/null
+++ b/tests/qemu-iotests/315
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+# group: rw auto quick
+#
+# Test handling of corrupt qcow2 file with all-zeroes reftable
+#
+# Copyright (c) 2026 Linaro Ltd
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+status=1 # failure is the default!
+
+_cleanup()
+{
+ _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+# This tests qcow2-specific low-level functionality
+_supported_fmt qcow2
+_supported_proto file
+_supported_os Linux
+# We are going to check changing refcount_bits from 16 to 64,
+# so anything else is not supported
+_unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)'
+
+echo
+echo "=== Testing empty refcount table ==="
+echo
+
+# Create an image, then modify it to zero out the entire reftable.
+# Check that this corrupted image doesn't cause qemu-img amend to crash.
+
+_make_test_img -o 'refcount_bits=16' 64K
+
+reftable_ofs=$(peek_file_be "$TEST_IMG" 48 8)
+reftable_clusters=$(peek_file_be "$TEST_IMG" 56 4)
+cluster_bits=$(peek_file_be "$TEST_IMG" 20 4)
+# Cluster size is (1 << cluster_bits). Zero out the data starting
+# at reftable_ofs for (cluster_size * reftable_clusters) bytes.
+reftable_size=$(((1 << cluster_bits) * reftable_clusters))
+
+dd "of=$TEST_IMG" "bs=1" "seek=$reftable_ofs" "count=$reftable_size" if=/dev/zero conv=notrunc >&/dev/null
+
+# This should complain that the image is corrupt
+$QEMU_IMG amend -f qcow2 -o refcount_bits=64 "$TEST_IMG" 2>&1
+
+# success, all done
+echo
+echo '*** done'
+rm -f $seq.full
+status=0
+
diff --git a/tests/qemu-iotests/315.out b/tests/qemu-iotests/315.out
new file mode 100644
index 0000000000..91b718df5c
--- /dev/null
+++ b/tests/qemu-iotests/315.out
@@ -0,0 +1,9 @@
+QA output created by 315
+
+=== Testing empty refcount table ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=65536
+qcow2: Marking image as corrupt: first refblock is empty; further corruption events will be suppressed
+qemu-img: Image is corrupt (first refblock is empty)
+
+*** done