Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 732ae95f authored by Adrian Roos's avatar Adrian Roos
Browse files

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
parent 7742a315
Loading
Loading
Loading
Loading
+24 −1
Original line number Original line Diff line number Diff line
@@ -345,7 +345,7 @@ public class ApplicationErrorReport implements Parcelable {
            PrintWriter pw = new FastPrintWriter(sw, false, 256);
            PrintWriter pw = new FastPrintWriter(sw, false, 256);
            tr.printStackTrace(pw);
            tr.printStackTrace(pw);
            pw.flush();
            pw.flush();
            stackTrace = sw.toString();
            stackTrace = sanitizeString(sw.toString());
            exceptionMessage = tr.getMessage();
            exceptionMessage = tr.getMessage();


            // Populate fields with the "root cause" exception
            // Populate fields with the "root cause" exception
@@ -374,6 +374,29 @@ public class ApplicationErrorReport implements Parcelable {
                throwMethodName = "unknown";
                throwMethodName = "unknown";
                throwLineNumber = 0;
                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;
        }
        }


        /**
        /**