Skip to content

Commit 94e2aa5

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 97613b9 commit 94e2aa5

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

strbuf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
691691
int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
692692
{
693693
struct strbuf line = STRBUF_INIT;
694-
if (strbuf_getwholeline(&line, fp, term))
694+
if (strbuf_getwholeline(&line, fp, term)) {
695+
strbuf_release(&line);
695696
return EOF;
697+
}
696698
strbuf_addbuf(sb, &line);
697699
strbuf_release(&line);
698700
return 0;

t/t1400-update-ref.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55

66
test_description='Test git update-ref and basic ref logging'
7+
8+
TEST_PASSES_SANITIZE_LEAK=true
79
. ./test-lib.sh
810

911
Z=$ZERO_OID

0 commit comments

Comments
 (0)