summaryrefslogtreecommitdiff
path: root/user/mbuffer/clamp-32-bit-parameters.patch
diff options
context:
space:
mode:
Diffstat (limited to 'user/mbuffer/clamp-32-bit-parameters.patch')
-rw-r--r--user/mbuffer/clamp-32-bit-parameters.patch39
1 files changed, 39 insertions, 0 deletions
diff --git a/user/mbuffer/clamp-32-bit-parameters.patch b/user/mbuffer/clamp-32-bit-parameters.patch
new file mode 100644
index 000000000..3dc2d0eac
--- /dev/null
+++ b/user/mbuffer/clamp-32-bit-parameters.patch
@@ -0,0 +1,39 @@
+In addition to clamping NumP and AvP, this patch reorders
+shift operations to prevent possible overflow when given
+values that are found in some environments where a 64-bit
+system is running with a 32-bit personality.
+
+It should not be construed as fixing any bug in mbuffer.
+
+--- a/mbuffer.c
++++ b/mbuffer.c
+@@ -984,7 +984,14 @@
+
+ /* get physical memory size */
+ #if defined(_SC_PHYS_PAGES)
+- NumP = sysconf(_SC_PHYS_PAGES);
++ if (sizeof(void *) == 4)
++ {
++ NumP = (unsigned)-1 / PgSz; /* 4GB worth of pages */
++ }
++ else
++ {
++ NumP = sysconf(_SC_PHYS_PAGES);
++ }
+ if (NumP < 0) {
+ warningmsg("unable to determine number of total memory pages: %s\n",strerror(errno));
+ NumP = 0;
+@@ -1004,8 +1011,12 @@
+ char *at = strstr(tmp,"MemAvailable:");
+ if (at) {
+ AvP = strtol(at+13,0,0);
+- AvP <<= 10;
+ AvP /= PgSz;
++ AvP <<= 10;
++ if (sizeof(void *) == 4 && AvP > NumP)
++ {
++ AvP = NumP;
++ }
+ debugmsg("available memory: %lu pages\n",AvP);
+ }
+ }