diff options
author | Russell Belfer <rb@github.com> | 2012-08-14 10:50:58 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-08-14 10:50:58 -0700 |
commit | 85a0e28b80e42a52247e16478b5f75475b00e56b (patch) | |
tree | 0eaf28379baa12661bb6a2d66a07bc1d67cf2589 /src/message.c | |
parent | fdc637c4e266349b35ac4fb45a4e5aa63c5a78e0 (diff) | |
download | libgit2-85a0e28b80e42a52247e16478b5f75475b00e56b.tar.gz |
Make git_message_prettify return bytes written
If you want to be absolutely safe with git_message_prettify, you
can now pass a NULL pointer for the buffer and get back the number
of bytes that would be copied into the buffer.
This means that an error is a non-negative return code and a
success will be greater than zero from this function.
Diffstat (limited to 'src/message.c')
-rw-r--r-- | src/message.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/message.c b/src/message.c index a5cc26237..e6dedc9fb 100644 --- a/src/message.c +++ b/src/message.c @@ -62,23 +62,25 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c int git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments) { git_buf buf = GIT_BUF_INIT; + ssize_t out_size = -1; - assert(message_out && buffer_size); + if (message_out && buffer_size) + *message_out = '\0'; - *message_out = '\0'; + if (git_message__prettify(&buf, message, strip_comments) < 0) + goto done; - if (git_message__prettify(&buf, message, strip_comments) < 0) { - git_buf_free(&buf); - return -1; - } - - if (buf.size + 1 > buffer_size) { /* +1 for NUL byte */ + if (message_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */ giterr_set(GITERR_INVALID, "Buffer too short to hold the cleaned message"); - return -1; + goto done; } - git_buf_copy_cstr(message_out, buffer_size, &buf); - git_buf_free(&buf); + if (message_out) + git_buf_copy_cstr(message_out, buffer_size, &buf); - return 0; + out_size = buf.size + 1; + +done: + git_buf_free(&buf); + return out_size; } |