In the original read_oneliner logic, we duplicated the logic for strbuf_trim_trailing_newline() with one exception: instead of preventing buffer accesses below index 0, it would prevent buffer accesses below index `orig_len`. Although this is correct, it isn't worth having the duplicated logic around. Add a second strbuf to which files are read and run strbuf_trim_trailing_newline() directly on this strbuf then concatenate this strbuf with the argument strbuf at the end of the function. The function's external behaviour is unchanged. Signed-off-by: Denton Liu <liu.denton@xxxxxxxxx> --- sequencer.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/sequencer.c b/sequencer.c index e528225e78..c49fe76fe6 100644 --- a/sequencer.c +++ b/sequencer.c @@ -431,26 +431,28 @@ static int write_message(const void *buf, size_t len, const char *filename, static int read_oneliner(struct strbuf *buf, const char *path, int skip_if_empty) { - int orig_len = buf->len; + int ret = 0; + struct strbuf file_buf = STRBUF_INIT; if (!file_exists(path)) return 0; - if (strbuf_read_file(buf, path, 0) < 0) { + if (strbuf_read_file(&file_buf, path, 0) < 0) { warning_errno(_("could not read '%s'"), path); - return 0; + goto done; } - if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { - if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') - --buf->len; - buf->buf[buf->len] = '\0'; - } + strbuf_trim_trailing_newline(&file_buf); - if (skip_if_empty && buf->len == orig_len) - return 0; + if (skip_if_empty && !file_buf.len) + goto done; - return 1; + strbuf_addbuf(buf, &file_buf); + ret = 1; + +done: + strbuf_release(&file_buf); + return ret; } static struct tree *empty_tree(struct repository *r) -- 2.25.0.114.g5b0ca878e0