]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
of: unittest: Fix memory leak in unittest_data_add()
authorZilin Guan <zilin@seu.edu.cn>
Wed, 31 Dec 2025 11:49:15 +0000 (11:49 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 17 Jan 2026 15:35:18 +0000 (16:35 +0100)
commitf09b0f705bd7197863b90256ef533a6414d1db2c
tree12f6d26a9e79fc9b3906fbae218050837a52c9ca
parentc8385851a5435f4006281828d428e5d0b0bbf8af
of: unittest: Fix memory leak in unittest_data_add()

[ Upstream commit 235a1eb8d2dcc49a6cf0a5ee1aa85544a5d0054b ]

In unittest_data_add(), if of_resolve_phandles() fails, the allocated
unittest_data is not freed, leading to a memory leak.

Fix this by using scope-based cleanup helper __free(kfree) for automatic
resource cleanup. This ensures unittest_data is automatically freed when
it goes out of scope in error paths.

For the success path, use retain_and_null_ptr() to transfer ownership
of the memory to the device tree and prevent double freeing.

Fixes: 2eb46da2a760 ("of/selftest: Use the resolver to fixup phandles")
Suggested-by: Rob Herring <robh@kernel.org>
Co-developed-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Jianhao Xu <jianhao.xu@seu.edu.cn>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
Link: https://patch.msgid.link/20251231114915.234638-1-zilin@seu.edu.cn
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/of/unittest.c