tools/testing/shared: catch a failed posix_memalign()

posix_memalign() returns zero or a positive error number and never sets
errno, so the checks for a negative return in kmem_cache_alloc_lru() and
kmem_cache_alloc_bulk() is not possible.  A failed aligned allocation
carries on with the pointer unset, into the constructor or the memset().

Test for any non-zero return.

Link: https://lore.kernel.org/20261020033128.1934349-1-liam@infradead.org
Fixes: 8801c35c36 ("tools: fix -Wunused-result in linux.c")
Signed-off-by: Liam R. Howlett (Google) <liam@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Vishal Moola (Fractile) <vishal.moola@gmail.com>
Reviewed-by: SJ Park <sj@kernel.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
This commit is contained in:
Liam R. Howlett (Google) 2026-10-19 23:31:28 -04:00 committed by Andrew Morton
parent 5695abc4e7
commit c9ed137890

View File

@ -83,7 +83,7 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
} else {
pthread_mutex_unlock(&cachep->lock);
if (cachep->align) {
if (posix_memalign(&p, cachep->align, cachep->size) < 0)
if (posix_memalign(&p, cachep->align, cachep->size))
return NULL;
} else {
p = malloc(cachep->size);
@ -191,7 +191,7 @@ bool kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
if (cachep->align) {
if (posix_memalign(&p[i], cachep->align,
cachep->size) < 0)
cachep->size))
break;
} else {
p[i] = malloc(cachep->size);