utils: Fix integer promotion UB in dec2bcd{16,32} encoders
Addresses the following:
```
$ UBSAN_OPTIONS=halt_on_error=1 meson test -C build libpldm_utils_test
ninja: no work to do.
ninja: Entering directory `/mnt/host/andrew/src/openbmc/libpldm/build'
ninja: no work to do.
1/1 libpldm_utils_test FAIL 0.01s exit status 1
>>> MALLOC_PERTURB_=165 LD_LIBRARY_PATH=/mnt/host/andrew/src/openbmc/libpldm/build/ /mnt/host/andrew/src/openbmc/libpldm/build/tests/libpldm_utils_test
Ok: 0
Expected Fail: 0
Fail: 1
Unexpected Pass: 0
Skipped: 0
Timeout: 0
Full log written to /mnt/host/andrew/src/openbmc/libpldm/build/meson-logs/testlog.txt
$ MALLOC_PERTURB_=194 \
> LD_LIBRARY_PATH=/mnt/host/andrew/src/openbmc/libpldm/build/ \
> /mnt/host/andrew/src/openbmc/libpldm/build/tests/libpldm_utils_test >/dev/null
../src/utils.c:186:57: runtime error: left shift of 39321 by 16 places cannot be represented in type 'int'
$
```
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I55ae3967706c1e0bee2869063a63746410c819f6
diff --git a/src/utils.c b/src/utils.c
index 482fd95..fb3553d 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -173,7 +173,7 @@
uint16_t dec2bcd16(uint16_t dec)
{
- return dec2bcd8(dec % 100) | dec2bcd8(dec / 100) << 8;
+ return dec2bcd8(dec % 100) | ((uint16_t)(dec2bcd8(dec / 100)) << 8);
}
uint32_t bcd2dec32(uint32_t bcd)
@@ -183,7 +183,8 @@
uint32_t dec2bcd32(uint32_t dec)
{
- return dec2bcd16(dec % 10000) | dec2bcd16(dec / 10000) << 16;
+ return dec2bcd16(dec % 10000) |
+ ((uint32_t)(dec2bcd16(dec / 10000)) << 16);
}
bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,