diff options
author | Vadim Yanitskiy <vyanitskiy@sysmocom.de> | 2023-12-31 15:18:20 +0700 |
---|---|---|
committer | laforge <laforge@osmocom.org> | 2024-01-03 09:54:46 +0000 |
commit | 5ce0131d4389a44a02b2d16fff4655bb21240ae7 (patch) | |
tree | 9893c578273f9d683c511476239b17f0b46d2925 /src/core/utils.c | |
parent | fb4ce0b029cb52278b8d835c71ce54743226c239 (diff) |
utils: osmo_bcd2str(): fix applying non-zero offset to null pointer
This can be seen when building with CC=clang:
utils.c:150:22: runtime error: applying non-zero offset 100 to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior utils.c:150:22 in
utils.c:150:33: runtime error: addition of unsigned offset to 0x000000000064 overflowed to 0x000000000063
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior utils.c:150:33 in
The *dst pointer may be NULL (e.g. bcd2str_test() is passing it).
This makes tests/utils/utils_test fail. Let's fix this.
Change-Id: I542aef1ac220891b6bbdb0c60c39232f0df0a43c
Diffstat (limited to 'src/core/utils.c')
-rw-r--r-- | src/core/utils.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/core/utils.c b/src/core/utils.c index 882eb6f6..1ec940d0 100644 --- a/src/core/utils.c +++ b/src/core/utils.c @@ -147,13 +147,15 @@ uint8_t osmo_char2bcd(char c) */ int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex) { - char *dst_end = dst + dst_size - 1; + char *dst_end; int nibble_i; int rc = 0; if (!dst || dst_size < 1 || start_nibble < 0) return -ENOMEM; + dst_end = dst + dst_size - 1; + for (nibble_i = start_nibble; nibble_i < end_nibble && dst < dst_end; nibble_i++, dst++) { uint8_t nibble = bcd[nibble_i >> 1]; if ((nibble_i & 1)) |