summaryrefslogtreecommitdiff
path: root/user/mbuffer/clamp-32-bit-parameters.patch
diff options
context:
space:
mode:
authorZach van Rijn <me@zv.io>2022-11-15 19:43:24 +0000
committerZach van Rijn <me@zv.io>2022-11-15 20:08:12 +0000
commite11ed1ef79f380f40e14af8d45720f80fc172162 (patch)
tree50f648b0c2ebb4f8229b71c2f0623586b90b87fd /user/mbuffer/clamp-32-bit-parameters.patch
parent473ac780f15a7836df1c6f89a9a538174c252e9f (diff)
downloadpackages-e11ed1ef79f380f40e14af8d45720f80fc172162.tar.gz
packages-e11ed1ef79f380f40e14af8d45720f80fc172162.tar.bz2
packages-e11ed1ef79f380f40e14af8d45720f80fc172162.tar.xz
packages-e11ed1ef79f380f40e14af8d45720f80fc172162.zip
user/mbuffer: add patch to clamp 32-bit parameters. fixes #834.
The number of physical and available pages will of course be correct on real 32-bit systems, but mbuffer does not account for the case of when a 64-bit system with more memory is running with a 32-bit personality. In this scenario, dynamic detection of system memory limits yields values that are not safe for use in a 32-bit environment.
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);
+ }
+ }