mac: Don’t try to demangle non-C++ symbols with the C++ demangler

On Mac a C++ symbol has 1-4 underscore characters followed by a 'Z'.
Symbols that do not have this format (such as plain C symbols)
causes a lot of warnings to be printed.

Bug: chromium:1062556
Change-Id: I55977f756c7e20cc5e7b1cb8e38316d7bf1f748c
Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/2179482
Reviewed-by: Mark Mentovai <mark@chromium.org>
diff --git a/src/common/language.cc b/src/common/language.cc
index 978fb85..d26e563 100644
--- a/src/common/language.cc
+++ b/src/common/language.cc
@@ -79,6 +79,18 @@
     demangled->clear();
     return kDontDemangle;
 #else
+#if defined(__APPLE__)
+    // Mac C++ symbols can have up to 4 underscores, followed by a "Z".
+    // Non-C++ symbols are not coded that way, but may have leading underscores.
+    // Attempting to demangle non-C++ symbols with the C++ demangler would print
+    // warnings and fail, so return kDontDemangle for these.
+    size_t i = mangled.find_first_not_of('_');
+    if (i == 0 || i == string::npos || i > 4 || mangled[i] != 'Z') {
+      demangled->clear();
+      return kDontDemangle;
+    }
+#endif
+
     int status;
     char* demangled_c =
         abi::__cxa_demangle(mangled.c_str(), NULL, NULL, &status);