diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buf_text.c | 14 | ||||
-rw-r--r-- | src/buf_text.h | 5 |
2 files changed, 12 insertions, 7 deletions
diff --git a/src/buf_text.c b/src/buf_text.c index 631feb3f8..8d2b141b2 100644 --- a/src/buf_text.c +++ b/src/buf_text.c @@ -123,9 +123,13 @@ int git_buf_text_lf_to_crlf(git_buf *tgt, const git_buf *src) for (; next; scan = next + 1, next = memchr(scan, '\n', end - scan)) { size_t copylen = next - scan; - /* don't convert existing \r\n to \r\r\n */ - size_t extralen = (next > start && next[-1] == '\r') ? 1 : 2; - size_t needsize = tgt->size + copylen + extralen + 1; + size_t needsize = tgt->size + copylen + 2 + 1; + + /* if we find mixed line endings, bail */ + if (next > start && next[-1] == '\r') { + git_buf_free(tgt); + return GIT_PASSTHROUGH; + } if (tgt->asize < needsize && git_buf_grow(tgt, needsize) < 0) return -1; @@ -134,8 +138,8 @@ int git_buf_text_lf_to_crlf(git_buf *tgt, const git_buf *src) memcpy(tgt->ptr + tgt->size, scan, copylen); tgt->size += copylen; } - if (extralen == 2) - tgt->ptr[tgt->size++] = '\r'; + + tgt->ptr[tgt->size++] = '\r'; tgt->ptr[tgt->size++] = '\n'; } diff --git a/src/buf_text.h b/src/buf_text.h index 3ac9d1443..e753a0244 100644 --- a/src/buf_text.h +++ b/src/buf_text.h @@ -56,9 +56,10 @@ GIT_INLINE(int) git_buf_text_puts_escape_regex(git_buf *buf, const char *string) extern void git_buf_text_unescape(git_buf *buf); /** - * Replace all \r\n with \n. Does not modify \r without trailing \n. + * Replace all \r\n with \n. * - * @return 0 on success, -1 on memory error + * @return 0 on success, -1 on memory error, GIT_PASSTHROUGH if the + * source buffer has mixed line endings. */ extern int git_buf_text_crlf_to_lf(git_buf *tgt, const git_buf *src); |