]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 3164: Total memory info display 32-bit overflows
authorAmos Jeffries <squid3@treenet.co.nz>
Sun, 27 Feb 2011 09:44:25 +0000 (22:44 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Sun, 27 Feb 2011 09:44:25 +0000 (22:44 +1300)
Use double instead of long. This removes any Squid-caused overflow wraps.
Overflows may still appear since the underlying library is 32-bit.

include/util.h
lib/stub_memaccount.c
lib/util.c
src/stat.cc

index 0bf570b4c272bc99b2a012f74441aeb1f315c56e..85be84e35c9311fd53137e9f80d4924c9d2e3c35 100644 (file)
@@ -150,7 +150,7 @@ extern void gb_flush(gb_t *);  /* internal, do not use this */
 /*
  * Returns the amount of known allocated memory
  */
-int statMemoryAccounted(void);
+double statMemoryAccounted(void);
 
 /* Windows Port */
 /* win32lib.c */
index c49afc4a98319f15aa12746d5f9bcc7b8f47d42d..49b0a84bf2d33d719ad4863c8447d6eedb4841df 100644 (file)
@@ -5,8 +5,8 @@
 /* Stub function for programs not implementing statMemoryAccounted */
 #include "config.h"
 #include "util.h"
-int
+double
 statMemoryAccounted(void)
 {
-    return -1;
+    return -1.0;
 }
index 2cee8433b823f9f7ab0eeaf830165d4cc653630c..c3196cc6bd5cc19a740c5da32f9bb1d1bd3a337a 100644 (file)
@@ -365,9 +365,9 @@ malloc_number(void *p)
 static void
 xmalloc_show_trace(void *p, int sign)
 {
-    int statMemoryAccounted();
-    static size_t last_total = 0, last_accounted = 0, last_mallinfo = 0;
-    size_t accounted = statMemoryAccounted();
+    static double last_accounted = 0;
+    static size_t last_total = 0, last_mallinfo = 0;
+    double accounted = statMemoryAccounted();
     size_t mi = 0;
     size_t sz;
 #if HAVE_MALLINFO
@@ -381,10 +381,10 @@ xmalloc_show_trace(void *p, int sign)
     xmalloc_count += sign > 0;
 
     if (xmalloc_trace) {
-        fprintf(stderr, "%c%8p size=%5d/%d acc=%5d/%d mallinfo=%5d/%d %s:%d %s",
+        fprintf(stderr, "%c%8p size=%5d/%d acc=%5.0f/%.0f mallinfo=%5d/%d %s:%d %s",
                 sign > 0 ? '+' : '-', p,
                 (int) xmalloc_total - last_total, (int) xmalloc_total,
-                (int) accounted - last_accounted, (int) accounted,
+                accounted - last_accounted, accounted,
                 (int) mi - last_mallinfo, (int) mi,
                 xmalloc_file, xmalloc_line, xmalloc_func);
 
index 83af8ca517e9d3f1b22ecc61a1cab7f2ea6675ac..a68b88313731226db855b3167e804ec2035b6679 100644 (file)
@@ -445,14 +445,6 @@ info_get(StoreEntry * sentry)
     struct rusage rusage;
     double cputime;
     double runtime;
-#if HAVE_MSTATS && HAVE_GNUMALLOC_H
-
-    struct mstats ms;
-#elif HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
-
-    struct mallinfo mp;
-    long t;
-#endif
 
     runtime = tvSubDsec(squid_start, current_time);
 
@@ -624,72 +616,73 @@ info_get(StoreEntry * sentry)
 
 #if HAVE_MSTATS && HAVE_GNUMALLOC_H
 
-    ms = mstats();
+
+    struct mstats ms = mstats();
 
     storeAppendPrintf(sentry, "Memory usage for %s via mstats():\n",APP_SHORTNAME);
 
-    storeAppendPrintf(sentry, "\tTotal space in arena:  %6ld KB\n",
-                      (long)(ms.bytes_total >> 10));
+    storeAppendPrintf(sentry, "\tTotal space in arena:  %6.0f KB\n",
+                      static_cast<double>(ms.bytes_total / 1024));
 
-    storeAppendPrintf(sentry, "\tTotal free:            %6ld KB %d%%\n",
-                      (long)(ms.bytes_free >> 10), Math::intPercent(ms.bytes_free, ms.bytes_total));
+    storeAppendPrintf(sentry, "\tTotal free:            %6.0f KB %.0f%%\n",
+                      static_cast<double>(ms.bytes_free / 1024),
+                      Math::doublePercent(static_cast<double>(ms.bytes_free), static_cast<double>(ms.bytes_total)));
 
 #elif HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
 
-    mp = mallinfo();
+    struct mallinfo mp = mallinfo();
 
     storeAppendPrintf(sentry, "Memory usage for %s via mallinfo():\n",APP_SHORTNAME);
 
-    storeAppendPrintf(sentry, "\tTotal space in arena:  %6ld KB\n",
-                      (long)(mp.arena >> 10));
+    storeAppendPrintf(sentry, "\tTotal space in arena:  %6.0f KB\n",
+                      static_cast<double>(mp.arena / 1024));
 
-    storeAppendPrintf(sentry, "\tOrdinary blocks:       %6ld KB %6ld blks\n",
-                      (long)(mp.uordblks >> 10), (long)mp.ordblks);
+    storeAppendPrintf(sentry, "\tOrdinary blocks:       %6.0f KB %6.0f blks\n",
+                      static_cast<double>(mp.uordblks / 1024), static_cast<double>(mp.ordblks));
 
-    storeAppendPrintf(sentry, "\tSmall blocks:          %6ld KB %6ld blks\n",
-                      (long)(mp.usmblks >> 10), (long)mp.smblks);
+    storeAppendPrintf(sentry, "\tSmall blocks:          %6.0f KB %6.0f blks\n",
+                      static_cast<double>(mp.usmblks / 1024), static_cast<double>(mp.smblks));
 
-    storeAppendPrintf(sentry, "\tHolding blocks:        %6ld KB %6ld blks\n",
-                      (long)(mp.hblkhd >> 10), (long)mp.hblks);
+    storeAppendPrintf(sentry, "\tHolding blocks:        %6.0f KB %6.0f blks\n",
+                      static_cast<double>(mp.hblkhd / 1024), static_cast<double>(mp.hblks));
 
-    storeAppendPrintf(sentry, "\tFree Small blocks:     %6ld KB\n",
-                      (long)(mp.fsmblks >> 10));
+    storeAppendPrintf(sentry, "\tFree Small blocks:     %6.0f KB\n",
+                      static_cast<double>(mp.fsmblks / 1024));
 
-    storeAppendPrintf(sentry, "\tFree Ordinary blocks:  %6ld KB\n",
-                      (long)(mp.fordblks >> 10));
+    storeAppendPrintf(sentry, "\tFree Ordinary blocks:  %6.0f KB\n",
+                      static_cast<double>(mp.fordblks / 1024));
 
-    t = mp.uordblks + mp.usmblks + mp.hblkhd;
+    double t = mp.uordblks + mp.usmblks + mp.hblkhd;
 
-    storeAppendPrintf(sentry, "\tTotal in use:          %6ld KB %d%%\n",
-                      (long)(t >> 10), Math::intPercent(t, mp.arena + mp.hblkhd));
+    storeAppendPrintf(sentry, "\tTotal in use:          %6.0f KB %.0f%%\n",
+                      (t / 1024), Math::doublePercent(t, static_cast<double>(mp.arena + mp.hblkhd)));
 
     t = mp.fsmblks + mp.fordblks;
 
-    storeAppendPrintf(sentry, "\tTotal free:            %6ld KB %d%%\n",
-                      (long)(t >> 10), Math::intPercent(t, mp.arena + mp.hblkhd));
+    storeAppendPrintf(sentry, "\tTotal free:            %6.0f KB %.0f%%\n",
+                      (t / 1024), Math::doublePercent(t, static_cast<double>(mp.arena + mp.hblkhd)));
 
     t = mp.arena + mp.hblkhd;
 
-    storeAppendPrintf(sentry, "\tTotal size:            %6ld KB\n",
-                      (long)(t >> 10));
+    storeAppendPrintf(sentry, "\tTotal size:            %6.0f KB\n", (t / 1024));
 
 #if HAVE_STRUCT_MALLINFO_MXFAST
 
-    storeAppendPrintf(sentry, "\tmax size of small blocks:\t%d\n", mp.mxfast);
+    storeAppendPrintf(sentry, "\tmax size of small blocks:\t%.0f\n", static_cast<double>(mp.mxfast));
 
-    storeAppendPrintf(sentry, "\tnumber of small blocks in a holding block:\t%ld\n",
-                      (long)mp.nlblks);
+    storeAppendPrintf(sentry, "\tnumber of small blocks in a holding block:\t%6.0f\n",
+                      static_cast<double>(mp.nlblks));
 
-    storeAppendPrintf(sentry, "\tsmall block rounding factor:\t%ld\n", (long)mp.grain);
+    storeAppendPrintf(sentry, "\tsmall block rounding factor:\t%.0f\n", static_cast<double>(mp.grain));
 
-    storeAppendPrintf(sentry, "\tspace (including overhead) allocated in ord. blks:\t%ld\n",
-                      (long)mp.uordbytes);
+    storeAppendPrintf(sentry, "\tspace (including overhead) allocated in ord. blks:\t%.0f\n",
+                      static_cast<double>(mp.uordbytes));
 
-    storeAppendPrintf(sentry, "\tnumber of ordinary blocks allocated:\t%ld\n",
-                      (long)mp.allocated);
+    storeAppendPrintf(sentry, "\tnumber of ordinary blocks allocated:\t%.0f\n",
+                      static_cast<double>(mp.allocated));
 
-    storeAppendPrintf(sentry, "\tbytes used in maintaining the free tree:\t%ld\n",
-                      (long)mp.treeoverhead);
+    storeAppendPrintf(sentry, "\tbytes used in maintaining the free tree:\t%.0f\n",
+                      static_cast<double>(mp.treeoverhead));
 
 #endif /* HAVE_STRUCT_MALLINFO_MXFAST */
 #endif /* HAVE_MALLINFO */
@@ -698,13 +691,13 @@ info_get(StoreEntry * sentry)
 
 #if !(HAVE_MSTATS && HAVE_GNUMALLOC_H) && HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
 
-    storeAppendPrintf(sentry, "\tTotal accounted:       %6ld KB %3d%%\n",
-                      (long)(statMemoryAccounted() >> 10), Math::intPercent(statMemoryAccounted(), t));
+    storeAppendPrintf(sentry, "\tTotal accounted:       %6.0f KB %.0f%%\n",
+                      (statMemoryAccounted() / 1024), Math::doublePercent(statMemoryAccounted(), t));
 
 #else
 
-    storeAppendPrintf(sentry, "\tTotal accounted:       %6ld KB\n",
-                      (long)(statMemoryAccounted() >> 10));
+    storeAppendPrintf(sentry, "\tTotal accounted:       %6.0f KB\n",
+                      (statMemoryAccounted() / 1024));
 
 #endif
     {
@@ -712,15 +705,15 @@ info_get(StoreEntry * sentry)
         memPoolGetGlobalStats(&mp_stats);
 #if !(HAVE_MSTATS && HAVE_GNUMALLOC_H) && HAVE_MALLINFO && HAVE_STRUCT_MALLINFO
 
-        storeAppendPrintf(sentry, "\tmemPool accounted:     %6ld KB %3d%%\n",
-                          (long)(mp_stats.TheMeter->alloc.level >> 10),
-                          Math::intPercent(mp_stats.TheMeter->alloc.level, t));
+        storeAppendPrintf(sentry, "\tmemPool accounted:     %6.0f KB %.0f%%\n",
+                          static_cast<double>(mp_stats.TheMeter->alloc.level / 1024),
+                          Math::doublePercent(static_cast<double>(mp_stats.TheMeter->alloc.level), t));
 
-        int iFree = 0;
+        double iFree = 0;
         if (t >= mp_stats.TheMeter->alloc.level)
-            iFree = Math::intPercent((t - mp_stats.TheMeter->alloc.level), t);
-        storeAppendPrintf(sentry, "\tmemPool unaccounted:   %6ld KB %3d%%\n",
-                          (long)((t - mp_stats.TheMeter->alloc.level) >> 10), iFree);
+            iFree = Math::doublePercent((t - static_cast<double>(mp_stats.TheMeter->alloc.level)), t);
+        storeAppendPrintf(sentry, "\tmemPool unaccounted:   %6.0f KB %.0f%%\n",
+                          static_cast<double>((t - mp_stats.TheMeter->alloc.level) / 1024), iFree);
 #endif
 
         storeAppendPrintf(sentry, "\tmemPoolAlloc calls: %9.0f\n",
@@ -1774,8 +1767,8 @@ statGraphDump(StoreEntry * e)
 
 #endif /* STAT_GRAPHS */
 
-int
+double
 statMemoryAccounted(void)
 {
-    return memPoolsTotalAllocated();
+    return static_cast<double>(memPoolsTotalAllocated());
 }