strbuf: fix leak when `appendwholeline()` fails with EOF
In `strbuf_appendwholeline()` we call `strbuf_getwholeline()` with a temporary buffer. In case the call returns an error we indicate this by returning EOF, but never release the temporary buffer. This can cause a leak though because `strbuf_getwholeline()` calls getline(3). Quoting its documentation: If *lineptr was set to NULL before the call, then the buffer should be freed by the user program even on failure. Consequently, the temporary buffer may hold allocated memory even when the call to `strbuf_getwholeline()` fails. Fix this by releasing the temporary buffer on error. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
May 27, 2024 at 13:46 UTC
94e2aa555e7dab4f5296a8dcd8605d751e02b12d
2 files changed
+5
-1
strbuf.c
+3
-1
@@ -691,8 +691,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
691
int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
692
{
693
struct strbuf line = STRBUF_INIT;
694
- if (strbuf_getwholeline(&line, fp, term))
694
+ if (strbuf_getwholeline(&line, fp, term)) {
695
+ strbuf_release(&line);
696
return EOF;
697
+ }
698
strbuf_addbuf(sb, &line);
699
strbuf_release(&line);
700
return 0;
t/t1400-update-ref.sh
+2
@@ -4,6 +4,8 @@
4
#
5
6
test_description='Test git update-ref and basic ref logging'
7
+
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
11
Z=$ZERO_OID