Skip to contentSkip to conversation

qemu/pci: clarify pci config load routine

PATCH In review Michael S. Tsirkin 2 participants 2 messages Oct 5, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu/pci: clarify pci config load routine

PCI load routine has to be called with size equal to 256 (otherwise it will crash in weird ways). So assert this, making code clearer. Also avoid dynamically sized array on stack - good for portability.

---
 hw/pci.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
hw/pci.c +5 -4
index ade778f..196297a 100644
@@ -193,14 +193,15 @@ int pci_bus_num(PCIBus *s)
 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
 {
     PCIDevice *s = container_of(pv, PCIDevice, config);
-    uint8_t config[size];
+    uint8_t config[PCI_CONFIG_SPACE_SIZE];
     int i;
 
-    qemu_get_buffer(f, config, size);
-    for (i = 0; i < size; ++i)
+    assert(size == sizeof config);
+    qemu_get_buffer(f, config, sizeof config);
+    for (i = 0; i < sizeof config; ++i)
         if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i])
             return -EINVAL;
-    memcpy(s->config, config, size);
+    memcpy(s->config, config, sizeof config);
 
     pci_update_mappings(s);
 
Signature · 1 line
1.6.5.rc2
#1 lore raw
Quoted 2 lines · Michael S. Tsirkin

PCI load routine has to be called with size equal to 256 (otherwise it will crash in weird ways). So assert this,

Agreed with the assert().

Quoted 2 lines

making code clearer.
Also avoid dynamically sized array on stack - good for portability.

size has the right value, namely sizeof(PCIDevice.config). Only real advantage is that you are not using a dynamically sized array on the stack.

I don't care one way or another. And as you are more probable to touch that code than me, it is up to you :)

Later, Juan.