You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2026/10/01 13:18:44 UTC

(qpid-proton) branch main updated (d793f97e4 -> c58a80270)

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git


    from d793f97e4 PROTON-2976: Make pn_ssl_domain_set_trusted_ca_db replace the default CAs
     new 9e02ba75d PROTON-2978: Improve transfer performative validation
     new c58a80270 PROTON-2979: Omit delivery-id, tag and message-format on continuation transfers

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 c/src/core/consumers.h              |  17 ++++
 c/src/core/engine-internal.h        |   2 +
 c/src/core/engine.c                 |   3 +
 c/src/core/transport.c              | 179 +++++++++++++++++++++++-------------
 c/tests/engine_test.cpp             |  28 ++++--
 c/tools/codec-generator/generate.py |   2 +
 c/tools/codec-generator/specs.json  |   4 +-
 python/tests/proton_tests/engine.py | 173 +++++++++++++++++++++++++++++++++-
 8 files changed, 330 insertions(+), 78 deletions(-)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


(qpid-proton) 02/02: PROTON-2979: Omit delivery-id, tag and message-format on continuation transfers

Posted by as...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git

commit c58a802706858da15f742086ecdda776d14eef5d
Author: Andrew Stitcher <as...@apache.org>
AuthorDate: Wed Sep 30 23:02:11 2026 -0400

    PROTON-2979: Omit delivery-id, tag and message-format on continuation transfers
    
    These three fields are carried on the first transfer of a delivery and may be
    omitted on the continuation transfers that follow. We already accept peers that
    omit them, but always sent them on every frame of a multi-frame delivery.
    
    pni_post_amqp_transfer_frame now takes a continuation flag and passes it as the
    presence bool for the three fields. The frame loop sends as many frames as it
    can from one encoded performative, so the flag has to be maintained as it goes:
    a transfer is a continuation exactly when the preceding one set 'more', so it
    follows more_flag and marks the performative stale when that changes.
    
    Continuation frames carry a little more payload as a side effect, their
    performative being shorter by the length of the delivery-tag.
    
    Assisted-By: Claude Opus 5 <no...@anthropic.com>
---
 c/src/core/transport.c              | 72 ++++++++++++++++++++++---------------
 c/tests/engine_test.cpp             | 28 +++++++++------
 c/tools/codec-generator/generate.py |  2 ++
 c/tools/codec-generator/specs.json  |  2 +-
 python/tests/proton_tests/engine.py | 58 +++++++++++++++++++++++++-----
 5 files changed, 114 insertions(+), 48 deletions(-)

diff --git a/c/src/core/transport.c b/c/src/core/transport.c
index 6012143ca..0f7934906 100644
--- a/c/src/core/transport.c
+++ b/c/src/core/transport.c
@@ -863,33 +863,40 @@ static int pni_post_amqp_transfer_frame(pn_transport_t *transport, uint16_t ch,
                                         pn_disposition_t *disposition,
                                         bool resume,
                                         bool aborted,
-                                        bool batchable)
+                                        bool batchable,
+                                        bool continuation)
 {
   bool more_flag = more;
   unsigned framecount = 0;
-
-  // create performative, assuming 'more' flag need not change
- compute_performatives:;
-  /* "DL[IIzI?o?on?DLC?o?o?o]" */
-  pn_bytes_t performative =
-    pn_amqp_encode_transfer(&transport->scratch_space, AMQP_DESC_TRANSFER,
-                         handle,
-                         id,
-                         tag.size, tag.start,
-                         message_format,
-                         settled, settled,
-                         more_flag, more_flag,
-                         disposition,
-                         resume, resume,
-                         aborted, aborted,
-                         batchable, batchable);
-  if (!performative.start) {
-    return PN_ERR;
-  }
-
-  // At this point the side affect of the fill is to encode the performative into transport->scratch_space
-
-  do { // send as many frames as possible without changing the 'more' flag...
+  pn_bytes_t performative = {0, NULL};
+  // The encoded performative is stale whenever one of the fields it depends on
+  // changes: the 'more' flag, or whether this is a continuation transfer.
+  bool stale = true;
+
+  do { // send as many frames as possible without re-encoding...
+
+    if (stale) {
+      /* "DL[I?I?Z?I?o?ond?o?o?o]" */
+      // delivery-id, delivery-tag and message-format are only carried by the
+      // first transfer of a delivery; continuation transfers must omit them.
+      performative =
+        pn_amqp_encode_transfer(&transport->scratch_space, AMQP_DESC_TRANSFER,
+                             handle,
+                             !continuation, id,
+                             !continuation, tag.size, tag.start,
+                             !continuation, message_format,
+                             settled, settled,
+                             more_flag, more_flag,
+                             disposition,
+                             resume, resume,
+                             aborted, aborted,
+                             batchable, batchable);
+      if (!performative.start) {
+        return PN_ERR;
+      }
+      // At this point the side affect of the fill is to encode the performative into transport->scratch_space
+      stale = false;
+    }
 
     // check if we need to break up the outbound frame
     size_t available = full_payload->size;
@@ -908,12 +915,14 @@ static int pni_post_amqp_transfer_frame(pn_transport_t *transport, uint16_t ch,
         available = max_payload - performative.size;
         if (more_flag == false) {
           more_flag = true;
-          goto compute_performatives;  // deal with flag change
+          stale = true;
+          continue;  // deal with flag change
         }
       } else if (more_flag == true && more == false) {
         // caller has no more, and this is the last frame
         more_flag = false;
-        goto compute_performatives;
+        stale = true;
+        continue;
       }
     }
     pn_bytes_t payload = {.size = available, .start = full_payload->start};
@@ -922,7 +931,12 @@ static int pni_post_amqp_transfer_frame(pn_transport_t *transport, uint16_t ch,
     full_payload->start += available;
     full_payload->size -= available;
     framecount++;
-  } while (full_payload->size > 0 && framecount < frame_limit);
+    // The next transfer continues this one exactly when this one said 'more'.
+    if (continuation != more_flag) {
+      continuation = more_flag;
+      stale = true;
+    }
+  } while (full_payload->size != 0 && framecount < (unsigned) frame_limit);
 
   return framecount;
 }
