Bug 1239376 - Fix StrictMode thread read access in SysInfo.getMemSize. r=mfinkle
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 27 Jan 2016 12:06:33 -0800
changeset 292636 5b37f138e5bfdb84fe3460dd115ec225933e0f5f
parent 292635 60ea379dd10231fd7970b7244b656ccd0af0b6a6
child 292637 039b504d1161b427ab475451bc805da8cd014b4c
push id30165
push usercbook@mozilla.com
push dateTue, 12 Apr 2016 11:43:19 +0000
treeherdermozilla-central@711691554872 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs1239376
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1239376 - Fix StrictMode thread read access in SysInfo.getMemSize. r=mfinkle This caused a StrictMode violation on startup. MozReview-Commit-ID: Ip2ywUoexOh
mobile/android/base/java/org/mozilla/gecko/SysInfo.java
--- a/mobile/android/base/java/org/mozilla/gecko/SysInfo.java
+++ b/mobile/android/base/java/org/mozilla/gecko/SysInfo.java
@@ -138,16 +138,19 @@ public final class SysInfo {
      */
     public static int getMemSize() {
         if (totalRAM >= 0) {
             return totalRAM;
         }
 
         // This is the string "MemTotal" that we're searching for in the buffer.
         final byte[] MEMTOTAL = {'M', 'e', 'm', 'T', 'o', 't', 'a', 'l'};
+
+        // `/proc/meminfo` is not a real file and thus safe to read on the main thread.
+        final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
         try {
             final byte[] buffer = new byte[MEMINFO_BUFFER_SIZE_BYTES];
             final FileInputStream is = new FileInputStream("/proc/meminfo");
             try {
                 final int length = is.read(buffer);
 
                 for (int i = 0; i < length; i++) {
                     if (matchMemText(buffer, i, length, MEMTOTAL)) {
@@ -162,16 +165,18 @@ public final class SysInfo {
             }
 
             Log.w(LOG_TAG, "Did not find MemTotal line in /proc/meminfo.");
             return totalRAM = 0;
         } catch (FileNotFoundException f) {
             return totalRAM = 0;
         } catch (IOException e) {
             return totalRAM = 0;
+        } finally {
+            StrictMode.setThreadPolicy(savedPolicy);
         }
     }
 
     /**
      * @return the SDK version supported by this device, such as '16'.
      */
     public static int getVersion() {
         return android.os.Build.VERSION.SDK_INT;