-
Notifications
You must be signed in to change notification settings - Fork 918
Add msgpack_object_print_buffer() function #500
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
Conversation
|
@smititelu , thank you sending the PR. It looks good to me. I will merge the PR with #497, and then merge to master with a small modification. |
|
Thanks for feedback. |
src/objectc.c
Outdated
| ret = snprintf(aux_buffer, aux_buffer_size, "\""); | ||
| aux_buffer = aux_buffer + ret; | ||
| aux_buffer_size = aux_buffer_size - ret; | ||
| ret = snprintf(aux_buffer, aux_buffer_size, "%.*s", o.via.bin.size, o.via.bin.ptr); |
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.
This does not work if bin contains NULs.
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.
Can you give some details about what "not work" means (i.e. test input/output, error?!) .
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 also the environment you are testing on.
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.
I haven't tested this, but *printf functions format C strings, not binary strings. E.g. printf("%.*s", 200, "\0hello"); will not output anything, while printf("%.*s", 200, "hello"); will output "hello". In your code, the .* format qualifier is an upper bound of the characters written, not an exact number.
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.
See also the C standard draft, §7.19.6.1, in the description of the %s conversion specifier: "If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character".
This also means your string printing works because although it has no null character, the precision is exactly the array 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.
Thanks for clarifications. I overlooked the "\0" what might be encountered at any point in a binary string.
I will use memcpy for this scenario, while taking care of the lengths.
In order to print the msgpack object in a memory buffer.
|
merged. |
In order to print the msgpack object in a memory buffer.
Similar to msgpack_object_print(), but prints in a given, allocated buffer.
Basically tested it with ./user_buffer_unpack.