Skip to contentSkip to conversation

Add virtio disk identification support

PATCH In review john cooper 2 participants 3 messages Mar 25, 2010 – Jun 4, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 Add virtio disk identification support

Fix bug which truncated serial string to 8 bytes, nul terminate.

---

vl.c +1 -1
index d69250c..b74cbba 100644
@@ -1162,7 +1162,7 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque,
     dinfo->on_write_error = on_write_error;
     dinfo->opts = opts;
     if (serial)
-        strncpy(dinfo->serial, serial, sizeof(serial));
+        strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
     QTAILQ_INSERT_TAIL(&drives, dinfo, next);
 
     switch(type) {
Signature · 1 line
[email protected]
#1 lore raw
Quoted 10 lines · john cooper

Fix bug which truncated serial string to 8 bytes, nul terminate.

---

vl.c +0 -0
index d69250c..b74cbba 100644

vl.c DriveInfo *drive_init(QemuOpts *opts, void *opaque, · line 1166

Quoted 6 lines
vl.c +1 -1
@@ -1162,7 +1162,7 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque,
      dinfo->on_write_error = on_write_error;
      dinfo->opts = opts;
      if (serial)
-        strncpy(dinfo->serial, serial, sizeof(serial));
+        strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
 

You need to explicitly add a null terminator. Far better to just never use strncpy().

Regards,

Anthony Liguori

Quoted 5 lines
vl.c +0 -0
      QTAILQ_INSERT_TAIL(&drives, dinfo, next);
 
      switch(type) {
#2 lore raw
Quoted 6 lines · Anthony Liguori
Quoted 5 lines · john cooper

Fix bug which truncated serial string to 8 bytes, nul terminate.

---

Quoted 14 lines

vl.c DriveInfo *drive_init(QemuOpts *opts, void *opaque, · line 1166

Quoted 11 lines
vl.c +1 -1
index d69250c..b74cbba 100644
@@ -1162,7 +1162,7 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque,
      dinfo->on_write_error = on_write_error;
      dinfo->opts = opts;
      if (serial)
-        strncpy(dinfo->serial, serial, sizeof(serial));
+        strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
 

You need to explicitly add a null terminator. Far better to just never use strncpy().

As previous this is a case where dinfo->serial[] is defined as BLOCK_SERIAL_STRLEN + 1 bytes as an internal convenience. Above the context of the patch here is a:

    dinfo = qemu_mallocz(sizeof(*dinfo));

which assures this will do as intended, namely copy all potential BLOCK_SERIAL_STRLEN bytes and assure they are nul terminated should the full length be present.

I didn't conjure up the existing logic but rather am trying to peacefully coexist with it.

-john

Signature · 1 line
[email protected]