"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Kyle Ackerman <kackerman0102@gmail.com>
Subject:
Diff memory leak
To:
gameoftrees@openbsd.org
Date:
Mon, 27 May 2024 23:58:02 -0500

Download raw body.

Thread
Hello all,

There is a memory leak within `got diff` that leaks line metadata.

******** Start dump got *******
M=8 I=1 F=1 U=1 J=2 R=0 X=0 C=0x449af07a cache=0 G=4096
Leak report:
                 f     sum      #    avg
     0xd551a917735     512      1    512 addr2line -e /home/kyle/bin/got 0x4e735
     0xd57f17f70b0      64      1     64 addr2line -e /usr/lib/libc.so.100.1 0xa40b0
     0xd57f182c4d1     112      7     16 addr2line -e /usr/lib/libc.so.100.1 0xd94d1
     0xd57f18158d2    1024      1   1024 addr2line -e /usr/lib/libc.so.100.1 0xc28d2
     0xd57f17aec53   69632      1  69632 addr2line -e /usr/lib/libc.so.100.1 0x5bc53

******** End dump got *******


Here is the diff to patch the memory leak

diff /home/kyle/src/got
commit - c89c70b628c1825024e333214392011409d71184
path + /home/kyle/src/got
blob - 245df76cba6ccd1d6c155ecbb3632f386db7f3e1
file + lib/diff.c
--- lib/diff.c
+++ lib/diff.c
@@ -1282,6 +1282,8 @@ diff_objects_as_trees(struct got_diff_line **lines, si
 	if (want_linemeta) {
 		*lines = arg.lines; /* was likely re-allocated */
 		*nlines = arg.nlines;
+	} else {
+		free(arg.lines);
 	}
 done:
 	if (tree1)

My logic here is that this function need to free the memory it reallocs
if it doesn't give the callee function access to (free) the memory.
Alternatively, I can refactor so that the memory, NULL or not, will get
passed back up and have the caller free it.  Thoughts?