Skip to content

Commit bba6a1d

Browse files
feraralashkarcarlescufi
authored andcommitted
lib: os: hex: add explicit unsigned suffices
add explicit unsigned suffices to various immediate numbers, matching them to size_t, complying with required [misra-c2012-10.4] rule which states; Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category. Found as a coding guideline violation (Rule 10.4) by static code scanning tool. Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx). Signed-off-by: ferar alashkar <[email protected]>
1 parent 8081fb3 commit bba6a1d

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lib/os/hex.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,33 @@ int hex2char(uint8_t x, char *c)
3939

4040
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
4141
{
42-
if (hexlen < (buflen * 2 + 1)) {
42+
if (hexlen < (buflen * 2U + 1U)) {
4343
return 0;
4444
}
4545

4646
for (size_t i = 0; i < buflen; i++) {
47-
if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) {
47+
if (hex2char(buf[i] >> 4, &hex[2U * i]) < 0) {
4848
return 0;
4949
}
50-
if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) {
50+
if (hex2char(buf[i] & 0xf, &hex[2U * i + 1U]) < 0) {
5151
return 0;
5252
}
5353
}
5454

55-
hex[2 * buflen] = '\0';
56-
return 2 * buflen;
55+
hex[2U * buflen] = '\0';
56+
return 2U * buflen;
5757
}
5858

5959
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
6060
{
6161
uint8_t dec;
6262

63-
if (buflen < hexlen / 2 + hexlen % 2) {
63+
if (buflen < hexlen / 2U + hexlen % 2U) {
6464
return 0;
6565
}
6666

6767
/* if hexlen is uneven, insert leading zero nibble */
68-
if (hexlen % 2) {
68+
if (hexlen % 2U) {
6969
if (char2hex(hex[0], &dec) < 0) {
7070
return 0;
7171
}
@@ -75,17 +75,17 @@ size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
7575
}
7676

7777
/* regular hex conversion */
78-
for (size_t i = 0; i < hexlen / 2; i++) {
79-
if (char2hex(hex[2 * i], &dec) < 0) {
78+
for (size_t i = 0; i < hexlen / 2U; i++) {
79+
if (char2hex(hex[2U * i], &dec) < 0) {
8080
return 0;
8181
}
8282
buf[i] = dec << 4;
8383

84-
if (char2hex(hex[2 * i + 1], &dec) < 0) {
84+
if (char2hex(hex[2U * i + 1U], &dec) < 0) {
8585
return 0;
8686
}
8787
buf[i] += dec;
8888
}
8989

90-
return hexlen / 2 + hexlen % 2;
90+
return hexlen / 2U + hexlen % 2U;
9191
}

0 commit comments

Comments
 (0)