@@ -2307,7 +2321,9 @@ static int pni_process_tpwork_sender(pn_transport_t *transport, pn_delivery_t *d
                                                &delivery->local,
                                                false, /* Resume */
                                                delivery->aborted,
-                                               false /* Batchable */
+                                               false, /* Batchable */
+                                               /* Continuation: a previously sent frame said 'more' */
+                                               state->sending
       );
       if (count < 0) return count;
       state->sending = true;
diff --git a/c/tests/engine_test.cpp b/c/tests/engine_test.cpp
index 60f564aa5..e6f1d4c4b 100644
--- a/c/tests/engine_test.cpp
+++ b/c/tests/engine_test.cpp
@@ -428,13 +428,17 @@ TEST_CASE("session_capacity") {
 
   // This is complicated by messy accounting: max_frame_size is a proxy for frames buffered on the
   // receiver side, but payload per transfer frame is strictly less than max frame size due to
-  // frame headers.  For this test 997 bytes of payload fits in a 1024 byte transfer frame.
+  // frame headers.  Only the first transfer of a delivery carries delivery-id, delivery-tag and
+  // message-format; continuation transfers omit them, so their performative is smaller and they
+  // carry correspondingly more payload.  For this test, with a 6 byte delivery-tag, 997 bytes of
+  // payload fit in the first 1024 byte transfer frame and 1004 bytes in each continuation frame.
   // Senders and receivers count/update frames a bit differently.
 
-  size_t payloadsz = 997;
-  size_t onefrm = 1 * payloadsz;
-  size_t fourfrm = 4 * payloadsz;
-  size_t fivefrm = 5 * payloadsz;
+  size_t firstfrm = 997;
+  size_t contfrm = 1004;
+  size_t onefrm = contfrm;
+  size_t fourfrm = firstfrm + 3 * contfrm;
+  size_t fivefrm = firstfrm + 4 * contfrm;
 
   pn_delivery_t *d1 = pn_delivery(tx, pn_dtag("tag-1", 6));
   REQUIRE(link_send(tx, fivefrm) == (ssize_t) fivefrm);
@@ -533,13 +537,17 @@ TEST_CASE("session_window") {
 
   // This is complicated by messy accounting: max_frame_size is a proxy for frames buffered on the
   // receiver side, but payload per transfer frame is strictly less than max frame size due to
-  // frame headers.  For this test 997 bytes of payload fits in a 1024 byte transfer frame.
+  // frame headers.  Only the first transfer of a delivery carries delivery-id, delivery-tag and
+  // message-format; continuation transfers omit them, so their performative is smaller and they
+  // carry correspondingly more payload.  For this test, with a 6 byte delivery-tag, 997 bytes of
+  // payload fit in the first 1024 byte transfer frame and 1004 bytes in each continuation frame.
   // Senders and receivers count/update frames a bit differently.
 
-  size_t payloadsz = 997;
-  size_t onefrm = 1 * payloadsz;
-  size_t fourfrm = 4 * payloadsz;
-  size_t fivefrm = 5 * payloadsz;
+  size_t firstfrm = 997;
+  size_t contfrm = 1004;
+  size_t onefrm = contfrm;
+  size_t fourfrm = firstfrm + 3 * contfrm;
+  size_t fivefrm = firstfrm + 4 * contfrm;
 
   REQUIRE(pn_link_credit(txa) > 0);
   REQUIRE(pn_link_credit(txb) > 0);
diff --git a/c/tools/codec-generator/generate.py b/c/tools/codec-generator/generate.py
index 2dd292f8a..dfcbb318e 100644
--- a/c/tools/codec-generator/generate.py
+++ b/c/tools/codec-generator/generate.py
@@ -34,6 +34,8 @@ Structural:
     ]       End of list
     @T      Start of typed array (T = type marker)
     ?       Optional field - followed by presence boolean, then the value
+            (pair with Z rather than z: z treats null as a value in its own
+            right, so ?z would always report the field as present)
     !       Suffix: omit entire described list if resulting list would be empty
 
 Primitive Types:
diff --git a/c/tools/codec-generator/specs.json b/c/tools/codec-generator/specs.json
index 8eb4e73f0..fb00e0173 100644
--- a/c/tools/codec-generator/specs.json
+++ b/c/tools/codec-generator/specs.json
@@ -7,7 +7,7 @@
     ["DL[SIoBB?DL[SIsIoR?sRnMM]?DL[SIsIoRM]nnILnnR]", "attach"],
     ["DL[SIoBB?DL[SIsIoR?sRnRR]DL[R]nnI]", "attach_coordinator"],
     ["DL[?IIII?I?I?In?o]", "flow"],
-    ["DL[IIZI?o?ond?o?o?o]", "transfer"],
+    ["DL[I?I?Z?I?o?ond?o?o?o]", "transfer"],
     ["DL[oI?I?o?DL[]]", "disposition_batch"],
     ["DL[oIn?od]", "disposition"],
     ["DL[I?oc]", "detach"],
diff --git a/python/tests/proton_tests/engine.py b/python/tests/proton_tests/engine.py
index a7fccee91..9de95e845 100644
--- a/python/tests/proton_tests/engine.py
+++ b/python/tests/proton_tests/engine.py
@@ -1019,8 +1019,10 @@ class TransferTest(Test):
         assert sd.aborted
 
         # Confirm abort discards the sender's buffered content, i.e. no data in generated transfer frame.
+        # The abort is a continuation of the delivery, so delivery-id, delivery-tag and
+        # message-format are omitted.
         # We want:
-        # @transfer(20) [handle=0, delivery-id=0, delivery-tag=b"tag", message-format=0, settled=true, aborted=true]
+        # @transfer(20) [handle=0, settled=true, aborted=true]
         # wanted = b"\x00\x00\x00%\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x15\x00\x00\x00\nR\x00R'\
         #          b'\x00\xa0\x03tagR\x00A@@@@A"
         # wanted = b"\x00\x00\x00\x26\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x16\x00\x00\x00\x0bR\x00R'\
@@ -1028,7 +1030,8 @@ class TransferTest(Test):
         # wanted = b'\x00\x00\x00\x20\x02\x00\x00\x00\x00S\x14\xc0\x13\x0bR\x00R\x00\xa0\x03tagR\x00A@@@@A@'
         # wanted = b'\x00\x00\x00"\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x12\x00\x00\x00\nCC\xa0\x03tagCA@@@@A'
         # wanted = b'\x00\x00\x00\x1d\x02\x00\x00\x00\x00S\x14\xc0\x10\x0bCC\xa0\x03tagCA@@@@A@'
-        wanted = b'\x00\x00\x00\x1c\x02\x00\x00\x00\x00S\x14\xc0\x0f\x0aCC\xa0\x03tagCA@@@@A'
+        # wanted = b'\x00\x00\x00\x1c\x02\x00\x00\x00\x00S\x14\xc0\x0f\x0aCC\xa0\x03tagCA@@@@A'
+        wanted = b'\x00\x00\x00\x18\x02\x00\x00\x00\x00S\x14\xc0\x0b\x0aC@@@A@@@@A'
         t = self.snd.transport
         wire_bytes = t.peek(1024)
         assert wanted == wire_bytes, wire_bytes
@@ -1171,26 +1174,60 @@ class TransferTest(Test):
         assert cond.name == "amqp:invalid-field", cond
         assert cond.description == "message-format required on initial delivery transfer", cond
 
+    # A continuation transfer omits delivery-id, delivery-tag and message-format, so
+    # its performative list starts: handle (uint0, 0x43), then three nulls (0x40).
+    # Each test below puts one of those fields back with a value that does not match
+    # the first transfer of the delivery.
+
     def test_multiframe_continuation_tag_mismatch(self):
-        cond = self._corrupt_continuation(b"\xa0\x04tag1", b"\xa0\x04tagZ")
+        # Replace the null delivery-tag with b"tagZ" (vbin8 0xa0), not b"tag1".
+        cond = self._corrupt_continuation(b"\x43\x40\x40", b"\x43\x40\xa0\x04tagZ")
         assert cond is not None, "mismatched continuation delivery-tag accepted"
         assert cond.name == "amqp:invalid-field", cond
         assert cond.description == "invalid delivery-tag for a continuation transfer", cond
 
     def test_multiframe_continuation_id_mismatch(self):
-        # delivery-id 0 is encoded as uint0 (0x43); make it 1 (smalluint 0x52 0x01).
-        cond = self._corrupt_continuation(b"\x43\xa0\x04tag1", b"\x52\x01\xa0\x04tag1")
+        # Replace the null delivery-id with 1 (smalluint 0x52 0x01), not 0.
+        cond = self._corrupt_continuation(b"\x43\x40", b"\x43\x52\x01")
         assert cond is not None, "mismatched continuation delivery-id accepted"
         assert cond.name == "amqp:invalid-field", cond
         assert cond.description == "invalid delivery-id for a continuation transfer", cond
 
     def test_multiframe_continuation_format_mismatch(self):
-        # message-format follows the delivery-tag, 0 encoded as uint0 (0x43).
-        cond = self._corrupt_continuation(b"\xa0\x04tag1\x43", b"\xa0\x04tag1\x52\x01")
+        # Replace the null message-format with 1 (smalluint 0x52 0x01), not 0.
+        cond = self._corrupt_continuation(b"\x43\x40\x40\x40", b"\x43\x40\x40\x52\x01")
         assert cond is not None, "mismatched continuation message-format accepted"
         assert cond.name == "amqp:invalid-field", cond
         assert cond.description == "invalid message-format for a continuation transfer", cond
 
+    def test_multiframe_continuation_omits_fields(self):
+        # The first transfer of a delivery carries delivery-id, delivery-tag and
+        # message-format; continuation transfers must not repeat them.
+        self.rcv.flow(1)
+        self.pump()  # get the credit to the sender
+        t1 = self.snd.transport
+        t1.pop(len(t1.peek(4096)))  # drain any pending output
+
+        self.snd.delivery("tag1")
+        self.snd.send(b"this is a test")
+        # @transfer(20) [handle=0, delivery-id=0, delivery-tag=b"tag1", message-format=0, more=true]
+        # handle, delivery-id and message-format are uint0 (0x43), settled is null (0x40).
+        first = b'\x00\x00\x00\x27\x02\x00\x00\x00\x00S\x14\xc0\x0c\x06CC\xa0\x04tag1C@Athis is a test'
+        assert t1.peek(4096) == first, repr(t1.peek(4096))
+        t1.pop(len(t1.peek(4096)))
+
+        self.snd.send(b"this is more")
+        # @transfer(20) [handle=0, more=true] - id, tag and format are all null (0x40).
+        cont = b'\x00\x00\x00\x20\x02\x00\x00\x00\x00S\x14\xc0\x07\x06C@@@@Athis is more'
+        assert t1.peek(4096) == cont, repr(t1.peek(4096))
+        t1.pop(len(t1.peek(4096)))
+
+        # The receiver accepts the continuation and reassembles the delivery.
+        self.rcv.transport.push(first + cont)
+        assert self.rcv.transport.condition is None, self.rcv.transport.condition
+        assert self.rcv.current.partial
+        assert self.rcv.recv(1024) == b"this is a testthis is more"
+
     def test_disposition(self):
         self.rcv.flow(1)
 
@@ -1575,7 +1612,9 @@ class MaxFrameTransferTest(Test):
         sd.abort()
         assert sd.aborted
         # Expect a single abort transfer frame with no content.  One credit is consumed.
-        # @transfer(20) [handle=0, delivery-id=0, delivery-tag=b"tag_1", message-format=0, settled=true, aborted=true]
+        # The abort is a continuation of the delivery, so delivery-id, delivery-tag and
+        # message-format are omitted.
+        # @transfer(20) [handle=0, settled=true, aborted=true]
         # wanted = b"\x00\x00\x00\x27\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x17\x00\x00\x00\nR\x00R'\
         #          b'\x00\xa0\x05tag_1R\x00A@@@@A"
         # wanted = b"\x00\x00\x00\x28\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x18\x00\x00\x00\x0bR\x00R'\
@@ -1583,7 +1622,8 @@ class MaxFrameTransferTest(Test):
         # wanted = b'\x00\x00\x00\x22\x02\x00\x00\x00\x00S\x14\xc0\x15\x0bR\x00R\x00\xa0\x05tag_1R\x00A@@@@A@'
         # wanted = b'\x00\x00\x00\x24\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x14\x00\x00\x00\nCC\xa0\x05tag_1CA@@@@A'
         # wanted = b'\x00\x00\x00\x1f\x02\x00\x00\x00\x00S\x14\xc0\x12\x0bCC\xa0\x05tag_1CA@@@@A@'
-        wanted = b'\x00\x00\x00\x1e\x02\x00\x00\x00\x00S\x14\xc0\x11\x0aCC\xa0\x05tag_1CA@@@@A'
+        # wanted = b'\x00\x00\x00\x1e\x02\x00\x00\x00\x00S\x14\xc0\x11\x0aCC\xa0\x05tag_1CA@@@@A'
+        wanted = b'\x00\x00\x00\x18\x02\x00\x00\x00\x00S\x14\xc0\x0b\x0aC@@@A@@@@A'
         t = self.snd.transport
         wire_bytes = t.peek(2048)
         assert wanted == wire_bytes, wire_bytes


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


(qpid-proton) 01/02: PROTON-2978: Improve transfer performative validation

Posted by as...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git

commit 9e02ba75dcaf4b257646ccb5c633a533b224e478
Author: Andrew Stitcher <as...@apache.org>
AuthorDate: Mon Aug 24 21:43:50 2026 -0400

    PROTON-2978: Improve transfer performative validation
    
    - Ensure that our transfer frame will fit inside the allowed frame size
    - Correct checking for delivery-id, delivery-tag and message-format
      - Every delivery must have an id. If present in continuation
        transfers it must be the same id
      - Every delivery must have a tag. If present in continuation
        transfers it must be the same tag
      - Every delivery must have a message-format. If present in
        continuation transfers it must be the same message-format.
        This field was not previously decoded at all
    - Add tests for the above, and for discarding the remaining frames of a
      partial delivery that the application has already settled
---
 c/src/core/consumers.h              |  17 +++++
 c/src/core/engine-internal.h        |   2 +
 c/src/core/engine.c                 |   3 +
 c/src/core/transport.c              | 107 ++++++++++++++++++++----------
 c/tools/codec-generator/specs.json  |   4 +-
 python/tests/proton_tests/engine.py | 125 ++++++++++++++++++++++++++++++++++++
 6 files changed, 222 insertions(+), 36 deletions(-)

diff --git a/c/src/core/consumers.h b/c/src/core/consumers.h
index 92bd0638a..a4fffa4f1 100644
--- a/c/src/core/consumers.h
+++ b/c/src/core/consumers.h
@@ -752,4 +752,21 @@ static inline bool consume_binaryornull(pni_consumer_t *consumer, pn_bytes_t *bi
   }
 }
 
