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 330507 5b37f138e5bfdb84fe3460dd115ec225933e0f5f
parent 330506 60ea379dd10231fd7970b7244b656ccd0af0b6a6
child 330508 039b504d1161b427ab475451bc805da8cd014b4c
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [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;