mirror of
https://github.com/apache/nuttx.git
synced 2026-05-31 23:40:19 +08:00
mm/mm_heap/mm_calloc.c: Verify that the number of elements times the size of an element will not overflow type size_t. This is required by the SEI CERT C coding style and resolves anonymous Bitbucket Issue #139
This commit is contained in:
@@ -2181,6 +2181,9 @@ o File system / Generic drivers (fs/, drivers/)
|
|||||||
space at the seek position. Seeking beyond the end of the file
|
space at the seek position. Seeking beyond the end of the file
|
||||||
has the side effect of extending the file.
|
has the side effect of extending the file.
|
||||||
|
|
||||||
|
[NOTE: This automatic extension of the file cluster allocation
|
||||||
|
is probably unnecessary and another issue of its own.]
|
||||||
|
|
||||||
For example, suppose you have a cluster size that is 4096 bytes
|
For example, suppose you have a cluster size that is 4096 bytes
|
||||||
and a file that is 8192 bytes long. Then the file will consist
|
and a file that is 8192 bytes long. Then the file will consist
|
||||||
of 2 allocated clusters at offsets 0 through 8191.
|
of 2 allocated clusters at offsets 0 through 8191.
|
||||||
|
|||||||
+12
-1
@@ -57,9 +57,20 @@ FAR void *mm_calloc(FAR struct mm_heap_s *heap, size_t n, size_t elem_size)
|
|||||||
{
|
{
|
||||||
FAR void *ret = NULL;
|
FAR void *ret = NULL;
|
||||||
|
|
||||||
|
/* Verify input parameters */
|
||||||
|
|
||||||
if (n > 0 && elem_size > 0)
|
if (n > 0 && elem_size > 0)
|
||||||
{
|
{
|
||||||
ret = mm_zalloc(heap, n * elem_size);
|
/* Assure that the following multiplication cannot overflow the size_t
|
||||||
|
* type, i.e., that: SIZE_MAX >= n * elem_size
|
||||||
|
*
|
||||||
|
* Refer to SEI CERT C Coding Standard.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (n <= (SIZE_MAX / elem_size))
|
||||||
|
{
|
||||||
|
ret = mm_zalloc(heap, n * elem_size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
Reference in New Issue
Block a user