Branch data Line data Source code
1 : : #include "mem.h"
2 : : #include <string.h>
3 : :
4 : 588 : Return memory_copy_literal(
5 : : memory *destination,
6 : : const char *literal)
7 : : {
8 : : /* Status returned by this function through provide()
9 : : Default value assumes successful completion */
10 : 588 : Return status = SUCCESS;
11 : :
12 [ - + ]: 588 : if(destination == NULL)
13 : : {
14 : 0 : report("Memory management; copy_literal destination must be non-NULL");
15 : 0 : status = FAILURE;
16 : : }
17 : :
18 [ + - - + ]: 588 : if((TRIUMPH & status) && literal == NULL)
19 : : {
20 : : /* Treat NULL literals as a no-op to keep existing payload intact */
21 : 0 : provide(SUCCESS);
22 : : }
23 : :
24 [ + - - + ]: 588 : if((TRIUMPH & status) && destination->element_size != sizeof(char))
25 : : {
26 : 0 : report("Memory management; copy_literal supports byte-sized elements only");
27 : 0 : status = FAILURE;
28 : : }
29 : :
30 : 588 : size_t literal_length = 0;
31 : :
32 [ + - ]: 588 : if(TRIUMPH & status)
33 : : {
34 : 588 : literal_length = strlen(literal);
35 : : }
36 : :
37 : 588 : size_t new_total_elements = 0;
38 : :
39 [ + - ]: 588 : if(TRIUMPH & status)
40 : : {
41 [ - + ]: 588 : if(literal_length == SIZE_MAX)
42 : : {
43 : 0 : report("Memory management; Not enough room for string terminator");
44 : 0 : status = FAILURE;
45 : : } else {
46 : 588 : new_total_elements = literal_length + 1;
47 : : }
48 : : }
49 : :
50 : 588 : size_t literal_bytes = 0;
51 : :
52 [ + - - + ]: 588 : run(memory_guarded_size(destination->element_size,literal_length,&literal_bytes));
53 : :
54 [ + - - + ]: 588 : run(resize(destination,new_total_elements));
55 : :
56 [ + - ]: 588 : if(TRIUMPH & status)
57 : : {
58 : 588 : unsigned char *destination_bytes = (unsigned char *)destination->data;
59 : :
60 [ - + ]: 588 : if(destination_bytes == NULL)
61 : : {
62 : 0 : report("Memory management; Destination data pointer is NULL after resize");
63 : 0 : status = FAILURE;
64 : : } else {
65 [ + - ]: 588 : if(literal_bytes > 0)
66 : : {
67 : 588 : memcpy(destination_bytes,literal,literal_bytes);
68 : : }
69 : :
70 : 588 : destination_bytes[literal_bytes] = '\0';
71 : 588 : telemetry_string_padding_event();
72 : : }
73 : : }
74 : :
75 : 588 : provide(status);
76 : : }
|