mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-02 22:29:25 +00:00
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
c30bb893f8
commit
cef9bf51a9
3 changed files with 27 additions and 0 deletions
|
@ -37,6 +37,7 @@ src = [
|
||||||
'src/util/intmap.c',
|
'src/util/intmap.c',
|
||||||
'src/util/intr.c',
|
'src/util/intr.c',
|
||||||
'src/util/log.c',
|
'src/util/log.c',
|
||||||
|
'src/util/memory.c',
|
||||||
'src/util/net.c',
|
'src/util/net.c',
|
||||||
'src/util/net_intr.c',
|
'src/util/net_intr.c',
|
||||||
'src/util/process.c',
|
'src/util/process.c',
|
||||||
|
|
14
app/src/util/memory.c
Normal file
14
app/src/util/memory.c
Normal file
|
@ -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);
|
||||||
|
}
|
12
app/src/util/memory.h
Normal file
12
app/src/util/memory.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef SC_MEMORY_H
|
||||||
|
#define SC_MEMORY_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/* Like calloc(), but without initialization.
|
||||||
|
* Like reallocarray(), but without reallocation.
|
||||||
|
*/
|
||||||
|
void *
|
||||||
|
sc_allocarray(size_t nmemb, size_t size);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Add a link
Reference in a new issue