{"record":{"id":"e87af6a78c6c7b18","repo":"Tencent/matrix","slug":"couldn-t-read-number","errorCode":null,"errorMessage":"Couldn't read number!","messagePattern":"Couldn't read number!","errorType":"exception","errorClass":"ProcStatUtil.ParseException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/utils/ProcStatReader.java","lineNumber":175,"sourceCode":"\n        buffer.flip();\n        return buffer;\n    }\n\n    public long readNumber() {\n        long sign = 1;\n        long result = 0;\n        boolean isFirstRun = true;\n\n        while (hasNext()) {\n            next();\n            if (Character.isDigit(mChar)) {\n                result = result * 10 + (mChar - '0');\n            } else if (isFirstRun) {\n                if (mChar == '-') {\n                    sign = -1;\n                } else {\n                    throw new ParseException(\"Couldn't read number!\");\n                }\n            } else {\n                rewind();\n                break;\n            }\n\n            isFirstRun = false;\n        }\n\n        if (isFirstRun) {\n            throw new ParseException(\"Couldn't read number because the file ended!\");\n        }\n\n        return sign * result;\n    }\n\n    public CharBuffer readToSymbol(char symbol, CharBuffer buffer) {\n        buffer.clear();","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/utils/ProcStatReader.java#L157-L193","documentation":"ProcStatReader.readNumber() accumulates digits into a number; if the first character is neither a digit nor '-' it cannot parse a numeric token and throws ParseException(\"Couldn't read number!\"). Called by jiffies(), it means the expected utime/stime numeric field did not start with a number.","triggerScenarios":"Calling jiffies() when the current position lands on a non-digit, non-'-' character on the first iteration - e.g. field offsets shifted by an unusual comm value or an unexpected stat layout.","commonSituations":"Kernel variations or emulators where field ordering differs; a prior mis-parse of the parenthesized process name (comm) leaving the reader positioned inside text.","solutions":["Catch ParseException and skip this sampling round, then retry next tick.","Verify field alignment: ensure comm (in parentheses, possibly containing spaces/digits) is consumed correctly before reading utime/stime.","Use ProcStatUtil.parseWithBuffer which tokenizes from the raw byte array and handles the comm field explicitly.","Sanity-check the raw /proc/<pid>/stat line on the failing device."],"exampleFix":"// before\nlong jiffies = ProcStatUtil.instance.jiffies(pid, true);\n// after\nLong jiffies = null;\ntry {\n    jiffies = ProcStatUtil.instance.jiffies(pid, true);\n} catch (ProcStatUtil.ParseException e) {\n    // skip malformed sample\n}","handlingStrategy":"try-catch","validationCode":"// verify stat line field alignment after comm\nString line = readStatLine(pid);\nint close = line.lastIndexOf(')');\nString[] rest = line.substring(close + 2).trim().split(\"\\\\s+\");\nboolean ok = rest.length >= 2 && rest[11].matches(\"\\\\d+\"); // utime at index 11 after state","typeGuard":null,"tryCatchPattern":"try { long j = ProcStatUtil.instance.jiffies(pid, true); } catch (ProcStatUtil.ParseException e) { j = -1; /* skip */ }","preventionTips":["Ensure comm is parsed via parentheses, not whitespace splitting","Validate one stat line format per device before enabling sampling","Catch ParseException per-pid inside the sampling loop","Update Matrix if stat layout errors recur on specific kernels"],"tags":["android","proc-stat","parser","jiffies"],"backgroundTag":"file-read-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}