From ae53bf87d773e424066608ededb98de573048728 Mon Sep 17 00:00:00 2001 From: "wu@webrtc.org" Date: Tue, 23 Aug 2011 18:56:56 +0000 Subject: [PATCH] =?UTF-8?q?The=20variable=20=E2=80=98dummy=E2=80=99=20set?= =?UTF-8?q?=20but=20not=20used=20being=20treated=20as=20errors=20in=20Fedo?= =?UTF-8?q?ra.=20Review=20URL:=20http://webrtc-codereview.appspot.com/1220?= =?UTF-8?q?04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://webrtc.googlecode.com/svn/trunk@425 4adac7df-926f-26a2-2b94-8c16560cd09d --- src/system_wrappers/source/cpu_linux.cc | 39 ++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/system_wrappers/source/cpu_linux.cc b/src/system_wrappers/source/cpu_linux.cc index 1abaf124d..9d7d89de6 100644 --- a/src/system_wrappers/source/cpu_linux.cc +++ b/src/system_wrappers/source/cpu_linux.cc @@ -65,7 +65,8 @@ WebRtc_Word32 CpuLinux::CpuUsageMultiCore(WebRtc_UWord32& numCores, numCores = m_numCores; long long busy = 0; long long idle = 0; - GetData(busy, idle, m_busyArray, m_idleArray); + if (GetData(busy, idle, m_busyArray, m_idleArray) != 0) + return -1; long long deltaBusy = busy - m_oldBusyTime; long long deltaIdle = idle - m_oldIdleTime; @@ -116,18 +117,28 @@ int CpuLinux::GetData(long long& busy, long long& idle, long long*& busyArray, } char line[100]; - char* dummy = fgets(line, 100, fp); + if (fgets(line, 100, fp) == NULL) { + fclose(fp); + return -1; + } char firstWord[100]; - sscanf(line, "%s ", firstWord); - if(strncmp(firstWord, "cpu", 3)!=0) - { + if (sscanf(line, "%s ", firstWord) != 1) { + fclose(fp); + return -1; + } + if (strncmp(firstWord, "cpu", 3) != 0) { + fclose(fp); return -1; } char sUser[100]; char sNice[100]; char sSystem[100]; char sIdle[100]; - sscanf(line, "%s %s %s %s %s ", firstWord, sUser, sNice, sSystem, sIdle); + if (sscanf(line, "%s %s %s %s %s ", + firstWord, sUser, sNice, sSystem, sIdle) != 5) { + fclose(fp); + return -1; + } long long luser = atoll(sUser); long long lnice = atoll(sNice); long long lsystem = atoll(sSystem); @@ -137,9 +148,15 @@ int CpuLinux::GetData(long long& busy, long long& idle, long long*& busyArray, idle = lidle; for (WebRtc_UWord32 i = 0; i < m_numCores; i++) { - dummy = fgets(line, 100, fp); - sscanf(line, "%s %s %s %s %s ", firstWord, sUser, sNice, sSystem, - sIdle); + if (fgets(line, 100, fp) == NULL) { + fclose(fp); + return -1; + } + if (sscanf(line, "%s %s %s %s %s ", firstWord, sUser, sNice, sSystem, + sIdle) != 5) { + fclose(fp); + return -1; + } luser = atoll(sUser); lnice = atoll(sNice); lsystem = atoll(sSystem); @@ -171,7 +188,9 @@ int CpuLinux::GetNumCores() numCores++; if (fgets(line, 100, fp)) { - sscanf(line, "%s ", firstWord); + if (sscanf(line, "%s ", firstWord) != 1) { + firstWord[0] = '\0'; + } } else { break; }