-
Notifications
You must be signed in to change notification settings - Fork 880
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
refactor example/c/lib_buffer_unpack.c #344
Conversation
The example has some duplicated code that somewhat distracts from the main processing loop. I think placing this into a separate function improves readability of the code.
CI tests indicate that the previous commit made the "buf" variable obsolete (unused), potentially causing warnings. |
{ | ||
// make sure there's enough room, or expand the unpacker accordingly | ||
if (msgpack_unpacker_buffer_capacity(unpacker) < request_size) { | ||
assert(msgpack_unpacker_reserve_buffer(unpacker, request_size)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert macro can be disabled and this line could completely be removed. So, this has to be like the original code:
bool expanded = msgpack_unpacker_reserve_buffer(unpacker, request_size);
assert(expanded);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and it generates unused variable error (-Werror) :) umm. I'll talk to @redboltz about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msgpack_unpacker_reserve_buffer(unpacker, request_size)
|| (assert(0 && "msgpack_unpacker_reserve_buffer failed"), 0);
@redboltz Is this acceptable to you? Do you have a better idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh... But that's a problem that also affects the original code, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since msgpack-c already has a MSGPACK_UNUSED
macro in https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/util.h#L21, it might be a saner approch to simply mark the local variable as "potentially unused":
bool expanded = msgpack_unpacker_reserve_buffer(unpacker, request_size);
assert(expanded);
MSGPACK_UNUSED(expanded);
EDIT:
A "clean" way to solve this is command-query separation. In this particular case that's easy to achieve:
if (msgpack_unpacker_buffer_capacity(unpacker) < request_size) {
msgpack_unpacker_reserve_buffer(unpacker, request_size);
assert(msgpack_unpacker_buffer_capacity(unpacker) >= request_size);
}
Your code looks much nicer than the previous one. Thanks! I'll merge this after fixing the problem above. |
Thanks! :) And nice catch with the assert() evaluation. We might even further reduce the reading loop to a single call of receiver_to_unpacker(), like this;
Regards, NiteHawk |
I think it seems better. I'll apply the change when I merge this PR. |
There's a small problem remaining if assertions are disabled (with -DNDEBUG).
I've incorporated the suggested changes into another commit. The only remaining issue that needs to be addressed is that assert() logic. |
Thank you! |
One more thing: I'm wondering if unpack() (in lib_buffer_unpack.c) is missing a |
@n1tehawk, thank you for pointing out the problem. You are right. I checked the executable using valgrind, then resource leaks are detected. |
Hi @redboltz! I'll prepare another commit then - which will contain the free() call, and also addresses the assert() stuff discussed before. |
refactor example/c/lib_buffer_unpack.c
Merged. Thanks you @n1tehawk :) |
The example has some duplicated code that somewhat distracts from
the main processing loop. I think placing this into a separate
function improves readability of the code.