diff options
author | Jere Leppänen <jere.leppanen@nokia.com> | 2024-03-20 17:19:54 +0200 |
---|---|---|
committer | Matias Elo <matias.elo@nokia.com> | 2024-04-16 16:49:46 +0300 |
commit | 9795b754631140566d6e793ec76d56f5ead064c4 (patch) | |
tree | 526ab3a1b849d9e2b8d4712c199c2cb6f353a2a4 | |
parent | 3c2f48311b4e52ea62f933fafe931742447cf1fa (diff) |
example: ipsec: use unsigned integers to form ip4 address
In parse_ipv4_string(), use unsigned variables and scanf formatters to
avoid negative values, and cast to uint32_t in order to avoid
undefined behavior in bit shift operations.
Fixes GCC undefined sanitizer error:
odp_ipsec_misc.h:192:47: runtime error: left shift of 192 by 24 places cannot be represented in type 'int'
Signed-off-by: Jere Leppänen <jere.leppanen@nokia.com>
Reviewed-by: Janne Peltonen <janne.peltonen@nokia.com>
-rw-r--r-- | example/ipsec_crypto/odp_ipsec_misc.h | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/example/ipsec_crypto/odp_ipsec_misc.h b/example/ipsec_crypto/odp_ipsec_misc.h index 921c4e3c0..5fba274d0 100644 --- a/example/ipsec_crypto/odp_ipsec_misc.h +++ b/example/ipsec_crypto/odp_ipsec_misc.h @@ -167,18 +167,18 @@ char *ipv4_addr_str(char *b, uint32_t addr) static inline int parse_ipv4_string(char *ipaddress, uint32_t *addr, uint32_t *mask) { - int b[4]; + unsigned int b[4]; int qualifier = 32; int converted; if (strchr(ipaddress, '/')) { - converted = sscanf(ipaddress, "%d.%d.%d.%d/%d", + converted = sscanf(ipaddress, "%u.%u.%u.%u/%d", &b[3], &b[2], &b[1], &b[0], &qualifier); if (5 != converted) return -1; } else { - converted = sscanf(ipaddress, "%d.%d.%d.%d", + converted = sscanf(ipaddress, "%u.%u.%u.%u", &b[3], &b[2], &b[1], &b[0]); if (4 != converted) return -1; @@ -189,7 +189,7 @@ int parse_ipv4_string(char *ipaddress, uint32_t *addr, uint32_t *mask) if (!qualifier || (qualifier > 32)) return -1; - *addr = b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24; + *addr = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24; if (mask) *mask = ~(0xFFFFFFFF & ((1ULL << (32 - qualifier)) - 1)); |