From 85a0e28b80e42a52247e16478b5f75475b00e56b Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Tue, 14 Aug 2012 10:50:58 -0700 Subject: 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. --- src/message.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src') 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; } -- cgit v1.2.1