Branch data Line data Source code
1 : : #include "mem.h"
2 : :
3 : : /**
4 : : * @brief Add two size_t values with overflow detection
5 : : *
6 : : * Used by implementation files to detect overflows when computing element
7 : : * counts or string sizes
8 : : *
9 : : * @param left Left operand
10 : : * @param right Right operand
11 : : * @param sum Output pointer for the sum on success
12 : : * @return Return status indicating whether the addition succeeded
13 : : */
14 : 167143 : Return mem_guarded_add(
15 : : size_t left,
16 : : size_t right,
17 : : size_t *sum)
18 : : {
19 : : /* This function was reviewed line by line by a human and is not AI-generated
20 : : Any change to this function requires separate explicit approval */
21 : :
22 : : /* Status returned by this function through provide()
23 : : Default value assumes successful completion */
24 : 167143 : Return status = SUCCESS;
25 : :
26 [ - + ]: 167143 : if(sum == NULL)
27 : : {
28 : 0 : report("Memory management; Output pointer is NULL");
29 : 0 : provide(FAILURE);
30 : : }
31 : :
32 [ + + ]: 167143 : if(left > SIZE_MAX - right)
33 : : {
34 : 2 : status = FAILURE;
35 : 2 : telemetry_arithmetic_guard_failures();
36 : : }
37 : :
38 [ + + ]: 167143 : if(TRIUMPH & status)
39 : : {
40 : 167141 : *sum = left + right;
41 : : }
42 : :
43 : 167143 : provide(status);
44 : : }
|