snprintf in write_float.def
FX
fxcoudert@gmail.com
Sun May 11 11:40:00 GMT 2008
Hi all,
While reducing PR36200, I was looking at libgfortran/io/
write_float.def, and after having refreshed my memory about printf
formats, it seems to me that what we're doing in DTOA and DTOAL is
unsafe: the "e" format expects an argument of type double, and yet
when used for real(kind=4), we'll happily pass it a "float". So, am I
overlooking something or should we apply the patch below (which casts
values to "double" or "long double")? I suspect there is more to it
that I saw, because we'd see a large number of failures otherwise.
So, comments welcome.
FX
Index: io/write_float.def
===================================================================
--- io/write_float.def (revision 135088)
+++ io/write_float.def (working copy)
@@ -708,21 +708,21 @@ OUTPUT_FLOAT_FMT_G(16)
#define DTOA \
snprintf (buffer, size, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
- "e", ndigits - 1, tmp);
+ "e", ndigits - 1, (double) tmp);
#define DTOAL \
snprintf (buffer, size, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
- "Le", ndigits - 1, tmp);
+ "Le", ndigits - 1, (long double) tmp);
#else
#define DTOA \
sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
- "e", ndigits - 1, tmp);
+ "e", ndigits - 1, (double) tmp);
#define DTOAL \
sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
- "Le", ndigits - 1, tmp);
+ "Le", ndigits - 1, (long double) tmp);
#endif
--
François-Xavier Coudert
http://www.homepages.ucl.ac.uk/~uccafco/
More information about the Fortran
mailing list