Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROTON-2785: Fix pn_data_clear not clearing error #431

Merged

Conversation

PatrickTaibel
Copy link
Contributor

We've also hit the bug of the Go library that is described in PROTON-2785. This bug happens when one large message is sent and another message with arbitrary size gets marshaled.

Reason is that the large message does multiple calls with a too small message size to pn_data_encode which leads to the error PN_OVERFLOW to be set in pn_encoder_encode on the data field until a large enough buffer is provided. As the pn_message_t is reused the error hits the second message encoding.

Relevant Go code:

func (mc *MessageCodec) Encode(m Message, buffer []byte) ([]byte, error) {
pn := mc.pnMessage()
m.(*message).put(pn)
encode := func(buf []byte) ([]byte, error) {
len := cLen(buf)
result := C.pn_message_encode(pn, cPtr(buf), &len)
switch {
case result == C.PN_OVERFLOW:
return buf, overflow
case result < 0:
return buf, fmt.Errorf("cannot encode message: %s", PnErrorCode(result))
default:
return buf[:len], nil
}
}
return encodeGrow(buffer, encode)

Relevant C code:

ssize_t pn_encoder_encode(pn_encoder_t *encoder, pn_data_t *src, char *dst, size_t size)
{
encoder->output = dst;
encoder->position = 0;
encoder->size = size;
int err = pni_data_traverse(src, pni_encoder_enter, pni_encoder_exit, encoder);
if (err) return err;
size_t encoded = encoder->position;
if (encoded > size) {
pn_error_format(pn_data_error(src), PN_OVERFLOW, "not enough space to encode");
return PN_OVERFLOW;
}
return (ssize_t)encoded;
}

This PR adds clearing the error field when pn_data_clear is called. According to the docs "A cleared pn_data_t object is equivalent to a newly constructed one.", so that would match this fix.

Other possible way would be to not set those PN_OVERFLOW errors on pn_data_t at all. I don't have enough overview of the internals to know if this makes sense or not. Especially, as there are a few other errors that are set on pn_data_t too which might cause similar issues.

c/src/core/codec.c Outdated Show resolved Hide resolved
@PatrickTaibel PatrickTaibel force-pushed the pr/PROTON-2785_fix_go_multiple_messages branch from 1f9343b to e747419 Compare July 30, 2024 05:39
@astitcher astitcher merged commit 0095a69 into apache:main Jul 30, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants