Skip to content

Commit 90d16ec

Browse files
MadCoderspearce
authored andcommitted
Fix in-place editing functions in convert.c
* crlf_to_git and ident_to_git: Don't grow the buffer if there is enough space in the first place. As a side effect, when the editing is done "in place", we don't grow, so the buffer pointer doesn't changes, and `src' isn't invalidated anymore. Thanks to Bernt Hansen for the bug report. * apply_filter: Fix memory leak due to fake in-place editing that didn't collected the old buffer when the filter succeeds. Also a cosmetic fix. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent 425b78e commit 90d16ec

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

convert.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
110110
return 0;
111111
}
112112

113-
strbuf_grow(buf, len);
113+
/* only grow if not in place */
114+
if (strbuf_avail(buf) + buf->len < len)
115+
strbuf_grow(buf, len - buf->len);
114116
dst = buf->buf;
115117
if (action == CRLF_GUESS) {
116118
/*
@@ -281,20 +283,19 @@ static int apply_filter(const char *path, const char *src, size_t len,
281283
ret = 0;
282284
}
283285
if (close(pipe_feed[0])) {
284-
ret = error("read from external filter %s failed", cmd);
286+
error("read from external filter %s failed", cmd);
285287
ret = 0;
286288
}
287289
status = finish_command(&child_process);
288290
if (status) {
289-
ret = error("external filter %s failed %d", cmd, -status);
291+
error("external filter %s failed %d", cmd, -status);
290292
ret = 0;
291293
}
292294

293295
if (ret) {
294-
*dst = nbuf;
295-
} else {
296-
strbuf_release(&nbuf);
296+
strbuf_swap(dst, &nbuf);
297297
}
298+
strbuf_release(&nbuf);
298299
return ret;
299300
}
300301

@@ -422,7 +423,9 @@ static int ident_to_git(const char *path, const char *src, size_t len,
422423
if (!ident || !count_ident(src, len))
423424
return 0;
424425

425-
strbuf_grow(buf, len);
426+
/* only grow if not in place */
427+
if (strbuf_avail(buf) + buf->len < len)
428+
strbuf_grow(buf, len - buf->len);
426429
dst = buf->buf;
427430
for (;;) {
428431
dollar = memchr(src, '$', len);

0 commit comments

Comments
 (0)