alloc: Add more memalign asserts
Subsystems (2)
PATCH 1/1 alloc: Add more memalign asserts
Let's test for size and alignment in memalign to catch invalid input data. Also we need to test for NULL after calling the memalign function of the registered alloc operations.
---
Tested only under s390, tests under other architectures are highly appreciated.
--- lib/alloc.c | 3 +++ 1 file changed, 3 insertions(+)
lib/alloc.c +3 -0
@@ -46,6 +46,7 @@ void *memalign(size_t alignment, size_t size)uintptr_t blkalign;uintptr_t mem; +assert (size && alignment);assert (alloc_ops && alloc_ops->memalign);if (alignment <=sizeof (uintptr_t )) alignment =sizeof (uintptr_t );
@@ -56,6 +57,8 @@ void *memalign(size_t alignment, size_t size) size =ALIGN (size +METADATA_EXTRA , alloc_ops->align_min); p = alloc_ops->memalign (blkalign, size); +assert (p !=NULL ); +/* Leave room for metadata before aligning the result. */ mem = (uintptr_t )p +METADATA_EXTRA ; mem =ALIGN (mem, alignment);
Signature · 1 line
2.20.1
Quoted 28 lines · Janosch Frank
Let's test for size and alignment in memalign to catch invalid input data. Also we need to test for NULL after calling the memalign function of the registered alloc operations.
Signed-off-by Janosch Frank <[email protected]>---
Tested only under s390, tests under other architectures are highly appreciated.
--- lib/alloc.c | 3 +++ 1 file changed, 3 insertions(+)lib/alloc.c +1
-0@@ -46,6 +46,7 @@ void *memalign(size_t alignment, size_t size)uintptr_t blkalign;uintptr_t mem; +assert (size && alignment);assert (alloc_ops && alloc_ops->memalign);if (alignment <=sizeof (uintptr_t )) alignment =sizeof (uintptr_t );
lib/alloc.c void *memalign(size_t alignment, size_t size) · line 64
Quoted 8 lines
lib/alloc.c +2
-0@@ -56,6 +57,8 @@ void *memalign(size_t alignment, size_t size) size =ALIGN (size +METADATA_EXTRA , alloc_ops->align_min); p = alloc_ops->memalign (blkalign, size); +assert (p !=NULL ); +/* Leave room for metadata before aligning the result. */ mem = (uintptr_t )p +METADATA_EXTRA ; mem =ALIGN (mem, alignment);
Signature · 3 lines
Thanks, David / dhildenb
Paolo Bonzini
maintainerQuoted 19 lines · Janosch Frank
Let's test for size and alignment in memalign to catch invalid input data. Also we need to test for NULL after calling the memalign function of the registered alloc operations.
Signed-off-by Janosch Frank <[email protected]>---
Tested only under s390, tests under other architectures are highly appreciated.
--- lib/alloc.c | 3 +++ 1 file changed, 3 insertions(+)lib/alloc.c +0
-0
lib/alloc.c void *memalign(size_t alignment, size_t size) · line 49
Quoted 4 lines
lib/alloc.c +1
-0@@ -46,6 +46,7 @@ void *memalign(size_t alignment, size_t size)uintptr_t blkalign;uintptr_t mem; +assert (size && alignment);
Do we want to return NULL instead on !size? This is how malloc(3) is documented.
Paolo
Quoted 13 lines
lib/alloc.c +2
-0assert (alloc_ops && alloc_ops->memalign);if (alignment <=sizeof (uintptr_t )) alignment =sizeof (uintptr_t );@@ -56,6 +57,8 @@ void *memalign(size_t alignment, size_t size) size =ALIGN (size +METADATA_EXTRA , alloc_ops->align_min); p = alloc_ops->memalign (blkalign, size); +assert (p !=NULL ); +/* Leave room for metadata before aligning the result. */ mem = (uintptr_t )p +METADATA_EXTRA ; mem =ALIGN (mem, alignment);
Quoted 23 lines · Paolo Bonzini
Quoted 22 lines · Janosch Frank
Let's test for size and alignment in memalign to catch invalid input data. Also we need to test for NULL after calling the memalign function of the registered alloc operations.
Signed-off-by Janosch Frank <[email protected]>---
Tested only under s390, tests under other architectures are highly appreciated.
--- lib/alloc.c | 3 +++ 1 file changed, 3 insertions(+)lib/alloc.c +0
-0@@ -46,6 +46,7 @@ void *memalign(size_t alignment, size_t size)uintptr_t blkalign;uintptr_t mem;
Quoted 6 lines
lib/alloc.c void *memalign(size_t alignment, size_t size) · line 49
Quoted 1 line
lib/alloc.c +1
-0+assert (size && alignment);Do we want to return NULL instead on !size? This is how malloc(3) is documented.
Paolo
I myself never check for NULL on a unit test and therefore added the asserts to have it fail visibly.
But sure, we can return NULL for both asserts.
Quoted 15 lines
Quoted 14 lines
lib/alloc.c +2
-0assert (alloc_ops && alloc_ops->memalign);if (alignment <=sizeof (uintptr_t )) alignment =sizeof (uintptr_t );@@ -56,6 +57,8 @@ void *memalign(size_t alignment, size_t size) size =ALIGN (size +METADATA_EXTRA , alloc_ops->align_min); p = alloc_ops->memalign (blkalign, size); +assert (p !=NULL ); +/* Leave room for metadata before aligning the result. */ mem = (uintptr_t )p +METADATA_EXTRA ; mem =ALIGN (mem, alignment);
Paolo Bonzini
maintainerQuoted 29 lines · Janosch Frank
Quoted 28 lines · Paolo Bonzini
Quoted 19 lines · Janosch Frank
Let's test for size and alignment in memalign to catch invalid input data. Also we need to test for NULL after calling the memalign function of the registered alloc operations.
Signed-off-by Janosch Frank <[email protected]>---
Tested only under s390, tests under other architectures are highly appreciated.
--- lib/alloc.c | 3 +++ 1 file changed, 3 insertions(+)lib/alloc.c +0
-0lib/alloc.c void *memalign(size_t alignment, size_t size) · line 49
Quoted 4 lines
lib/alloc.c +1
-0@@ -46,6 +46,7 @@ void *memalign(size_t alignment, size_t size)uintptr_t blkalign;uintptr_t mem; +assert (size && alignment);Do we want to return NULL instead on !size? This is how malloc(3) is documented.
Quoted 6 lines
Quoted 1 line
Paolo
I myself never check for NULL on a unit test and therefore added the asserts to have it fail visibly.
But sure, we can return NULL for both asserts.
Hmm yeah for ->memalign it makes sense since unit tests won't SIGSEGV. For !size let's return NULL, it can simplify code a bit.
Paolo
Quoted 17 lines
Quoted 15 lines
Quoted 14 lines
lib/alloc.c +2
-0assert (alloc_ops && alloc_ops->memalign);if (alignment <=sizeof (uintptr_t )) alignment =sizeof (uintptr_t );@@ -56,6 +57,8 @@ void *memalign(size_t alignment, size_t size) size =ALIGN (size +METADATA_EXTRA , alloc_ops->align_min); p = alloc_ops->memalign (blkalign, size); +assert (p !=NULL ); +/* Leave room for metadata before aligning the result. */ mem = (uintptr_t )p +METADATA_EXTRA ; mem =ALIGN (mem, alignment);