Print this page
6375 Add native name demangling support
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Richard Lowe <richlowe@richlowe.net>

@@ -22,10 +22,11 @@
 /*
  *      Copyright (c) 1988 AT&T
  *        All Rights Reserved
  *
  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright 2018, Joyent, Inc.
  */
 
 /* Get definitions for the relocation types supported. */
 #define ELF_TARGET_ALL
 

@@ -475,32 +476,36 @@
 /* demangle C++ names */
 static char *
 demangled_name(char *s)
 {
         static char     *buf = NULL;
-        const char      *dn;
+        size_t          buflen = 0;
+        char            *dn;
         size_t          len;
 
-        dn = conv_demangle_name(s);
+        dn = (char *)conv_demangle_name(s);
 
         /*
          * If not demangled, just return the symbol name
          */
-        if (strcmp(s, dn) == 0)
+        if (dn == s)
                 return (s);
 
-        /*
-         * Demangled. Format it
-         */
-        if (buf != NULL)
-                free(buf);
-
         len = strlen(dn) + strlen(s) + 4;
+
+        if (buflen < len) {
+                free(buf);
         if ((buf = malloc(len)) == NULL)
                 return (s);
+                buflen = len;
+        }
 
-        (void) snprintf(buf, len, "%s\t[%s]", dn, s);
+        /*
+         * Demangled. Format it
+         */
+        (void) snprintf(buf, buflen, "%s\t[%s]", dn, s);
+        free(dn);
         return (buf);
 }
 
 /*
  * Print the symbol table.  Input is an ELF file descriptor, a

@@ -524,11 +529,11 @@
 
         if (gelf_getclass(elf_file) == ELFCLASS64)
                 adj = 8;
 
         while (range > 0) {
-                char            *sym_name = (char *)0;
+                char            *sym_name = NULL;
                 int             type, bind;
                 int             specsec;
                 unsigned int    shndx;
 
                 (void) gelf_getsym(sym_data, index, &sym);