]> git.ipfire.org Git - thirdparty/git.git/commit - submodule-config.c
submodule-config: avoid memory leak
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Wed, 15 Jun 2022 23:35:39 +0000 (23:35 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Jun 2022 20:22:03 +0000 (13:22 -0700)
commitf53559227ccb600f4fdd1bfe08e1164a5aed60b5
tree09c614f7823f06f921e5eba4035f26eff86b6fa2
parent5a09991e32f3487702bd032703bacba1c4c46612
submodule-config: avoid memory leak

In 961b130d20c9 (branch: add --recurse-submodules option for branch
creation, 2022-01-28), a funny pattern was introduced where first some
struct is `xmalloc()`ed, then we resize an array whose element type is
the same struct, and then the first struct's contents are copied into
the last element of that array.

Crucially, the `xmalloc()`ed memory never gets released.

Let's avoid that memory leak and that memory allocation dance altogether
by first reallocating the array, then using a pointer to the last array
element to go forward.

Reported by Coverity.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
submodule-config.c