diff --git a/src/qemu_driver.c b/src/qemu_driver.c index de31581322172f07025d7ee9fe743d6b80f5e54c..2ddcdc0f6e4d6af910d5618d9e851c82aee0842e 100644 --- a/src/qemu_driver.c +++ b/src/qemu_driver.c @@ -6206,14 +6206,20 @@ static int qemudDomainDetachHostPciDevice(virConnectPtr conn, pciFreeDevice(conn, pci); } - if (i != --vm->def->nhostdevs) - memmove(&vm->def->hostdevs[i], - &vm->def->hostdevs[i+1], - sizeof(*vm->def->hostdevs) * (vm->def->nhostdevs-i)); - if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs) < 0) { - virReportOOMError(conn); - ret = -1; + if (vm->def->nhostdevs > 1) { + memmove(vm->def->hostdevs + i, + vm->def->hostdevs + i + 1, + sizeof(*vm->def->hostdevs) * + (vm->def->nhostdevs - (i + 1))); + vm->def->nhostdevs--; + if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs) < 0) { + /* ignore, harmless */ + } + } else { + VIR_FREE(vm->def->hostdevs); + vm->def->nhostdevs = 0; } + virDomainHostdevDefFree(detach); return ret; }