From 732ae95fbf5eb255d3384a36af51cde972a4eaad Mon Sep 17 00:00:00 2001 From: Adrian Roos Date: Fri, 1 Jul 2016 13:11:18 -0700 Subject: [PATCH] ApplicationErrorReport: Sanitize stack trace Makes sure that the size of the stack trace does not exceed 40 KiBi, to ensure that even errors with large stack traces are properly reported to ActivityManagerService. For good measure, also ensure that the exception message is not too long either. Change-Id: Id8a99094023c15a981b1b79fd9ee0d803921e220 Fixes: 29918978 --- .../android/app/ApplicationErrorReport.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/core/java/android/app/ApplicationErrorReport.java b/core/java/android/app/ApplicationErrorReport.java index 8692336439f91..9fa8a5d2faee0 100644 --- a/core/java/android/app/ApplicationErrorReport.java +++ b/core/java/android/app/ApplicationErrorReport.java @@ -345,7 +345,7 @@ public class ApplicationErrorReport implements Parcelable { PrintWriter pw = new FastPrintWriter(sw, false, 256); tr.printStackTrace(pw); pw.flush(); - stackTrace = sw.toString(); + stackTrace = sanitizeString(sw.toString()); exceptionMessage = tr.getMessage(); // Populate fields with the "root cause" exception @@ -374,6 +374,29 @@ public class ApplicationErrorReport implements Parcelable { throwMethodName = "unknown"; throwLineNumber = 0; } + + exceptionMessage = sanitizeString(exceptionMessage); + } + + /** + * Ensure that the string is of reasonable size, truncating from the middle if needed. + */ + private String sanitizeString(String s) { + int prefixLength = 10 * 1024; + int suffixLength = 10 * 1024; + int acceptableLength = prefixLength + suffixLength; + + if (s != null && s.length() > acceptableLength) { + String replacement = + "\n[TRUNCATED " + (s.length() - acceptableLength) + " CHARS]\n"; + + StringBuilder sb = new StringBuilder(acceptableLength + replacement.length()); + sb.append(s.substring(0, prefixLength)); + sb.append(replacement); + sb.append(s.substring(s.length() - suffixLength)); + return sb.toString(); + } + return s; } /**