Branch data Line data Source code
1 : : #include "mem_internal.h"
2 : : #include <string.h>
3 : :
4 : : /**
5 : : * @brief Overwrite one logical element with a zero terminator
6 : : *
7 : : * Writes a zero-valued element at @p terminator_index using the descriptor's
8 : : * configured element width. The helper performs only the minimal descriptor
9 : : * checks needed to reject impossible writes while remaining reusable across
10 : : * string helpers inside the library
11 : : *
12 : : * @param memory_structure Descriptor whose buffer receives the terminator
13 : : * @param terminator_index Logical element index where the terminator is written
14 : : * @return `SUCCESS` on success; `FAILURE` otherwise
15 : : */
16 : 70458 : Return mem_write_zero_terminator(
17 : : memory *memory_structure,
18 : : const size_t terminator_index)
19 : : {
20 : : /* Status returned by this function through provide()
21 : : Default value assumes successful completion */
22 : 70458 : Return status = SUCCESS;
23 : 70458 : size_t terminator_offset = 0;
24 : 70458 : size_t required_bytes = 0;
25 : :
26 [ + - - + ]: 70458 : if(memory_structure == NULL || memory_structure->single_element_size == 0)
27 : : {
28 : 0 : report("Memory management; Invalid descriptor for zero terminator write");
29 : 0 : status = FAILURE;
30 : : }
31 : :
32 [ + - - + ]: 70458 : if((TRIUMPH & status) && memory_structure->data == NULL)
33 : : {
34 : 0 : report("Memory management; Data pointer is NULL during zero terminator write");
35 : 0 : status = FAILURE;
36 : : }
37 : :
38 [ + - ]: 70458 : if(TRIUMPH & status)
39 : : {
40 : 70458 : run(mem_guarded_byte_size(memory_structure,terminator_index,&terminator_offset));
41 : 70458 : run(mem_guarded_add(terminator_offset,memory_structure->single_element_size,&required_bytes));
42 : :
43 [ - + ]: 70458 : if(CRITICAL & status)
44 : : {
45 : 0 : report("Memory management; Zero terminator write overflows descriptor bounds");
46 : : }
47 : : }
48 : :
49 [ + - - + ]: 70458 : if((TRIUMPH & status) && required_bytes > memory_structure->actually_allocated_bytes)
50 : : {
51 : 0 : report("Memory management; Zero terminator write exceeds reserved capacity");
52 : 0 : status = FAILURE;
53 : : }
54 : :
55 [ + - ]: 70458 : if(TRIUMPH & status)
56 : : {
57 : 70458 : memset((unsigned char *)memory_structure->data + terminator_offset,0,memory_structure->single_element_size);
58 : 70458 : telemetry_string_terminator_writes();
59 : : }
60 : :
61 : 70458 : provide(status);
62 : : }
|