Skip to content

Commit 52b8297

Browse files
committed
Avoid possible output of "-nan" in XML format.
Avoid possible output of "-nan" in XML format in order to be consistent throughout the toolkit and to avoid problems when reading such data. This closes DCMTK Bug #1153. Thanks to Mathieu Malaterre <[email protected]> for the report and sample instructions to reproduce this issue.
1 parent ed85991 commit 52b8297

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

dcmdata/libsrc/dcvrod.cc

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "dcmtk/ofstd/ofuuid.h"
2626
#include "dcmtk/ofstd/ofstd.h"
27+
#include "dcmtk/ofstd/ofmath.h"
2728

2829
#include "dcmtk/dcmdata/dcvrod.h"
2930
#include "dcmtk/dcmdata/dcswap.h"
@@ -95,6 +96,15 @@ unsigned long DcmOtherDouble::getVM()
9596

9697
// ********************************
9798

99+
/* need to check for "Not a Number" in order to avoid possible output of "-nan" */
100+
static inline void checkAndOutputFloatValue(STD_NAMESPACE ostream &out,
101+
const Float64 value)
102+
{
103+
if (OFMath::isnan(value))
104+
out << "nan";
105+
else
106+
out << value;
107+
}
98108

99109
OFCondition DcmOtherDouble::writeXML(STD_NAMESPACE ostream &out,
100110
const size_t flags)
@@ -143,9 +153,12 @@ OFCondition DcmOtherDouble::writeXML(STD_NAMESPACE ostream &out,
143153
/* use the standard "C" locale for proper decimal point */
144154
const STD_NAMESPACE locale oldLocale = out.imbue(STD_NAMESPACE locale("C"));
145155
/* print float values with separators */
146-
out << (*(floatValues++));
156+
checkAndOutputFloatValue(out, *(floatValues++));
147157
for (unsigned long i = 1; i < count; i++)
148-
out << "\\" << (*(floatValues++));
158+
{
159+
out << "\\";
160+
checkAndOutputFloatValue(out, *(floatValues++));
161+
}
149162
/* reset i/o manipulators and locale */
150163
out.precision(oldPrecision);
151164
out.imbue(oldLocale);

dcmdata/libsrc/dcvrof.cc

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "dcmtk/ofstd/ofuuid.h"
2626
#include "dcmtk/ofstd/ofstd.h"
27+
#include "dcmtk/ofstd/ofmath.h"
2728

2829
#include "dcmtk/dcmdata/dcjson.h"
2930
#include "dcmtk/dcmdata/dcvrof.h"
@@ -95,6 +96,15 @@ unsigned long DcmOtherFloat::getVM()
9596

9697
// ********************************
9798

99+
/* need to check for "Not a Number" in order to avoid possible output of "-nan" */
100+
static inline void checkAndOutputFloatValue(STD_NAMESPACE ostream &out,
101+
const Float32 value)
102+
{
103+
if (OFMath::isnan(value))
104+
out << "nan";
105+
else
106+
out << value;
107+
}
98108

99109
OFCondition DcmOtherFloat::writeXML(STD_NAMESPACE ostream &out,
100110
const size_t flags)
@@ -143,9 +153,12 @@ OFCondition DcmOtherFloat::writeXML(STD_NAMESPACE ostream &out,
143153
/* use the standard "C" locale for proper decimal point */
144154
const STD_NAMESPACE locale oldLocale = out.imbue(STD_NAMESPACE locale("C"));
145155
/* print float values with separators */
146-
out << (*(floatValues++));
156+
checkAndOutputFloatValue(out, *(floatValues++));
147157
for (unsigned long i = 1; i < count; i++)
148-
out << "\\" << (*(floatValues++));
158+
{
159+
out << "\\";
160+
checkAndOutputFloatValue(out, *(floatValues++));
161+
}
149162
/* reset i/o manipulators and locale */
150163
out.precision(oldPrecision);
151164
out.imbue(oldLocale);

dcmsr/libsrc/dsrnumvl.cc

+4-8
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,10 @@ OFCondition DSRNumericMeasurementValue::writeXML(STD_NAMESPACE ostream &stream,
238238
stream << "<float>";
239239
if (hasFloating)
240240
{
241-
/* increase default precision */
242-
const STD_NAMESPACE streamsize oldPrecision = stream.precision(17);
243-
/* use the standard "C" locale for proper decimal point */
244-
const STD_NAMESPACE locale oldLocale = stream.imbue(STD_NAMESPACE locale("C"));
245-
stream << floatValue;
246-
/* reset i/o manipulators and locale */
247-
stream.precision(oldPrecision);
248-
stream.imbue(oldLocale);
241+
char buffer[64];
242+
/* need to convert float to avoid problems with decimal point and "-nan" */
243+
OFStandard::ftoa(buffer, sizeof(buffer), floatValue, 0, 0, -2 /* print enough digits to permit lossless conversion back to FD */);
244+
stream << buffer;
249245
}
250246
stream << "</float>" << OFendl;
251247
}

0 commit comments

Comments
 (0)