Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
asn1c
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
asn1c
Commits
68079d38
Commit
68079d38
authored
Oct 08, 2017
by
Lev Walkin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
detect double->float overflows
parent
ab25f195
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
21 deletions
+61
-21
skeletons/NativeReal.c
skeletons/NativeReal.c
+37
-19
skeletons/REAL.c
skeletons/REAL.c
+13
-2
skeletons/REAL.h
skeletons/REAL.h
+11
-0
No files found.
skeletons/NativeReal.c
View file @
68079d38
...
@@ -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_MI
N
INFINITY
,
-
INFINITY
,
NA
N
-
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
;
}
}
...
...
skeletons/REAL.c
View file @
68079d38
...
@@ -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
,
...
...
skeletons/REAL.h
View file @
68079d38
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment