]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix floating point printing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 16 Apr 2019 13:04:28 +0000 (14:04 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 16 Apr 2019 13:04:28 +0000 (14:04 +0100)
contrib/fpconv/fpconv.c
contrib/fpconv/fpconv.h
src/libutil/printf.c

index 3fbd55af01b8ee0abf429ff50bcbbc4dc097c817..12c67cfa687c8b571620e1505e71e72ce319e31e 100644 (file)
@@ -99,7 +99,7 @@ static Fp multiply(Fp* a, Fp* b)
     uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask);
     uint64_t ah_bh = (a->frac >> 32)    * (b->frac >> 32);
 
-    uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32); 
+    uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32);
     /* round up */
     tmp += 1U << 31;
 
@@ -206,7 +206,8 @@ static int grisu2(double d, char* digits, int* K)
     return generate_digits(&w, &upper, &lower, digits, K);
 }
 
-static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
+static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg,
+               bool scientific)
 {
     int exp = absv(K + ndigits - 1);
 
@@ -219,7 +220,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
     }
 
     /* write decimal w/o scientific notation */
-    if(K < 0 && (K > -7 || exp < 4)) {
+    if(scientific && (K < 0 && (K > -7 || exp < 4))) {
         int offset = ndigits - absv(K);
         /* fp < 1.0 -> write leading zero */
         if(offset <= 0) {
@@ -304,7 +305,7 @@ static int filter_special(double fp, char* dest)
     return 3;
 }
 
-int fpconv_dtoa(double d, char dest[24])
+int fpconv_dtoa(double d, char dest[24], bool scientific)
 {
     char digits[18];
 
@@ -326,7 +327,7 @@ int fpconv_dtoa(double d, char dest[24])
     int K = 0;
     int ndigits = grisu2(d, digits, &K);
 
-    str_len += emit_digits(digits, ndigits, dest + str_len, K, neg);
+    str_len += emit_digits(digits, ndigits, dest + str_len, K, neg, scientific);
 
     return str_len;
 }
index 58bbcccbb1576ec471644e94f550afe7b76e5847..83180d41f54262811fd3e75b732608ab6bd04dbe 100644 (file)
@@ -26,7 +26,7 @@
  *
  */
 
-int fpconv_dtoa(double fp, char dest[24]);
+int fpconv_dtoa(double fp, char dest[24], bool scientific);
 
 #endif
 
index 9dc14441cf01c364cf31553d5a5e36ccffa67d1e..84dd5d27260a2d50e4f6c3e539d6f2e4d1af4fa3 100644 (file)
@@ -947,9 +947,8 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
 
                        case 'f':
-                       case 'g':
                                f = (gdouble) va_arg (args, double);
-                               slen = fpconv_dtoa (f, dtoabuf);
+                               slen = fpconv_dtoa (f, dtoabuf, false);
 
                                if (frac_width != 0) {
                                        const gchar *dot_pos = memchr (dtoabuf, '.', slen);
@@ -980,10 +979,16 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
                                continue;
 
+                       case 'g':
+                               f = (gdouble) va_arg (args, double);
+                               slen = fpconv_dtoa (f, dtoabuf, true);
+                               RSPAMD_PRINTF_APPEND (dtoabuf, slen);
+
+                               continue;
+
                        case 'F':
-                       case 'G':
                                f = (gdouble) va_arg (args, long double);
-                               slen = fpconv_dtoa (f, dtoabuf);
+                               slen = fpconv_dtoa (f, dtoabuf, false);
 
                                if (frac_width != 0) {
                                        const gchar *dot_pos = memchr (dtoabuf, '.', slen);
@@ -1014,6 +1019,13 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
                                continue;
 
+                       case 'G':
+                               f = (gdouble) va_arg (args, long double);
+                               slen = fpconv_dtoa (f, dtoabuf, true);
+                               RSPAMD_PRINTF_APPEND (dtoabuf, slen);
+
+                               continue;
+
                        case 'p':
                                ui64 = (uintptr_t) va_arg (args, void *);
                                hex = 2;