Skip to content

Commit 67b5455

Browse files
committed
PROTON-2913: pn_message_encode could generate an illegal message
If given a message with no message body, pn_message_encode would generate a message with no body which is illegal in AMQP. Would have preferred to generate an error in this case, but this would be a change to a very long standing behaviour.
1 parent b053795 commit 67b5455

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

c/src/core/message.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,11 @@ int pn_message_set_reply_to_group_id(pn_message_t *msg, const char *reply_to_gro
730730

731731
int pn_message_decode(pn_message_t *msg, const char *bytes, size_t size)
732732
{
733-
assert(msg && bytes && size);
733+
assert(msg);
734+
735+
if (!bytes || !size) {
736+
return pn_error_format(msg->error, PN_ARG_ERR, "invalid message bytes");
737+
}
734738

735739
pn_bytes_t msg_bytes = {.size=size, .start=bytes};
736740
pn_bytes_t instructions_bytes = {0, 0};
@@ -852,6 +856,7 @@ int pn_message_encode(pn_message_t *msg, char *bytes, size_t *isize)
852856
if (!pni_switch_to_raw_bytes(scratch, &msg->body_deprecated, &msg->body_raw)) {
853857
return PN_OVERFLOW;
854858
}
859+
855860
size_t remaining = *isize;
856861
size_t total = 0;
857862

@@ -942,6 +947,15 @@ int pn_message_encode(pn_message_t *msg, char *bytes, size_t *isize)
942947
last_size = pn_amqp_encode_bytes_described_type_raw(bytes, remaining, descriptor, msg->body_raw);
943948
if (last_size > remaining) return PN_OVERFLOW;
944949

950+
remaining -= last_size;
951+
bytes += last_size;
952+
total += last_size;
953+
} else {
954+
// AMQP requires a body, so encode a null body if none present
955+
static const char null_byte = PNE_NULL;
956+
last_size = pn_amqp_encode_bytes_described_type_raw(bytes, remaining, AMQP_DESC_AMQP_VALUE, (pn_bytes_t){.size=1, .start=&null_byte});
957+
if (last_size > remaining) return PN_OVERFLOW;
958+
945959
remaining -= last_size;
946960
bytes += last_size;
947961
total += last_size;

python/proton/_message.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ def _pre_encode(self) -> None:
135135
self._check_property_keys()
136136
props.put_object(self.properties)
137137
body.clear()
138-
if self.body is not None:
139-
body.put_object(self.body)
138+
# Message body must be present for a valid AMQP message
139+
# If it is None, encode as an explicit null
140+
body.put_object(self.body)
140141

141142
def _post_decode(self) -> None:
142143
inst = Data(pn_message_instructions(self._msg))

0 commit comments

Comments
 (0)