tools: fix -Wunused-result in linux.c
PATCH 1/1 tools: fix -Wunused-result in linux.c
Fix the following -Wunused-result warnings on posix_memalign() return values and add error handling.
./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
100 | posix_memalign(&p, cachep->align, cachep->size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:
../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
198 | posix_memalign(&p[i], cachep->align,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199 | cachep->size);
| ~~~~~~~~~~~~~--- tools/testing/shared/linux.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tools/testing/shared/linux.c +9 -5
@@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, p = node; }else {pthread_mutex_unlock (&cachep->lock); -if (cachep->align) -posix_memalign (&p, cachep->align, cachep->size); -else +if (cachep->align) { +if (posix_memalign (&p, cachep->align, cachep->size) <0 ) +return NULL ; + }else { p =malloc (cachep->size); + } +if (cachep->ctor) cachep->ctor (p);else if (gfp & __GFP_ZERO)
@@ -195,8 +198,9 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size, }if (cachep->align) { -posix_memalign (&p[i], cachep->align, - cachep->size); +if (posix_memalign (&p[i], cachep->align, + cachep->size) <0 ) +break ; }else { p[i] =malloc (cachep->size);if (!p[i])
Signature · 1 line
2.40.1
Quoted 13 lines · Shuah Khan
Fix the following -Wunused-result warnings on
posix_memalign()return values and add error handling.
./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]100 | posix_memalign(&p, cachep->align, cachep->size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../shared/linux.c: In function ‘kmem_cache_alloc_bulk’: ../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 198 | posix_memalign(&p[i], cachep->align, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 199 | cachep->size); | ~~~~~~~~~~~~~
Quoted 1 line
Signed-off-by Shuah Khan <[email protected]>
Looks good to me!
Quoted 40 lines
--- tools/testing/shared/linux.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)tools/testing/shared/linux.c +9
-6@@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, p = node; }else {pthread_mutex_unlock (&cachep->lock); -if (cachep->align) -posix_memalign (&p, cachep->align, cachep->size); -else +if (cachep->align) { +if (posix_memalign (&p, cachep->align, cachep->size) <0 ) +return NULL ; + }else { p =malloc (cachep->size); + } +if (cachep->ctor) cachep->ctor (p);else if (gfp & __GFP_ZERO)@@ -195,8 +198,9 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size, }if (cachep->align) { -posix_memalign (&p[i], cachep->align, - cachep->size); +if (posix_memalign (&p[i], cachep->align, + cachep->size) <0 ) +break ; }else { p[i] =malloc (cachep->size);if (!p[i]) --2.40.1
Quoted 13 lines · Shuah Khan
Fix the following -Wunused-result warnings on
posix_memalign()return values and add error handling.
./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result]100 | posix_memalign(&p, cachep->align, cachep->size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../shared/linux.c: In function ‘kmem_cache_alloc_bulk’: ../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 198 | posix_memalign(&p[i], cachep->align, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 199 | cachep->size); | ~~~~~~~~~~~~~
Quoted 1 line
Signed-off-by Shuah Khan <[email protected]>
Quoted 40 lines
--- tools/testing/shared/linux.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)tools/testing/shared/linux.c +9
-5@@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, p = node; }else {pthread_mutex_unlock (&cachep->lock); -if (cachep->align) -posix_memalign (&p, cachep->align, cachep->size); -else +if (cachep->align) { +if (posix_memalign (&p, cachep->align, cachep->size) <0 ) +return NULL ; + }else { p =malloc (cachep->size); + } +if (cachep->ctor) cachep->ctor (p);else if (gfp & __GFP_ZERO)@@ -195,8 +198,9 @@ int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size, }if (cachep->align) { -posix_memalign (&p[i], cachep->align, - cachep->size); +if (posix_memalign (&p[i], cachep->align, + cachep->size) <0 ) +break ; }else { p[i] =malloc (cachep->size);if (!p[i])Signature · 1 line
2.40.1
From: Liam R. Howlett
Quoted 36 lines
Sent: 15 October 2024 02:11
Quoted 4 lines · Shuah Khan
Fix the following -Wunused-result warnings on
posix_memalign()return values and add error handling.
./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute‘warn_unused_result’ [-Wunused-result]
Quoted 4 lines
100 | posix_memalign(&p, cachep->align, cachep->size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute‘warn_unused_result’ [-Wunused-result]
Quoted 6 lines
198 | posix_memalign(&p[i], cachep->align, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 199 | cachep->size); | ~~~~~~~~~~~~~Signed-off-by Shuah Khan <[email protected]>Reviewed-by Liam R. Howlett <[email protected]>Quoted 14 lines
--- tools/testing/shared/linux.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)tools/testing/shared/linux.c +0
-2@@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, p = node; }else {pthread_mutex_unlock (&cachep->lock); -if (cachep->align) -posix_memalign (&p, cachep->align, cachep->size);
Quoted 8 lines
tools/testing/shared/linux.c void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, · line 105
Quoted 8 lines
tools/testing/shared/linux.c +6
-1-else +if (cachep->align) { +if (posix_memalign (&p, cachep->align, cachep->size) <0 ) +return NULL ; + }else { p =malloc (cachep->size); + } +
You really ought to be checking malloc() as well.
Perhaps:
if (...) {
if (posix_memalign(...) < 0)
p = NULL;
} else {
p = malloc(...);
}
if (!p)
return NULL;Or just use a hack to get the compiler to STFU :-)
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 57 lines · David Laight
From: Liam R. Howlett
Quoted 36 lines
Sent: 15 October 2024 02:11
Quoted 4 lines · Shuah Khan
Fix the following -Wunused-result warnings on
posix_memalign()return values and add error handling.
./shared/linux.c:100:25: warning: ignoring return value of ‘posix_memalign’ declared with attribute‘warn_unused_result’ [-Wunused-result]
Quoted 4 lines
100 | posix_memalign(&p, cachep->align, cachep->size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../shared/linux.c: In function ‘kmem_cache_alloc_bulk’:../shared/linux.c:198:33: warning: ignoring return value of ‘posix_memalign’ declared with attribute‘warn_unused_result’ [-Wunused-result]
Quoted 6 lines
198 | posix_memalign(&p[i], cachep->align, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 199 | cachep->size); | ~~~~~~~~~~~~~Signed-off-by Shuah Khan <[email protected]>Reviewed-by Liam R. Howlett <[email protected]>Quoted 14 lines
--- tools/testing/shared/linux.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)tools/testing/shared/linux.c +0
-2@@ -96,10 +96,13 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, p = node; }else {pthread_mutex_unlock (&cachep->lock); -if (cachep->align) -posix_memalign (&p, cachep->align, cachep->size);Quoted 8 lines
tools/testing/shared/linux.c void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, · line 105
Quoted 8 lines
tools/testing/shared/linux.c +6
-1-else +if (cachep->align) { +if (posix_memalign (&p, cachep->align, cachep->size) <0 ) +return NULL ; + }else { p =malloc (cachep->size); + } +You really ought to be checking
malloc()as well.
Perhaps:if (...) { if (posix_memalign(...) < 0) p = NULL; } else { p = malloc(...); } if (!p) return NULL;
Quoted 1 line
Or just use a hack to get the compiler to STFU :-)
Yes you are right. I will spin another version to cover the malloc and I just noticed another posix_memalign() with -Wunused-result that compiler didn't flag in kmem_cache_alloc_bulk() to fix as well in the next version.
thanks,
-- Shuah