laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/35470?usp=email )
Change subject: utils: osmo_bcd2str(): fix applying non-zero offset to null pointer ......................................................................
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 --- M src/core/utils.c 1 file changed, 22 insertions(+), 1 deletion(-)
Approvals: laforge: Looks good to me, approved pespin: Looks good to me, but someone else must approve Jenkins Builder: Verified
diff --git a/src/core/utils.c b/src/core/utils.c index 882eb6f..1ec940d 100644 --- a/src/core/utils.c +++ b/src/core/utils.c @@ -147,13 +147,15 @@ */ 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))