Skip to contentSkip to conversation

hw/xen: Check if len is 0 before memcpy()

PATCH In review Akihiko Odaki 3 participants 3 messages Jan 8, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/xen: Check if len is 0 before memcpy()

data->data can be NULL when len is 0. Strictly speaking, the behavior of memcpy() in such a scenario is undefined so UBSan complaints.

Satisfy UBSan by checking if len is 0 before memcpy().

---
 hw/i386/kvm/xen_xenstore.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/i386/kvm/xen_xenstore.c +4 -0
index 59691056670e..17802aa33d20 100644
@@ -532,6 +532,10 @@ static void xs_read(XenXenstoreState *s, unsigned int req_id,
         return;
     }
 
+    if (!len) {
+        return;
+    }
+
     memcpy(&rsp_data[rsp->len], data->data, len);
     rsp->len += len;
 }

---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20241227-xen-fb5a15cc0ca7

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
Reviewed-by on v1 1/1
#2 lore raw
In reply to Akihiko Odaki
Quoted 6 lines · Akihiko Odaki

data->data can be NULL when len is 0. Strictly speaking, the behavior of memcpy() in such a scenario is undefined so UBSan complaints.

Satisfy UBSan by checking if len is 0 before memcpy().

Thanks.

Quoted 26 lines

---
 hw/i386/kvm/xen_xenstore.c | 4 ++++

 1 file changed, 4 insertions(+)
hw/i386/kvm/xen_xenstore.c +0 -0
index 59691056670e..17802aa33d20 100644
@@ -532,6 +532,10 @@ static void xs_read(XenXenstoreState *s,
 unsigned int req_id,
          return;
      }


+    if (!len) {
+        return;
+    }
+
     memcpy(&rsp_data[rsp->len], data->data, len); rsp->len += len;
 }

---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20241227-xen-fb5a15cc0ca7

Best regards,