dump: enhance dump_state_prepare fd initialization
Initializing descriptor with zero is unsafe: during cleanup we risk to unconditional close of fd == 0 in case dump state wasn't fully initialized. Thus, let's init fd with -1 value and check its value before closing it. Signed-off-by: Nikolai Barybin <nikolai.barybin@virtuozzo.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20250911123656.413160-2-nikolai.barybin@virtuozzo.com>
Nikolai Barybin committed
Sep 11, 2025 at 15:36 UTC
8114cd44c8a18d92a715fa77d6c136c67f807d20
1 file changed
+6
-3
dump/dump.c
+6
-3
@@ -104,7 +104,10 @@ static int dump_cleanup(DumpState *s)
104
105
guest_phys_blocks_free(&s->guest_phys_blocks);
106
memory_mapping_list_free(&s->list);
107
- close(s->fd);
107
+ if (s->fd != -1) {
108
+ close(s->fd);
109
+ }
110
+ s->fd = -1;
111
g_free(s->guest_note);
112
g_clear_pointer(&s->string_table_buf, g_array_unref);
113
s->guest_note = NULL;
@@ -1709,8 +1712,8 @@ static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1712
1713
static void dump_state_prepare(DumpState *s)
1714
{
1712
- /* zero the struct, setting status to active */
1713
- *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1715
+ /* zero the struct, setting status to active and fd to -1 */
1716
+ *s = (DumpState) { .fd = -1, .status = DUMP_STATUS_ACTIVE };
1717
}
1718
1719
bool qemu_system_dump_in_progress(void)