Branch data Line data Source code
1 : : #include "mem.h"
2 : :
3 : : /**
4 : : * @brief Compute a descriptor-backed byte size with overflow detection
5 : : *
6 : : * Uses @ref memory::single_element_size from @p memory_structure and multiplies it by
7 : : * @p element_count after validating the descriptor metadata
8 : : *
9 : : * @param memory_structure Descriptor providing the element size
10 : : * @param element_count Number of elements to convert into bytes
11 : : * @param size_in_bytes Output pointer for the byte size on success
12 : : * @return Return status indicating whether the size calculation succeeded
13 : : */
14 : 566162 : Return mem_guarded_byte_size(
15 : : const memory *memory_structure,
16 : : size_t element_count,
17 : : size_t *size_in_bytes)
18 : : {
19 : : /* Status returned by this function through provide()
20 : : Default value assumes successful completion */
21 : 566162 : Return status = SUCCESS;
22 : :
23 [ - + ]: 566162 : if(size_in_bytes == NULL)
24 : : {
25 : 0 : report("Memory management; Output pointer is NULL");
26 : 0 : provide(FAILURE);
27 : : }
28 : :
29 [ - + ]: 566162 : if(memory_structure == NULL)
30 : : {
31 : 0 : report("Memory management; Descriptor is NULL");
32 : 0 : provide(FAILURE);
33 : : }
34 : :
35 [ - + ]: 566162 : if(memory_structure->single_element_size == 0)
36 : : {
37 : 0 : report("Memory management; Descriptor element size is zero (uninitialized)");
38 : 0 : provide(FAILURE);
39 : : }
40 : :
41 [ + + ]: 566162 : if(element_count > SIZE_MAX / memory_structure->single_element_size)
42 : : {
43 : 1 : status = FAILURE;
44 : 1 : telemetry_arithmetic_guard_failures();
45 : : }
46 : :
47 [ + + ]: 566162 : if(TRIUMPH & status)
48 : : {
49 : 566161 : *size_in_bytes = memory_structure->single_element_size * element_count;
50 : : }
51 : :
52 : 566162 : provide(status);
53 : : }
|