]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
mmc: mmc_test: Fix __counted_by handling after kzalloc_flex() conversion
authorLad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Tue, 2 Jun 2026 20:13:44 +0000 (21:13 +0100)
committerUlf Hansson <ulfh@kernel.org>
Mon, 6 Jul 2026 13:44:16 +0000 (15:44 +0200)
commitc35fac6481a1dbb2d114dd337b54a40799437546
tree400df5279aab33e9a166e9dc2a9e552e8dcf74f8
parent521f39ca93cc43ce1b3eae8d44201f8f55dd9151
mmc: mmc_test: Fix __counted_by handling after kzalloc_flex() conversion

Fix logic issues introduced by the kzalloc_flex() conversion in
mmc_test_alloc_mem() due to interaction with the __counted_by
annotation on the flexible array.

Bounds-checking sanitizers rely on the counter field reflecting the
allocated array size before any array access occurs. However, use
mem->cnt both as the allocation size and as the runtime insertion
index, causing incorrect indexing and potentially invalid bounds
tracking.

Initialize mem->cnt to the maximum allocated number of segments
immediately after kzalloc_flex(), then use a separate local index
variable to track successfully allocated entries. Update mem->cnt to
the actual number of initialized elements before returning or entering
the cleanup path.

Also rewrite mmc_test_free_mem() to use a forward for-loop, improving
readability and ensuring only initialized entries are freed.

Fixes: c3126dccfd7b ("mmc: mmc_test: use kzalloc_flex")
Signed-off-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: stable@vger.kernel.org
Signed-off-by: Ulf Hansson <ulfh@kernel.org>
drivers/mmc/core/mmc_test.c