+static inline bool consume_binarynonull(pni_consumer_t *consumer, pn_bytes_t *binary) {
+  uint8_t type;
+  *binary  = (pn_bytes_t){.size=0, .start=0};
+  if (!pni_consumer_readf8(consumer, &type)) return false;
+  switch (type) {
+    case PNE_VBIN32:{
+      return pni_consumer_readv32(consumer, binary);
+    }
+    case PNE_VBIN8:{
+      return pni_consumer_readv8(consumer, binary);
+    }
+    default:
+      pni_consumer_skip_value(consumer, type);
+      return false;
+  }
+}
+
 #endif // PROTON_CONSUMERS_H
diff --git a/c/src/core/engine-internal.h b/c/src/core/engine-internal.h
index 1ddeea913..8dfbf979a 100644
--- a/c/src/core/engine-internal.h
+++ b/c/src/core/engine-internal.h
@@ -327,6 +327,8 @@ struct pn_link_t {
   pn_sequence_t credit;
   pn_sequence_t queued;
   pn_sequence_t more_id;
+  pn_delivery_tag_t more_tag; // owned copy, valid while more_pending
+  uint32_t more_format;
   int drained; // number of drained credits
   uint8_t snd_settle_mode;
   uint8_t rcv_settle_mode;
diff --git a/c/src/core/engine.c b/c/src/core/engine.c
index 1c6f7a9dc..66bb7e1e2 100644
--- a/c/src/core/engine.c
+++ b/c/src/core/engine.c
@@ -1252,6 +1252,7 @@ static void pn_link_finalize(void *object)
     pn_free(link->unsettled_head);
   }
 
