kernel32: Use unsigned types in GlobalMemoryStatusEx to prevent overflows.

This commit is contained in:
Alexandre Julliard 2006-06-15 11:38:20 +02:00
parent 1ec3e9fe7d
commit 3df59e4fee
1 changed files with 26 additions and 39 deletions

View File

@ -1172,12 +1172,14 @@ BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
#ifdef linux #ifdef linux
FILE *f; FILE *f;
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
int *tmp, size_sys, mib[2]; unsigned long val;
int size_sys, mib[2];
#elif defined(__APPLE__) #elif defined(__APPLE__)
int *tmp, mib[2]; unsigned int val;
int mib[2];
size_t size_sys; size_t size_sys;
#elif defined(sun) #elif defined(sun)
long pagesize,maxpages,freepages,swapspace,swapfree; unsigned long pagesize,maxpages,freepages,swapspace,swapfree;
struct anoninfo swapinf; struct anoninfo swapinf;
int rval; int rval;
#endif #endif
@ -1200,36 +1202,37 @@ BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
if (f) if (f)
{ {
char buffer[256]; char buffer[256];
int total, used, free, shared, buffers, cached; unsigned long total, used, free, shared, buffers, cached;
lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0; lpmemex->ullTotalPhys = lpmemex->ullAvailPhys = 0;
lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0; lpmemex->ullTotalPageFile = lpmemex->ullAvailPageFile = 0;
while (fgets( buffer, sizeof(buffer), f )) while (fgets( buffer, sizeof(buffer), f ))
{ {
/* old style /proc/meminfo ... */ /* old style /proc/meminfo ... */
if (sscanf( buffer, "Mem: %d %d %d %d %d %d", &total, &used, &free, &shared, &buffers, &cached )) if (sscanf( buffer, "Mem: %lu %lu %lu %lu %lu %lu",
&total, &used, &free, &shared, &buffers, &cached ))
{ {
lpmemex->ullTotalPhys += total; lpmemex->ullTotalPhys += total;
lpmemex->ullAvailPhys += free + buffers + cached; lpmemex->ullAvailPhys += free + buffers + cached;
} }
if (sscanf( buffer, "Swap: %d %d %d", &total, &used, &free )) if (sscanf( buffer, "Swap: %lu %lu %lu", &total, &used, &free ))
{ {
lpmemex->ullTotalPageFile += total; lpmemex->ullTotalPageFile += total;
lpmemex->ullAvailPageFile += free; lpmemex->ullAvailPageFile += free;
} }
/* new style /proc/meminfo ... */ /* new style /proc/meminfo ... */
if (sscanf(buffer, "MemTotal: %d", &total)) if (sscanf(buffer, "MemTotal: %lu", &total))
lpmemex->ullTotalPhys = total*1024; lpmemex->ullTotalPhys = total*1024;
if (sscanf(buffer, "MemFree: %d", &free)) if (sscanf(buffer, "MemFree: %lu", &free))
lpmemex->ullAvailPhys = free*1024; lpmemex->ullAvailPhys = free*1024;
if (sscanf(buffer, "SwapTotal: %d", &total)) if (sscanf(buffer, "SwapTotal: %lu", &total))
lpmemex->ullTotalPageFile = total*1024; lpmemex->ullTotalPageFile = total*1024;
if (sscanf(buffer, "SwapFree: %d", &free)) if (sscanf(buffer, "SwapFree: %lu", &free))
lpmemex->ullAvailPageFile = free*1024; lpmemex->ullAvailPageFile = free*1024;
if (sscanf(buffer, "Buffers: %d", &buffers)) if (sscanf(buffer, "Buffers: %lu", &buffers))
lpmemex->ullAvailPhys += buffers*1024; lpmemex->ullAvailPhys += buffers*1024;
if (sscanf(buffer, "Cached: %d", &cached)) if (sscanf(buffer, "Cached: %lu", &cached))
lpmemex->ullAvailPhys += cached*1024; lpmemex->ullAvailPhys += cached*1024;
} }
fclose( f ); fclose( f );
@ -1245,33 +1248,17 @@ BOOL WINAPI GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpmemex )
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__) #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__APPLE__)
mib[0] = CTL_HW; mib[0] = CTL_HW;
mib[1] = HW_PHYSMEM; mib[1] = HW_PHYSMEM;
sysctl(mib, 2, NULL, &size_sys, NULL, 0); size_sys = sizeof(val);
tmp = malloc(size_sys * sizeof(int)); sysctl(mib, 2, &val, &size_sys, NULL, 0);
sysctl(mib, 2, tmp, &size_sys, NULL, 0); if (val) lpmemex->ullTotalPhys = val;
if (tmp && *tmp)
{
lpmemex->ullTotalPhys = *tmp;
free(tmp);
mib[1] = HW_USERMEM; mib[1] = HW_USERMEM;
sysctl(mib, 2, NULL, &size_sys, NULL, 0); size_sys = sizeof(val);
tmp = malloc(size_sys * sizeof(int)); sysctl(mib, 2, &val, &size_sys, NULL, 0);
sysctl(mib, 2, tmp, &size_sys, NULL, 0); if (!val) val = lpmemex->ullTotalPhys;
if (tmp && *tmp) lpmemex->ullAvailPhys = val;
{ lpmemex->ullTotalPageFile = val;
lpmemex->ullAvailPhys = *tmp; lpmemex->ullAvailPageFile = val;
lpmemex->ullTotalPageFile = *tmp;
lpmemex->ullAvailPageFile = *tmp;
lpmemex->dwMemoryLoad = lpmemex->ullTotalPhys - lpmemex->ullAvailPhys; lpmemex->dwMemoryLoad = lpmemex->ullTotalPhys - lpmemex->ullAvailPhys;
} else
{
lpmemex->ullAvailPhys = lpmemex->ullTotalPhys;
lpmemex->ullTotalPageFile = lpmemex->ullTotalPhys;
lpmemex->ullAvailPageFile = lpmemex->ullTotalPhys;
lpmemex->dwMemoryLoad = 0;
}
free(tmp);
}
#elif defined ( sun ) #elif defined ( sun )
pagesize=sysconf(_SC_PAGESIZE); pagesize=sysconf(_SC_PAGESIZE);
maxpages=sysconf(_SC_PHYS_PAGES); maxpages=sysconf(_SC_PHYS_PAGES);