Branch data Line data Source code
1 : : #include "mem.h"
2 : :
3 : : /**
4 : : * @brief Compute visible C-string length inside a bounded descriptor.
5 : : *
6 : : * Scans descriptor bytes until the first `'\0'` or `memory_structure->length`,
7 : : * whichever comes first, and writes the result to @p length_out.
8 : : *
9 : : * Notes:
10 : : * - The helper does not validate `element_size`; callers should use it with
11 : : * byte-oriented descriptors.
12 : : * - The function returns through `provide(status)`, so a non-`SUCCESS`
13 : : * `global_return_status` may override the local return code.
14 : : */
15 : 6576 : Return memory_string_length(
16 : : const memory *memory_structure,
17 : : size_t *length_out)
18 : : {
19 : : /* Status returned by this function through provide()
20 : : Default value assumes successful completion */
21 : 6576 : Return status = SUCCESS;
22 : :
23 [ + - - + ]: 6576 : if(memory_structure == NULL || length_out == NULL)
24 : : {
25 : 0 : report("Memory management; Invalid arguments for string length helper");
26 : 0 : status = FAILURE;
27 [ + + ]: 6576 : } else if(memory_structure->length == 0){
28 : 233 : *length_out = 0;
29 [ - + ]: 6343 : } else if(memory_structure->data == NULL){
30 : 0 : *length_out = 0;
31 : : } else {
32 : 6343 : const unsigned char *bytes = (const unsigned char *)memory_structure->data;
33 : 6343 : size_t index = 0;
34 : :
35 [ + - ]: 2462690 : for(; index < memory_structure->length; ++index)
36 : : {
37 [ + + ]: 2462690 : if(bytes[index] == '\0')
38 : : {
39 : 6343 : break;
40 : : }
41 : : }
42 : :
43 : 6343 : *length_out = index;
44 : : }
45 : :
46 : 6576 : provide(status);
47 : : }
|