Commit 68079d38 authored by Lev Walkin's avatar Lev Walkin

detect double->float overflows

parent ab25f195
...@@ -78,6 +78,7 @@ asn_TYPE_descriptor_t asn_DEF_NativeReal = { ...@@ -78,6 +78,7 @@ asn_TYPE_descriptor_t asn_DEF_NativeReal = {
0 /* No specifics */ 0 /* No specifics */
}; };
static size_t NativeReal__float_size(const asn_TYPE_descriptor_t *td);
static double NativeReal__get_double(const asn_TYPE_descriptor_t *td, static double NativeReal__get_double(const asn_TYPE_descriptor_t *td,
const void *ptr); const void *ptr);
static ssize_t NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, static ssize_t NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr,
...@@ -553,36 +554,48 @@ NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -553,36 +554,48 @@ NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
#ifndef NAN #ifndef NAN
#define NAN (0.0/0.0) #define NAN (0.0/0.0)
#endif #endif
static const double values[] = { static const double double_values[] = {
0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255, -M_E, M_E, -M_PI, M_PI, /* Better precision than with floats */
-1E+308, 1E+308,
/* 2^51 */ /* 2^51 */
-2251799813685248.0, 2251799813685248.0, -2251799813685248.0, 2251799813685248.0,
/* 2^52 */ /* 2^52 */
-4503599627370496.0, 4503599627370496.0, -4503599627370496.0, 4503599627370496.0,
/* 2^100 */ /* 2^100 */
-1267650600228229401496703205376.0, 1267650600228229401496703205376.0, -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
-FLT_MIN, FLT_MIN,
-FLT_MAX, FLT_MAX,
-DBL_MIN, DBL_MIN, -DBL_MIN, DBL_MIN,
-DBL_MAX, DBL_MAX, -DBL_MAX, DBL_MAX,
#ifdef DBL_TRUE_MIN
-DBL_TRUE_MIN, DBL_TRUE_MIN
#endif
};
static const float float_values[] = {
0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255,
-FLT_MIN, FLT_MIN,
-FLT_MAX, FLT_MAX,
#ifdef FLT_TRUE_MIN #ifdef FLT_TRUE_MIN
-FLT_TRUE_MIN, FLT_TRUE_MIN, -FLT_TRUE_MIN, FLT_TRUE_MIN,
#endif #endif
#ifdef DBL_TRUE_MIN INFINITY, -INFINITY, NAN
-DBL_TRUE_MIN, DBL_TRUE_MIN, };
#endif ssize_t float_set_size = NativeReal__float_size(td);
INFINITY, -INFINITY, NAN}; const size_t n_doubles = sizeof(double_values) / sizeof(double_values[0]);
ssize_t float_set_size; const size_t n_floats = sizeof(float_values) / sizeof(float_values[0]);
double d; double d;
(void)constraints; (void)constraints;
if(max_length == 0) return result_skipped; if(max_length == 0) return result_skipped;
d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)]; if(float_set_size == sizeof(double) && asn_random_between(0, 1) == 0) {
d = double_values[asn_random_between(0, n_doubles - 1)];
} else {
d = float_values[asn_random_between(0, n_floats - 1)];
}
float_set_size = NativeReal__set(td, sptr, d); if(NativeReal__set(td, sptr, d) < 0) {
if(float_set_size < 0) return result_failed; return result_failed;
}
result_ok.length = float_set_size; result_ok.length = float_set_size;
return result_ok; return result_ok;
...@@ -593,11 +606,16 @@ NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -593,11 +606,16 @@ NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
* Local helper functions. * Local helper functions.
*/ */
static double static size_t
NativeReal__get_double(const asn_TYPE_descriptor_t *td, const void *ptr) { NativeReal__float_size(const asn_TYPE_descriptor_t *td) {
const asn_NativeReal_specifics_t *specs = const asn_NativeReal_specifics_t *specs =
(const asn_NativeReal_specifics_t *)td->specifics; (const asn_NativeReal_specifics_t *)td->specifics;
size_t float_size = specs ? specs->float_size : sizeof(double); return specs ? specs->float_size : sizeof(double);
}
static double
NativeReal__get_double(const asn_TYPE_descriptor_t *td, const void *ptr) {
size_t float_size = NativeReal__float_size(td);
if(float_size == sizeof(float)) { if(float_size == sizeof(float)) {
return *(const float *)ptr; return *(const float *)ptr;
} else { } else {
...@@ -607,9 +625,7 @@ NativeReal__get_double(const asn_TYPE_descriptor_t *td, const void *ptr) { ...@@ -607,9 +625,7 @@ NativeReal__get_double(const asn_TYPE_descriptor_t *td, const void *ptr) {
static ssize_t /* Returns -1 or float size. */ static ssize_t /* Returns -1 or float size. */
NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, double d) { NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, double d) {
const asn_NativeReal_specifics_t *specs = size_t float_size = NativeReal__float_size(td);
(const asn_NativeReal_specifics_t *)td->specifics;
size_t float_size = specs ? specs->float_size : sizeof(double);
void *native; void *native;
if(!(native = *sptr)) { if(!(native = *sptr)) {
...@@ -620,7 +636,9 @@ NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, double d) { ...@@ -620,7 +636,9 @@ NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, double d) {
} }
if(float_size == sizeof(float)) { if(float_size == sizeof(float)) {
*(float *)native = d; if(asn_double2float(d, (float *)native)) {
return -1;
}
} else { } else {
*(double *)native = d; *(double *)native = d;
} }
......
...@@ -63,7 +63,6 @@ static int asn_isfinite(double d) { ...@@ -63,7 +63,6 @@ static int asn_isfinite(double d) {
#endif #endif
#endif /* clang */ #endif /* clang */
/* /*
* REAL basic type description. * REAL basic type description.
*/ */
...@@ -835,6 +834,19 @@ asn_double2REAL(REAL_t *st, double dbl_value) { ...@@ -835,6 +834,19 @@ asn_double2REAL(REAL_t *st, double dbl_value) {
return 0; return 0;
} }
int CC_ATTR_NO_SANITIZE("float-cast-overflow")
asn_double2float(double d, float *outcome) {
float f = d;
*outcome = f;
if(asn_isfinite(d) == asn_isfinite(f)) {
return 0;
} else {
return -1;
}
}
#ifndef ASN_DISABLE_OER_SUPPORT #ifndef ASN_DISABLE_OER_SUPPORT
/* /*
...@@ -941,7 +953,6 @@ REAL_encode_uper(asn_TYPE_descriptor_t *td, ...@@ -941,7 +953,6 @@ REAL_encode_uper(asn_TYPE_descriptor_t *td,
#endif /* ASN_DISABLE_PER_SUPPORT */ #endif /* ASN_DISABLE_PER_SUPPORT */
asn_random_fill_result_t asn_random_fill_result_t
REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
const asn_encoding_constraints_t *constraints, const asn_encoding_constraints_t *constraints,
......
...@@ -47,6 +47,17 @@ ssize_t REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *a ...@@ -47,6 +47,17 @@ ssize_t REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *a
int asn_REAL2double(const REAL_t *real_ptr, double *d); int asn_REAL2double(const REAL_t *real_ptr, double *d);
int asn_double2REAL(REAL_t *real_ptr, double d); int asn_double2REAL(REAL_t *real_ptr, double d);
/*
* Downcast double to float while checking that no overflow occurs.
* This allows stricter control of the input data.
* RETURN VALUES:
* 0: The conversion was successful (perhaps with a loss of precision)
* -1: The conversion created overflow into infinities.
* The (outcome) is ALWAYS set to a value you'd expect from the
* standard silent float to double conversion behavior.
*/
int asn_double2float(double d, float *outcome);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment