mirror of https://github.com/Genymobile/scrcpy
Add sc_allocarray() util
Add a function to allocate an array, which fails safely in the case where the multiplication would overflow.
This commit is contained in:
parent
c735b8c127
commit
457385d5f4
|
@ -35,6 +35,7 @@ src = [
|
|||
'src/util/intmap.c',
|
||||
'src/util/intr.c',
|
||||
'src/util/log.c',
|
||||
'src/util/memory.c',
|
||||
'src/util/net.c',
|
||||
'src/util/net_intr.c',
|
||||
'src/util/process.c',
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
#include "memory.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
void *
|
||||
sc_allocarray(size_t nmemb, size_t size) {
|
||||
size_t bytes;
|
||||
if (__builtin_mul_overflow(nmemb, size, &bytes)) {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
return malloc(bytes);
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef SC_MEMORY_H
|
||||
#define SC_MEMORY_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* Allocate an array of `nmemb` items of `size` bytes each
|
||||
*
|
||||
* Like calloc(), but without initialization.
|
||||
* Like reallocarray(), but without reallocation.
|
||||
*/
|
||||
void *
|
||||
sc_allocarray(size_t nmemb, size_t size);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue