Branch data Line data Source code
1 : : #include "mem.h"
2 : :
3 : : /**
4 : : * @brief Subtract one size_t value from another with underflow detection
5 : : *
6 : : * Used by implementation files to detect unsigned underflow when computing
7 : : * deltas between byte counts
8 : : *
9 : : * @param left Left operand
10 : : * @param right Right operand
11 : : * @param difference Output pointer for the difference on success
12 : : * @return Return status indicating whether the subtraction succeeded
13 : : */
14 : 3 : Return mem_guarded_subtract(
15 : : size_t left,
16 : : size_t right,
17 : : size_t *difference)
18 : : {
19 : : /* Status returned by this function through provide()
20 : : Default value assumes successful completion */
21 : 3 : Return status = SUCCESS;
22 : :
23 [ - + ]: 3 : if(difference == NULL)
24 : : {
25 : 0 : report("Memory management; Output pointer is NULL");
26 : 0 : provide(FAILURE);
27 : : }
28 : :
29 [ + + ]: 3 : if(left < right)
30 : : {
31 : 2 : status = FAILURE;
32 : 2 : telemetry_arithmetic_guard_failures();
33 : : }
34 : :
35 [ + + ]: 3 : if(TRIUMPH & status)
36 : : {
37 : 1 : *difference = left - right;
38 : : }
39 : :
40 : 3 : provide(status);
41 : : }
|