If the guest can only see the first 20 characters of the serial, we
shouldn't pretend to have a longer string in the 'serial' qdev property
(which is visible through things like QOM or 'info qtree').

Truncate the serial already in the internal state of the device to make
sure we don't mix both versions anywhere.

Signed-off-by: Kevin Wolf <[email protected]>
---
 hw/block/virtio-blk.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
index eeeb405127c..cecd08a2f2e 100644
--- a/hw/block/virtio-blk.c
+++ b/hw/block/virtio-blk.c
@@ -1850,9 +1850,15 @@ static void virtio_blk_device_realize(DeviceState *dev, 
Error **errp)
     }
 
     if (conf->serial && strlen(conf->serial) > VIRTIO_BLK_ID_BYTES) {
+        char *truncated;
+
         warn_report("serial is longer than %d characters and will be truncated 
"
                     "for the guest. This configuration is deprecated.",
                     VIRTIO_BLK_ID_BYTES);
+
+        truncated = g_strndup(conf->serial, VIRTIO_BLK_ID_BYTES);
+        g_free(conf->serial);
+        conf->serial = truncated;
     }
 
     if (!blkconf_apply_backend_options(&conf->conf,
-- 
2.56.0

Reply via email to