@matheusbsbs / matheus-bagliano / commits / 90d16ec032
Fork 0

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>

Pierre Habouzit committed Oct 5, 2007 at 10:11 UTC 90d16ec032b20f9f1146f3aceca12165aba3b6d6
1 file changed +10 -7
convert.c
+10 -7
@@ -110,7 +110,9 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
110 return 0;
111 }
112
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);
116 dst = buf->buf;
117 if (action == CRLF_GUESS) {
118 /*
@@ -281,20 +283,19 @@ static int apply_filter(const char *path, const char *src, size_t len,
283 ret = 0;
284 }
285 if (close(pipe_feed[0])) {
284 - ret = error("read from external filter %s failed", cmd);
286 + error("read from external filter %s failed", cmd);
287 ret = 0;
288 }
289 status = finish_command(&child_process);
290 if (status) {
289 - ret = error("external filter %s failed %d", cmd, -status);
291 + error("external filter %s failed %d", cmd, -status);
292 ret = 0;
293 }
294
295 if (ret) {
294 - *dst = nbuf;
295 - } else {
296 - strbuf_release(&nbuf);
296 + strbuf_swap(dst, &nbuf);
297 }
298 + strbuf_release(&nbuf);
299 return ret;
300 }
301
@@ -422,7 +423,9 @@ static int ident_to_git(const char *path, const char *src, size_t len,
423 if (!ident || !count_ident(src, len))
424 return 0;
425
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);
429 dst = buf->buf;
430 for (;;) {
431 dollar = memchr(src, '$', len);