core/text/cstring_fit.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file cstring_fit.c | ||
| 3 | * @brief Copying text into a fixed buffer, and checking what a buffer holds before it is used. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "text/cstring_fit.h" | ||
| 8 | |||
| 9 | 40 | void copy_bounded(char *dst, uint8_t cap, const uint8_t *src, uint8_t len) | |
| 10 | { | ||
| 11 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 39 times.
|
40 | if (cap == 0) |
| 12 | { | ||
| 13 | // nowhere to even put the terminator. checked up front because cap - 1 below would wrap | ||
| 14 | // to 255 through the int promotion and copy a quarter kilobyte over whatever is there | ||
| 15 | 1 | return; | |
| 16 | } | ||
| 17 | |||
| 18 | 39 | uint8_t copy = len; | |
| 19 |
2/2✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 6 taken 35 times.
|
39 | if (copy > cap - 1) |
| 20 | { | ||
| 21 | 4 | copy = cap - 1; | |
| 22 | } | ||
| 23 |
2/2✓ Branch 8 → 7 taken 130 times.
✓ Branch 8 → 9 taken 39 times.
|
169 | for (uint8_t c = 0; c < copy; c++) |
| 24 | { | ||
| 25 | 130 | dst[c] = (char)src[c]; | |
| 26 | } | ||
| 27 | 39 | dst[copy] = '\0'; | |
| 28 | } | ||
| 29 | |||
| 30 | 10 | bool cstring_is_clean(const char *s, uint16_t size) | |
| 31 | { | ||
| 32 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 10 times.
|
10 | if (!s) |
| 33 | { | ||
| 34 | ✗ | return false; | |
| 35 | } | ||
| 36 | |||
| 37 |
2/2✓ Branch 11 → 5 taken 44 times.
✓ Branch 11 → 12 taken 1 time.
|
45 | for (uint16_t i = 0; i < size; i++) |
| 38 | { | ||
| 39 | // plain char is signed on the host and unsigned on the watch, so read it as unsigned | ||
| 40 | // to make the range test mean the same thing in a spec and on the wrist | ||
| 41 | 44 | unsigned char c = (unsigned char)s[i]; | |
| 42 |
2/2✓ Branch 5 → 6 taken 6 times.
✓ Branch 5 → 7 taken 38 times.
|
44 | if (c == '\0') |
| 43 | { | ||
| 44 | 6 | return i > 0; // ends inside its room, and said something on the way | |
| 45 | } | ||
| 46 | |||
| 47 | // control bytes are what a damaged blob looks like. everything from 0x20 up is kept, | ||
| 48 | // including UTF-8 lead and continuation bytes, so a place name like Zurich spelled with | ||
| 49 | // an umlaut keeps its bytes across a reboot | ||
| 50 |
4/4✓ Branch 7 → 8 taken 36 times.
✓ Branch 7 → 9 taken 2 times.
✓ Branch 8 → 9 taken 1 time.
✓ Branch 8 → 10 taken 35 times.
|
38 | if (c < 0x20 || c == 0x7F) |
| 51 | { | ||
| 52 | 3 | return false; | |
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 1 | return false; // ran out of room without ever ending | |
| 57 | } | ||
| 58 | |||
| 59 | 3 | bool cstring_setting_is_clean(const char *s, uint16_t size, const char *default_str) | |
| 60 | { | ||
| 61 |
3/4✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 1 time.
|
3 | bool empty_is_a_value = !default_str || default_str[0] == '\0'; |
| 62 |
6/8✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 11 taken 1 time.
✓ Branch 7 → 8 taken 2 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 2 times.
✗ Branch 8 → 11 not taken.
✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 1 time.
|
3 | if (empty_is_a_value && s && size > 0 && s[0] == '\0') |
| 63 | { | ||
| 64 | 1 | return true; | |
| 65 | } | ||
| 66 | |||
| 67 | 2 | return cstring_is_clean(s, size); | |
| 68 | } | ||
| 69 |