reallocarray() is a safer variant of realloc which checks for multiplication overflow in case of array allocation. Since it's not available in Glibc < 2.26 import kernel's overflow.h and add a static inline implementation when needed. Use feature detection to probe for existence of reallocarray. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Reviewed-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
21 lines
434 B
C
21 lines
434 B
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/* Copyright (C) 2018 Netronome Systems, Inc. */
|
|
|
|
#ifndef __TOOLS_LIBC_COMPAT_H
|
|
#define __TOOLS_LIBC_COMPAT_H
|
|
|
|
#include <stdlib.h>
|
|
#include <linux/overflow.h>
|
|
|
|
#ifdef COMPAT_NEED_REALLOCARRAY
|
|
static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
|
|
{
|
|
size_t bytes;
|
|
|
|
if (unlikely(check_mul_overflow(nmemb, size, &bytes)))
|
|
return NULL;
|
|
return realloc(ptr, bytes);
|
|
}
|
|
#endif
|
|
#endif
|