Harden failure signal handler in the face of memory corruptions
[folly.git] / folly / String.cpp
index 44b6a783369ddfbdc0060841ea89226f4e35404d..2841ce0c94ab0a4c9a15fd11733e84e937e378c6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <iterator>
 #include <glog/logging.h>
 
-#undef FOLLY_DEMANGLE
-
-#if defined(__GNUG__) && __GNUG__ >= 4
+#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
 # include <cxxabi.h>
-# define FOLLY_DEMANGLE 1
 
 // From libiberty
 //
 // symbols" (but, interestingly enough, will resolve undefined weak symbols
 // with definitions from archive members that were extracted in order to
 // resolve an undefined global (strong) symbol)
+
+# ifndef DMGL_NO_OPTS
+#  define FOLLY_DEFINED_DMGL 1
+#  define DMGL_NO_OPTS    0          /* For readability... */
+#  define DMGL_PARAMS     (1 << 0)   /* Include function args */
+#  define DMGL_ANSI       (1 << 1)   /* Include const, volatile, etc */
+#  define DMGL_JAVA       (1 << 2)   /* Demangle as Java rather than C++. */
+#  define DMGL_VERBOSE    (1 << 3)   /* Include implementation details.  */
+#  define DMGL_TYPES      (1 << 4)   /* Also try to demangle type encodings.  */
+#  define DMGL_RET_POSTFIX (1 << 5)  /* Print function return types (when
+                                        present) after function signature */
+# endif
+
 extern "C" int cplus_demangle_v3_callback(
     const char* mangled,
     int options,  // We use DMGL_PARAMS | DMGL_TYPES, aka 0x11
@@ -297,7 +307,7 @@ size_t my_strlcpy(char* dest, const char* src, size_t size) {
 
 }  // namespace
 
-#ifdef FOLLY_DEMANGLE
+#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
 
 fbstring demangle(const char* name) {
   int status;
@@ -340,7 +350,7 @@ size_t demangle(const char* name, char* out, size_t outSize) {
   // Unlike most library functions, this returns 1 on success and 0 on failure
   int status = cplus_demangle_v3_callback(
       name,
-      0x11,  // DMGL_PARAMS | DMGL_TYPES
+      DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES,
       demangleCallback,
       &dbuf);
   if (status == 0) {  // failed, return original
@@ -363,7 +373,6 @@ size_t demangle(const char* name, char* out, size_t outSize) {
 }
 
 #endif
-#undef FOLLY_DEMANGLE
 
 namespace detail {
 
@@ -410,3 +419,15 @@ size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
 } // namespace detail
 
 }   // namespace folly
+
+#ifdef FOLLY_DEFINED_DMGL
+# undef FOLLY_DEFINED_DMGL
+# undef DMGL_NO_OPTS
+# undef DMGL_PARAMS
+# undef DMGL_ANSI
+# undef DMGL_JAVA
+# undef DMGL_VERBOSE
+# undef DMGL_TYPES
+# undef DMGL_RET_POSTFIX
+#endif
+