summaryrefslogblamecommitdiff
path: root/user/mbuffer/clamp-32-bit-parameters.patch
blob: 3dc2d0eac7376015b637bf07da4ae6672ebf77ad (plain) (tree)






































                                                                                                     
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);
 			}
 		}