Commit 369852b536 for qemu.org

commit 369852b536ed817824d757c4f132f71bcc6d74e1
Author: Eric Blake <eblake@redhat.com>
Date:   Fri Nov 13 16:34:49 2026 -0600

    nbd: Refactor structured payload interpretation

    An upcoming patch wants to reuse the payload of a structured reply
    chunk in further code for additional safety checks on the server's
    response.  It will be easiest if the initial parse converts the
    payload from network order into host order along the way, so that I
    don't introduce subtle endian-specific bugs in that upcoming code. A
    quick audit shows that none of the current callers of
    payload_advance*() were reading a memory location within the payload
    after its initial parse.

    Signed-off-by: Eric Blake <eblake@redhat.com>
    Message-ID: <20261113223742.2546619-7-eblake@redhat.com>
    Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

diff --git a/block/nbd.c b/block/nbd.c
index e5e16722ba..052469b354 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -578,24 +578,36 @@ err:
     return rc;
 }

+/* Consume a field of payload, while swapping it in place to host-endian. */
 static inline uint16_t payload_advance16(uint8_t **payload)
 {
+    uint16_t value = lduw_be_p(*payload);
+    stw_he_p(*payload, value);
     *payload += 2;
-    return lduw_be_p(*payload - 2);
+    return value;
 }

 static inline uint32_t payload_advance32(uint8_t **payload)
 {
+    uint32_t value = ldl_be_p(*payload);
+    stl_he_p(*payload, value);
     *payload += 4;
-    return ldl_be_p(*payload - 4);
+    return value;
 }

 static inline uint64_t payload_advance64(uint8_t **payload)
 {
+    uint64_t value = ldq_be_p(*payload);
+    stq_he_p(*payload, value);
     *payload += 8;
-    return ldq_be_p(*payload - 8);
+    return value;
 }

+/*
+ * nbd_parse_offset_hole_payload:
+ * Parse a chunk for NBD_REPLY_TYPE_OFFSET_HOLE, updating payload in
+ * place to a host-endian description of the region described.
+ */
 static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
                                          NBDStructuredReplyChunk *chunk,
                                          uint8_t *payload, uint64_t orig_offset,