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

compat: nn_reallocmsg is incorrect #1701

Closed
Snarpix opened this issue Nov 10, 2023 · 1 comment
Closed

compat: nn_reallocmsg is incorrect #1701

Snarpix opened this issue Nov 10, 2023 · 1 comment

Comments

@Snarpix
Copy link

Snarpix commented Nov 10, 2023

nng/src/compat/nanomsg/nn.c

Lines 366 to 379 in a54820f

// This counts on message bodies being aligned sensibly.
msg = *(nng_msg **) (((char *) ptr) - sizeof(msg));
// We need to realloc the requested len, plus size for our header.
if ((rv = nng_msg_realloc(msg, len + sizeof(msg))) != 0) {
// We don't free the old message. Code is free to cope
// as it sees fit.
nn_seterror(rv);
return (NULL);
}
// Stash the msg header pointer
*(nng_msg **) (nng_msg_body(msg)) = msg;
nng_msg_trim(msg, sizeof(msg));
return (nng_msg_body(msg));

In this code nng_msg_realloc copies message without trimmed nng_msg* in the beginning.
Then we overwrite first 8 message bytes with nng_msg*, and trim them, so on realloc we are actually removing first 8 bytes of message.

@gdamore
Copy link
Contributor

gdamore commented Nov 26, 2023

Upon reading, I agree. There is no test for this code, and I don't use it. I should fix this, and a test case for it.

@gdamore gdamore changed the title Probably a bug in compat interface nn_reallocmsg compat: nn_reallocmsg is incorrect Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants