1
0
mirror of https://github.com/git/git.git synced 2024-11-08 15:19:28 +01:00

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>
This commit is contained in:
Patrick Steinhardt 2024-05-27 13:46:01 +02:00 committed by Junio C Hamano
parent 97613b9cb9
commit 94e2aa555e
2 changed files with 5 additions and 1 deletions

@ -691,8 +691,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
{
struct strbuf line = STRBUF_INIT;
if (strbuf_getwholeline(&line, fp, term))
if (strbuf_getwholeline(&line, fp, term)) {
strbuf_release(&line);
return EOF;
}
strbuf_addbuf(sb, &line);
strbuf_release(&line);
return 0;

@ -4,6 +4,8 @@
#
test_description='Test git update-ref and basic ref logging'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
Z=$ZERO_OID