printf-hook-builtin: Print NaN/Infinity floating point values as such
authorMartin Willi <martin@revosec.ch>
Fri, 27 Sep 2013 16:16:46 +0000 (18:16 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 11 Oct 2013 09:06:09 +0000 (11:06 +0200)
src/libstrongswan/tests/test_printf.c
src/libstrongswan/utils/printf_hook/printf_hook_builtin.c

index a7b19d9..6c15fbe 100644 (file)
@@ -16,6 +16,7 @@
 #include "test_suite.h"
 
 #include <errno.h>
+#include <math.h>
 
 static void verify(char *expected, char *format, ...)
 {
@@ -133,6 +134,19 @@ START_TEST(test_printf_float)
        verify("-1.12", "%.2f", -1.1249);
        verify("-1.124", "%.3f", -1.12351);
        verify("-1.123", "%.3f", -1.12349);
+
+#ifdef NAN
+       verify("nan", "%.3f", NAN);
+       verify("  nan", "%5.3f", NAN);
+       verify("NAN", "%.3F", NAN);
+       verify("NAN  ", "%-5.3F", NAN);
+#endif
+#ifdef INFINITY
+       verify("inf", "%.3f", INFINITY);
+       verify("-inf", "%.4f", -INFINITY);
+       verify("INF", "%.3F", INFINITY);
+       verify("-INF", "%.4F", -INFINITY);
+#endif
 }
 END_TEST
 
index a28ce7f..ec0a418 100644 (file)
@@ -923,9 +923,29 @@ int builtin_vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
                                                        }
                                                        is_double:
                                                        {
+                                                               double dval;
+
+                                                               dval = va_arg(ap, double);
+                                                               if (isinf(dval) == 1)
+                                                               {
+                                                                       sarg = flags & FL_UPPER ? "INF" : "inf";
+                                                                       slen = strlen(sarg);
+                                                                       goto is_string;
+                                                               }
+                                                               if (isinf(dval) == -1)
+                                                               {
+                                                                       sarg = flags & FL_UPPER ? "-INF" : "-inf";
+                                                                       slen = strlen(sarg);
+                                                                       goto is_string;
+                                                               }
+                                                               if (isnan(dval))
+                                                               {
+                                                                       sarg = flags & FL_UPPER ? "NAN" : "nan";
+                                                                       slen = strlen(sarg);
+                                                                       goto is_string;
+                                                               }
                                                                sz = format_double(q, (o < n) ? n - o : 0,
-                                                                                                  va_arg(ap, double),
-                                                                                                  flags, base, width, prec);
+                                                                                               dval, flags, base, width, prec);
                                                                q += sz;
                                                                o += sz;
                                                                break;