From: Arran Cudbard-Bell Date: Tue, 4 Aug 2015 17:07:35 +0000 (-0400) Subject: Allow casting from unsigned to signed as long as the unsigned value is <= INT_MAX X-Git-Tag: release_3_0_10~260 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c7e30d4108c54a1f69f8fc7d5330acbbffb7523e;p=thirdparty%2Ffreeradius-server.git Allow casting from unsigned to signed as long as the unsigned value is <= INT_MAX --- diff --git a/src/lib/value.c b/src/lib/value.c index a30e21dec52..47c50c301ef 100644 --- a/src/lib/value.c +++ b/src/lib/value.c @@ -1132,6 +1132,45 @@ ssize_t value_data_cast(TALLOC_CTX *ctx, value_data_t *dst, goto fixed_length; } + /* + * We can cast integers less that < INT_MAX to signed + */ + if (dst_type == PW_TYPE_SIGNED) { + switch (src_type) { + case PW_TYPE_BYTE: + dst->sinteger = src->byte; + break; + + case PW_TYPE_SHORT: + dst->sinteger = src->ushort; + break; + + case PW_TYPE_INTEGER: + if (src->integer > INT_MAX) { + fr_strerror_printf("Invalid cast: From integer to signed. integer value %u is larger " + "than max signed int and would overflow", src->integer); + return -1; + } + dst->sinteger = (int)src->integer; + break; + + case PW_TYPE_INTEGER64: + if (src->integer > INT_MAX) { + fr_strerror_printf("Invalid cast: From integer64 to signed. integer64 value %" PRIu64 + " is larger than max signed int and would overflow", src->integer64); + return -1; + } + dst->sinteger = (int)src->integer64; + break; + + case PW_TYPE_OCTETS: + goto do_octets; + + default: + goto invalid_cast; + } + goto fixed_length; + } /* * Conversions between IPv4 addresses, IPv6 addresses, IPv4 prefixes and IPv6 prefixes *