+  pn_bytes_free(link->more_tag);
   pn_free(link->context);
   pni_terminus_free(&link->source);
   pni_terminus_free(&link->target);
@@ -1304,6 +1305,8 @@ pn_link_t *pn_link_new(int type, pn_session_t *session, pn_string_t *name)
   link->credit = 0;
   link->queued = 0;
   link->more_id = 0;
+  link->more_tag = pn_bytes_null;
+  link->more_format = 0;
   link->drain = false;
   link->drain_flag_mode = true;
   link->drained = 0;
diff --git a/c/src/core/transport.c b/c/src/core/transport.c
index 4d63ef1a2..6012143ca 100644
--- a/c/src/core/transport.c
+++ b/c/src/core/transport.c
@@ -894,8 +894,18 @@ static int pni_post_amqp_transfer_frame(pn_transport_t *transport, uint16_t ch,
     // check if we need to break up the outbound frame
     size_t available = full_payload->size;
     if (transport->remote_max_frame) {
-      if ((available + performative.size) > transport->remote_max_frame - AMQP_HEADER_SIZE) {
-        available = transport->remote_max_frame - AMQP_HEADER_SIZE - performative.size;
+      size_t max_payload = transport->remote_max_frame - AMQP_HEADER_SIZE;
+      // The performative must fit, and must leave room for at least one byte of
+      // any payload still to send, otherwise we can never make progress.
+      if (performative.size > max_payload ||
+          (performative.size == max_payload && available > 0)) {
+        return pn_do_error(transport, "amqp:frame-size-too-small",
+                           "transfer performative does not fit remote max-frame: max payload %zu, performative %zu",
+                           max_payload, performative.size);
+      }
+
+      if ((available + performative.size) > max_payload) {
+        available = max_payload - performative.size;
         if (more_flag == false) {
           more_flag = true;
           goto compute_performatives;  // deal with flag change
@@ -1335,13 +1345,37 @@ static void pn_full_settle(pn_delivery_map_t *db, pn_delivery_t *delivery)
 
 static void pni_amqp_decode_disposition (uint64_t type, pn_bytes_t disp_data, pn_disposition_t *disp);
 
+// link->more_pending is true exactly while a multiframe delivery is assembling
+// on the link.  more_id/more_tag identify that delivery, and outlive the
+// delivery object itself: the application may settle a partial delivery, after
+// which the remaining frames still have to be matched and discarded.
+static void pni_link_more_begin(pn_link_t *link, pn_sequence_t id, pn_bytes_t tag, uint32_t format)
+{
+  assert(!link->more_pending);
+  link->more_pending = true;
+  link->more_id = id;
+  link->more_tag = pn_bytes_dup(tag);
+  link->more_format = format;
+}
+
+static void pni_link_more_end(pn_link_t *link)
+{
+  link->more_pending = false;
+  pn_bytes_free(link->more_tag);
+  link->more_tag = pn_bytes_null;
+  link->more_format = 0;
+}
+
 int pn_do_transfer(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_bytes_t payload)
 {
   // XXX: multi transfer
   uint32_t handle;
+  bool tag_present;
   pn_bytes_t tag;
   bool id_present;
   pn_sequence_t id;
+  bool format_present;
+  uint32_t format;
   bool settled;
   bool more;
   bool has_type, settled_set;
@@ -1350,7 +1384,8 @@ int pn_do_transfer(pn_transport_t *transport, uint8_t frame_type, uint16_t chann
 
   pn_bytes_t disp_data;
   size_t dsize =
-    pn_amqp_decode_transfer(payload, &handle, &id_present, &id, &tag,
+    pn_amqp_decode_transfer(payload, &handle, &id_present, &id, &tag_present, &tag,
+                                        &format_present, &format,
                                         &settled_set, &settled, &more, &has_type, &type, &disp_data,
                                         &resume, &aborted, &batchable);
   payload.size -= dsize;
@@ -1370,41 +1405,42 @@ int pn_do_transfer(pn_transport_t *transport, uint8_t frame_type, uint16_t chann
     return pn_do_error(transport, "amqp:invalid-field", "no such handle: %u", handle);
   }
   pn_delivery_t *delivery = NULL;
-  bool new_delivery = false;
+  // link->more_pending is true exactly while a multiframe delivery is still
+  // assembling on this link, so any transfer arriving now must continue it.
   if (link->more_pending) {
-    // Ongoing multiframe delivery.
+    // A continuation transfer may omit delivery-id, delivery-tag and
+    // message-format, but any it carries must match the first transfer.
+    if (id_present && id != link->more_id)
+      return pn_do_error(transport, "amqp:invalid-field", "invalid delivery-id for a continuation transfer");
+    if (tag_present && !pn_bytes_equal(tag, link->more_tag))
+      return pn_do_error(transport, "amqp:invalid-field", "invalid delivery-tag for a continuation transfer");
+    if (format_present && format != link->more_format)
+      return pn_do_error(transport, "amqp:invalid-field", "invalid message-format for a continuation transfer");
     if (link->unsettled_tail && !link->unsettled_tail->done) {
       delivery = link->unsettled_tail;
       if (settled_set && !settled && delivery->remote.settled)
         return pn_do_error(transport, "amqp:invalid-field", "invalid transition from settled to unsettled");
-      if (id_present && id != delivery->state.id)
-        return pn_do_error(transport, "amqp:invalid-field", "invalid delivery-id for a continuation transfer");
     } else {
-      // Application has already settled.  Delivery is no more.
-      // Ignore content and look for transition to a new delivery.
-      if (!id_present || id == link->more_id) {
-        // Still old delivery.
-        if (!more || aborted)
-          link->more_pending = false;
-      } else {
-        // New id.
-        new_delivery = true;
-        link->more_pending = false;
-      }
+      // Application has already settled the partial delivery.  Delivery is no
+      // more: discard the remaining frames, clearing more_pending on the last.
+      if (!more || aborted)
+        pni_link_more_end(link);
     }
   } else {
-    new_delivery = true;
-  }
+    // First transfer of a new delivery.
+    if (!id_present) {
+      return pn_do_error(transport, "amqp:invalid-field", "delivery-id required on initial delivery transfer");
+    }
+    if (!tag_present) {
+      return pn_do_error(transport, "amqp:invalid-field", "delivery-tag required on initial delivery transfer");
+    }
+    if (!format_present) {
+      return pn_do_error(transport, "amqp:invalid-field", "message-format required on initial delivery transfer");
+    }
 
-  if (new_delivery) {
-    assert(!link->more_pending);
-    assert(delivery == NULL);
     pn_delivery_map_t *incoming = &ssn->state.incoming;
 
     if (!ssn->state.incoming_init) {
-      if (!id_present) {
-        return pn_do_error(transport, "amqp:invalid-field", "delivery-id required on initial transfer of session");
-      }
       incoming->next = id;
       ssn->state.incoming_init = true;
       ssn->incoming_deliveries++;
@@ -1432,18 +1468,21 @@ int pn_do_transfer(pn_transport_t *transport, uint8_t frame_type, uint16_t chann
                          "connection delivery buffer limit exceeded: %zu bytes buffered, limit %zu",
                          transport->buffered_delivery_bytes, transport->max_buffered_delivery_bytes);
     }
-    if (more && !link->more_pending && !id_present) {
-      return pn_do_error(transport, "amqp:invalid-field", "delivery-id required for transfer");
-    }
     int err = pn_buffer_append(delivery->bytes, payload.start, payload.size);
     if (err) {
       return pn_do_error(transport, "amqp:resource-limit-exceeded", "out of memory buffering incoming delivery");
     }
     transport->buffered_delivery_bytes += payload.size;
-    if (more && !link->more_pending) {
-      // First frame of a multi-frame transfer. Remember at link level.
-      link->more_pending = true;
-      link->more_id = id;
+    if (more) {
+      if (!link->more_pending) {
+        // First frame of a multi-frame transfer. Remember at link level.
+        // Only reachable via the new delivery path, so both fields are present.
+        assert(id_present && tag_present);
+        pni_link_more_begin(link, id, tag, format);
+      }
+    } else {
+      // Last frame of the delivery: the link is no longer mid-assembly.
+      pni_link_more_end(link);
     }
     delivery->done = !more;
 
@@ -1458,7 +1497,7 @@ int pn_do_transfer(pn_transport_t *transport, uint8_t frame_type, uint16_t chann
       delivery->remote.settled = true;
       delivery->done = true;
       delivery->updated = true;
-      link->more_pending = false;
+      pni_link_more_end(link);
       pn_work_update(transport->connection, delivery);
     }
     pn_collector_put_object(transport->connection->collector, delivery, PN_DELIVERY);
diff --git a/c/tools/codec-generator/specs.json b/c/tools/codec-generator/specs.json
index 535fdf3b6..8eb4e73f0 100644
--- a/c/tools/codec-generator/specs.json
+++ b/c/tools/codec-generator/specs.json
@@ -7,7 +7,7 @@
     ["DL[SIoBB?DL[SIsIoR?sRnMM]?DL[SIsIoRM]nnILnnR]", "attach"],
     ["DL[SIoBB?DL[SIsIoR?sRnRR]DL[R]nnI]", "attach_coordinator"],
     ["DL[?IIII?I?I?In?o]", "flow"],
-    ["DL[IIzI?o?ond?o?o?o]", "transfer"],
+    ["DL[IIZI?o?ond?o?o?o]", "transfer"],
     ["DL[oI?I?o?DL[]]", "disposition_batch"],
     ["DL[oIn?od]", "disposition"],
     ["DL[I?oc]", "detach"],
@@ -31,7 +31,7 @@
     ["D.[.....D..D.[R]...]", "attach_coordinator_caps"],
     ["D.[.....D..DL....]", "attach_target_type"],
     ["D.[?IIII?I?II.o]", "flow"],
-    ["D.[I?Iz.?oo.D?LRooo]", "transfer"],
+    ["D.[I?I?Z?I?oo.D?LRooo]", "transfer"],
     ["D.[oI?IoD?LR]", "disposition"],
     ["[?I?L]", "disposition_received"],
     ["[D.[sSR]]", "disposition_rejected"],
diff --git a/python/tests/proton_tests/engine.py b/python/tests/proton_tests/engine.py
index 7fa8aab7c..a7fccee91 100644
--- a/python/tests/proton_tests/engine.py
+++ b/python/tests/proton_tests/engine.py
@@ -1066,6 +1066,131 @@ class TransferTest(Test):
         binary = self.rcv.recv(self.rcv.current.pending)
         assert binary == msg
 
+    def test_multiframe_settled_partial(self):
+        """
+        Settle a delivery while it is still assembling.  The remaining frames
+        of the abandoned delivery must be discarded, and the next delivery on
+        the link must still be received normally.
+        """
+        self.rcv.flow(2)
+        self.snd.delivery("tag1")
+        n = self.snd.send(b"this is a test")
+        assert n == 14
+
+        self.pump()
+
+        d = self.rcv.current
+        assert d.partial
+        # Settle the partial delivery: the receiver abandons it mid-assembly.
+        d.settle()
+
+        # Remaining frames of the abandoned delivery are discarded.
+        n = self.snd.send(b"this is more.  Error if not discarded.")
+        assert n == 38
+        self.pump()
+        assert self.snd.advance()
+        self.pump()
+
+        # A new delivery on the same link is received normally.
+        self.snd.delivery("tag2")
+        msg = b"second message"
+        n = self.snd.send(msg)
+        assert n == len(msg)
+        assert self.snd.advance()
+
+        self.pump()
+
+        d = self.rcv.current
+        assert d, "new delivery was discarded"
+        assert d.tag == "tag2", repr(d.tag)
+        assert not d.partial
+        assert self.rcv.recv(1024) == msg
+
+    def _patch_frame(self, frame, old, new):
+        """
+        Substitute bytes in a single AMQP frame, fixing up the frame size and
+        the performative list size when the length changes.
+        """
+        assert old in frame, repr(frame)
+        assert int.from_bytes(frame[0:4], "big") == len(frame), repr(frame)
+        patched = frame.replace(old, new, 1)
+        delta = len(new) - len(old)
+        if delta:
+            patched = (len(frame) + delta).to_bytes(4, "big") + patched[4:]
+            # performative is \x00S\x14 followed by a list8: \xc0 <size> <count>
+            i = patched.index(b"\x00S\x14\xc0") + 4
+            patched = patched[:i] + bytes([patched[i] + delta]) + patched[i + 1:]
+        return patched
+
+    def _corrupt_continuation(self, old, new):
+        """
+        Start a multiframe delivery, then hand the receiver a continuation
+        frame in which one of the fields that must match the first transfer
+        has been altered on the wire.  Returns the receiving transport's
+        condition.
+        """
+        self.rcv.flow(1)
+        self.snd.delivery("tag1")
+        self.snd.send(b"this is a test")
+
+        self.pump()
+        assert self.rcv.current.partial
+
+        # Take the continuation frame off the sender instead of pumping it.
+        self.snd.send(b"this is more")
+        t1 = self.snd.transport
+        frame = t1.peek(4096)
+        t1.pop(len(frame))
+        self.rcv.transport.push(self._patch_frame(frame, old, new))
+        return self.rcv.transport.condition
+
+    def _corrupt_initial(self, old, new):
+        """
+        Alter the first transfer of a delivery on the wire.  Returns the
+        receiving transport's condition.
+        """
+        self.rcv.flow(1)
+        self.pump()  # get the credit to the sender
+        t1 = self.snd.transport
+        t1.pop(len(t1.peek(4096)))  # drain any pending output
+
+        self.snd.delivery("tag1")
+        self.snd.send(b"this is a test")
+        assert self.snd.advance()
+
+        frame = t1.peek(4096)
+        t1.pop(len(frame))
+        self.rcv.transport.push(self._patch_frame(frame, old, new))
+        return self.rcv.transport.condition
+
+    def test_initial_transfer_no_format(self):
+        # Drop message-format (uint0, 0x43) from the first transfer.  The spec
+        # allows it to be omitted only on continuation transfers.
+        cond = self._corrupt_initial(b"\xa0\x04tag1\x43", b"\xa0\x04tag1\x40")
+        assert cond is not None, "initial transfer without message-format accepted"
+        assert cond.name == "amqp:invalid-field", cond
+        assert cond.description == "message-format required on initial delivery transfer", cond
+
+    def test_multiframe_continuation_tag_mismatch(self):
+        cond = self._corrupt_continuation(b"\xa0\x04tag1", b"\xa0\x04tagZ")
+        assert cond is not None, "mismatched continuation delivery-tag accepted"
+        assert cond.name == "amqp:invalid-field", cond
+        assert cond.description == "invalid delivery-tag for a continuation transfer", cond
+
+    def test_multiframe_continuation_id_mismatch(self):
+        # delivery-id 0 is encoded as uint0 (0x43); make it 1 (smalluint 0x52 0x01).
+        cond = self._corrupt_continuation(b"\x43\xa0\x04tag1", b"\x52\x01\xa0\x04tag1")
+        assert cond is not None, "mismatched continuation delivery-id accepted"
+        assert cond.name == "amqp:invalid-field", cond
+        assert cond.description == "invalid delivery-id for a continuation transfer", cond
+
+    def test_multiframe_continuation_format_mismatch(self):
+        # message-format follows the delivery-tag, 0 encoded as uint0 (0x43).
+        cond = self._corrupt_continuation(b"\xa0\x04tag1\x43", b"\xa0\x04tag1\x52\x01")
+        assert cond is not None, "mismatched continuation message-format accepted"
+        assert cond.name == "amqp:invalid-field", cond
+        assert cond.description == "invalid message-format for a continuation transfer", cond
+
     def test_disposition(self):
         self.rcv.flow(1)
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org