From 390c0e19c98b7b97840549767934f59459bbd12d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 14 May 2014 12:40:21 +0000 Subject: [PATCH] Check result of fgets() and sscanf() in wxGetFreeMemory(). This fixes g++ -Wunused-result warnings and also actually makes the code more robust. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76530 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/unix/utilsunx.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index 9e7c15bb5c..9fe422c782 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -1185,22 +1185,29 @@ wxMemorySize wxGetFreeMemory() // /proc/meminfo changed its format in kernel 2.6 if ( wxPlatformInfo().CheckOSVersion(2, 6) ) { - unsigned long cached, buffers; - sscanf(buf, "MemFree: %ld", &memFree); + if ( sscanf(buf, "MemFree: %ld", &memFree) == 1 ) + { + // We consider memory used by the IO buffers and cache as + // being "free" too as Linux aggressively uses free memory + // for caching and the amount of memory reported as really + // free is far too low for lightly loaded system. + if ( fgets(buf, WXSIZEOF(buf), fp) ) + { + unsigned long buffers; + if ( sscanf(buf, "Buffers: %lu", &buffers) == 1 ) + memFree += buffers; + } - fgets(buf, WXSIZEOF(buf), fp); - sscanf(buf, "Buffers: %lu", &buffers); + if ( fgets(buf, WXSIZEOF(buf), fp) ) + { + unsigned long cached; + if ( sscanf(buf, "Cached: %lu", &cached) == 1 ) + memFree += cached; + } - fgets(buf, WXSIZEOF(buf), fp); - sscanf(buf, "Cached: %lu", &cached); - - // add to "MemFree" also the "Buffers" and "Cached" values as - // free(1) does as otherwise the value never makes sense: for - // kernel 2.6 it's always almost 0 - memFree += buffers + cached; - - // values here are always expressed in kB and we want bytes - memFree *= 1024; + // values here are always expressed in kB and we want bytes + memFree *= 1024; + } } else // Linux 2.4 (or < 2.6, anyhow) {