Skip to contentSkip to conversation

hd-geo-test: Clean up use of buf[] in create_qcow2_with_mbr()

PATCH Applied Markus Armbruster 3 participants 3 messages Mar 17, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hd-geo-test: Clean up use of buf[] in create_qcow2_with_mbr()

valgrind reports write unitialized bytes from buf[]. Clear them.

ASan reports we store to misaligned address in buf[]. Use stl_le_p() for that.

---
 tests/qtest/hd-geo-test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/qtest/hd-geo-test.c +3 -3
index a249800544..48e8e02d6e 100644
@@ -421,7 +421,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
     char *raw_path = strdup(template);
     char *qcow2_path = strdup(template);
     char cmd[100 + 2 * PATH_MAX];
-    uint8_t buf[512];
+    uint8_t buf[512] = {};
     int i, ret, fd, offset;
     uint64_t qcow2_size = sectors * 512;
     uint8_t status, parttype, head, sector, cyl;
@@ -457,8 +457,8 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
         buf[offset + 0x6] = sector;
         buf[offset + 0x7] = cyl;
 
-        (*(uint32_t *)&buf[offset + 0x8]) = cpu_to_le32(mbr[i].start_sect);
-        (*(uint32_t *)&buf[offset + 0xc]) = cpu_to_le32(mbr[i].nr_sects);
+        stl_le_p(&buf[offset + 0x8], mbr[i].start_sect);
+        stl_le_p(&buf[offset + 0xc], mbr[i].nr_sects);
 
         offset += 0x10;
     }
Signature · 1 line
2.21.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Markus Armbruster

valgrind reports write unitialized bytes from buf[]. Clear them.

ASan reports we store to misaligned address in buf[]. Use stl_le_p() for that.

---
  tests/qtest/hd-geo-test.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
tests/qtest/hd-geo-test.c +1 -1
index a249800544..48e8e02d6e 100644
@@ -421,7 +421,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
      char *raw_path = strdup(template);
      char *qcow2_path = strdup(template);
      char cmd[100 + 2 * PATH_MAX];
-    uint8_t buf[512];
+    uint8_t buf[512] = {};
      int i, ret, fd, offset;
      uint64_t qcow2_size = sectors * 512;
      uint8_t status, parttype, head, sector, cyl;

tests/qtest/hd-geo-test.c static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors) · line 464

Quoted 10 lines
tests/qtest/hd-geo-test.c +2 -2
@@ -457,8 +457,8 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
          buf[offset + 0x6] = sector;
          buf[offset + 0x7] = cyl;
 
-        (*(uint32_t *)&buf[offset + 0x8]) = cpu_to_le32(mbr[i].start_sect);
-        (*(uint32_t *)&buf[offset + 0xc]) = cpu_to_le32(mbr[i].nr_sects);
+        stl_le_p(&buf[offset + 0x8], mbr[i].start_sect);
+        stl_le_p(&buf[offset + 0xc], mbr[i].nr_sects);
 
          offset += 0x10;
      }
Applied
#2 lore raw
In reply to Markus Armbruster
Quoted 27 lines · Markus Armbruster

valgrind reports write unitialized bytes from buf[]. Clear them.

ASan reports we store to misaligned address in buf[]. Use stl_le_p() for that.

---
 tests/qtest/hd-geo-test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/qtest/hd-geo-test.c +1 -1
index a249800544..48e8e02d6e 100644
@@ -421,7 +421,7 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
     char *raw_path = strdup(template);
     char *qcow2_path = strdup(template);
     char cmd[100 + 2 * PATH_MAX];
-    uint8_t buf[512];
+    uint8_t buf[512] = {};
     int i, ret, fd, offset;
     uint64_t qcow2_size = sectors * 512;
     uint8_t status, parttype, head, sector, cyl;

tests/qtest/hd-geo-test.c static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors) · line 464

Quoted 10 lines
tests/qtest/hd-geo-test.c +2 -2
@@ -457,8 +457,8 @@ static char *create_qcow2_with_mbr(MBRpartitions mbr, uint64_t sectors)
         buf[offset + 0x6] = sector;
         buf[offset + 0x7] = cyl;
 
-        (*(uint32_t *)&buf[offset + 0x8]) = cpu_to_le32(mbr[i].start_sect);
-        (*(uint32_t *)&buf[offset + 0xc]) = cpu_to_le32(mbr[i].nr_sects);
+        stl_le_p(&buf[offset + 0x8], mbr[i].start_sect);
+        stl_le_p(&buf[offset + 0xc], mbr[i].nr_sects);
 
         offset += 0x10;
     }

Staged.

Trying out our gitlab CI pipelines now:

https://gitlab.com/jsnow/qemu/-/tree/ide

Traditional message:

Thanks, applied to my IDE tree:

https://github.com/jnsnow/qemu/commits/ide
https://github.com/jnsnow/qemu.git

--js