Merge pull request #2121 from bska/fix-fmtout-segfault
Don't Overrun Formatting Buffer in 'doub_string_ecl'
This commit is contained in:
commit
20be6aa6d9
@ -489,8 +489,8 @@ std::string EclOutput::make_real_string_ix(float value) const
|
|||||||
|
|
||||||
std::string EclOutput::make_doub_string_ecl(double value) const
|
std::string EclOutput::make_doub_string_ecl(double value) const
|
||||||
{
|
{
|
||||||
char buffer [21];
|
char buffer [21 + 1];
|
||||||
std::sprintf (buffer, "%19.13E", value);
|
std::snprintf (buffer, sizeof buffer, "%19.13E", value);
|
||||||
|
|
||||||
if (value == 0.0) {
|
if (value == 0.0) {
|
||||||
return "0.00000000000000D+00";
|
return "0.00000000000000D+00";
|
||||||
@ -507,22 +507,23 @@ std::string EclOutput::make_doub_string_ecl(double value) const
|
|||||||
|
|
||||||
std::string tmpstr(buffer);
|
std::string tmpstr(buffer);
|
||||||
int exp = value < 0.0 ? std::stoi(tmpstr.substr(17, 4)) : std::stoi(tmpstr.substr(16, 4));
|
int exp = value < 0.0 ? std::stoi(tmpstr.substr(17, 4)) : std::stoi(tmpstr.substr(16, 4));
|
||||||
|
const bool use_exp_char = (exp >= -100) && (exp < 99);
|
||||||
|
|
||||||
if (value < 0.0) {
|
if (value < 0.0) {
|
||||||
if (std::abs(exp) < 100) {
|
if (use_exp_char) {
|
||||||
tmpstr = "-0." + tmpstr.substr(1, 1) + tmpstr.substr(3, 13) + "D";
|
tmpstr = "-0." + tmpstr.substr(1, 1) + tmpstr.substr(3, 13) + "D";
|
||||||
} else {
|
} else {
|
||||||
tmpstr = "-0." + tmpstr.substr(1, 1) + tmpstr.substr(3, 13);
|
tmpstr = "-0." + tmpstr.substr(1, 1) + tmpstr.substr(3, 13);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (std::abs(exp) < 100) {
|
if (use_exp_char) {
|
||||||
tmpstr = "0." + tmpstr.substr(0, 1) + tmpstr.substr(2, 13) + "D";
|
tmpstr = "0." + tmpstr.substr(0, 1) + tmpstr.substr(2, 13) + "D";
|
||||||
} else {
|
} else {
|
||||||
tmpstr = "0." + tmpstr.substr(0, 1) + tmpstr.substr(2, 13);
|
tmpstr = "0." + tmpstr.substr(0, 1) + tmpstr.substr(2, 13);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sprintf(buffer, "%+03i", exp + 1);
|
std::snprintf(buffer, sizeof buffer, "%+03i", exp + 1);
|
||||||
tmpstr = tmpstr + buffer;
|
tmpstr = tmpstr + buffer;
|
||||||
return tmpstr;
|
return tmpstr;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user