summaryrefslogtreecommitdiff
path: root/src/math.c
blob: 366bd5e67339881c6ab9fc0b944fb7a106f92c8c (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
#include "config.h"

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "math.h"


int
mul_size(const size_t x, const size_t y, size_t *const out) {
	int rc = -1;

	assert(x != 0U);
	assert(y != 0U);

	const bool overflows = x > (SIZE_MAX / y);
	if (overflows) {
		goto out;
	}

	*out = x * y;
	rc = 0;
out:
	return rc;
}