aboutsummaryrefslogtreecommitdiff
path: root/lib/alloc/bufalloc_noterm.c
blob: 82585210a886447fc8d03afb2b59ae3b354cc357 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <errno.h>
#if __has_include(<stdckdint.h>)
#	include <stdckdint.h>
#	ifdef __APPLE__
#		warning "stdckdint.h now available on Mac; remove manual ckd_*() code"
#	endif
#elifdef __GNUC__
#	define ckd_add(r, a, b) ((bool)__builtin_add_overflow(a, b, r))
#	define ckd_mul(r, a, b) ((bool)__builtin_mul_overflow(a, b, r))
#else
#	define ckd_add(r, a, b) (*(r) = (a) + (b))
#	define ckd_mul(r, a, b) (*(r) = (a) * (b))
#	warning "ckd_*() not supported on the current platform"
#endif
#include <stdlib.h>

#include "alloc.h"

void *
bufalloc_noterm(void *p, size_t n, size_t m)
{
	if (ckd_mul(&n, n, m)) {
		errno = EOVERFLOW;
		return nullptr;
	}

	return realloc(p, n);
}