Skip to content

Conversation

@smititelu
Copy link
Contributor

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.

@redboltz
Copy link
Contributor

redboltz commented Jul 7, 2016

@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.

@smititelu
Copy link
Contributor Author

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);
Copy link
Contributor

@iphydf iphydf Jul 7, 2016

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.

Copy link
Contributor Author

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?!) .

Copy link
Contributor Author

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.

Copy link
Contributor

@iphydf iphydf Jul 7, 2016

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.

Copy link
Contributor

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.

Copy link
Contributor Author

@smititelu smititelu Jul 7, 2016

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.
@redboltz redboltz merged commit 4fa661a into msgpack:master Jul 16, 2016
@redboltz
Copy link
Contributor

merged.

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.

3 participants