Skip to content

Commit

Permalink
Fix parsing of subnormal Real values (#9696)
Browse files Browse the repository at this point in the history
- Fix parsing of Real values between DBL_TRUE_MIN and DBL_MIN.
  • Loading branch information
perost committed Nov 14, 2022
1 parent d06b6c8 commit 65de200
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion OMCompiler/Parser/Modelica.g
Expand Up @@ -1762,7 +1762,9 @@ primary returns [void* ast]
if (errno == 0) // restore errno
errno = errno_saved;
if (!(*endptr == 0 && errno_local == 0)) {
if (*endptr == 0 && errno_local == ERANGE && fabs(d) == HUGE_VAL) { // overflow is an error
if (*endptr == 0 && !(d == 0 || fabs(d) > DBL_MIN)) { // ignore errors for subnormal values
$ast = Absyn__REAL(mmc_mk_scon(chars));
} else if (*endptr == 0 && errno_local == ERANGE && fabs(d) == HUGE_VAL) { // overflow is an error
c_add_source_message(NULL, 2, ErrorType_syntax, ErrorLevel_error, "Overflow: \%s cannot be represented by a double on this machine", (const char **)&chars, 1, $start->line, $start->charPosition+1, LT(1)->line, LT(1)->charPosition+1, ModelicaParser_readonly, ModelicaParser_filename_C_testsuiteFriendly);
$ast = Absyn__REAL(mmc_mk_scon(chars));
ModelicaParser_lexerError = ANTLR3_TRUE;
Expand Down
Expand Up @@ -101,7 +101,7 @@ modelica_real nobox_stringReal(threadData_t *threadData,metamodelica_string s)
MMC_CHECK_STRING(s);
errno = 0;
res = om_strtod(str,&endptr);
if (errno != 0 || str == endptr)
if ((errno != 0 && (res == 0 || res > DBL_MIN)) || str == endptr)
MMC_THROW_INTERNAL();
if (*endptr != '\0')
MMC_THROW_INTERNAL();
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/parser/Makefile
Expand Up @@ -43,6 +43,7 @@ ParseString.mos \
PureImpure.mo \
PureImpure2.mo \
PureImpure3.mo \
RealLiterals1.mo \
RealOpLexerModelica.mo \
Redeclare.mos \
ReloadClass.mos \
Expand Down
25 changes: 25 additions & 0 deletions testsuite/openmodelica/parser/RealLiterals1.mo
@@ -0,0 +1,25 @@
// name: RealLiterals1
// keywords:
// status: correct
// cflags: -d=-newInst
//
//

model RealLiterals1
Real x_min1 = -4.940656458412465e-324;
Real x_min2 = 4.940656458412465e-324;
Real x_underflow1 = -4.9e-325;
Real x_underflow2 = 4.9e-325;
end RealLiterals1;

// Result:
// [openmodelica/parser/RealLiterals1.mo:11:24-11:32:writable] Warning: Underflow: 4.9e-325 cannot be represented by a double on this machine. It will be converted to 0.0.
// [openmodelica/parser/RealLiterals1.mo:12:23-12:31:writable] Warning: Underflow: 4.9e-325 cannot be represented by a double on this machine. It will be converted to 0.0.
//
// class RealLiterals1
// Real x_min1 = -4.940656458412465e-324;
// Real x_min2 = 4.940656458412465e-324;
// Real x_underflow1 = -0.0;
// Real x_underflow2 = 0.0;
// end RealLiterals1;
// endResult

0 comments on commit 65de200

Please sign in to comment.