Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Sep 2012 02:38:07 +0000 (UTC)
From:      Rui Paulo <rpaulo@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r240182 - head/lib/libproc
Message-ID:  <201209070238.q872c7E6023674@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: rpaulo
Date: Fri Sep  7 02:38:07 2012
New Revision: 240182
URL: http://svn.freebsd.org/changeset/base/240182

Log:
  When calling the C++ demangler, make sure to free the returned buffer,
  which might have been reallocated.
  
  Pointed out by:	stefanf

Modified:
  head/lib/libproc/proc_sym.c

Modified: head/lib/libproc/proc_sym.c
==============================================================================
--- head/lib/libproc/proc_sym.c	Fri Sep  7 01:33:25 2012	(r240181)
+++ head/lib/libproc/proc_sym.c	Fri Sep  7 02:38:07 2012	(r240182)
@@ -51,6 +51,26 @@ extern char *__cxa_demangle(const char *
 static void	proc_rdl2prmap(rd_loadobj_t *, prmap_t *);
 
 static void
+demangle(const char *symbol, char *buf, size_t len)
+{
+	char *dembuf;
+	size_t demlen = len;
+
+	dembuf = malloc(len);
+	if (!dembuf)
+		goto fail;
+	dembuf = __cxa_demangle(symbol, dembuf, &demlen, NULL);
+	if (!dembuf)
+		goto fail;
+	strlcpy(buf, dembuf, len);
+	free(dembuf);
+
+	return;
+fail:
+	strlcpy(buf, symbol, len);
+}
+
+static void
 proc_rdl2prmap(rd_loadobj_t *rdl, prmap_t *map)
 {
 	map->pr_vaddr = rdl->rdl_saddr;
@@ -268,9 +288,8 @@ proc_addr2sym(struct proc_handle *p, uin
 		if (addr >= rsym && addr <= (rsym + sym.st_size)) {
 			s = elf_strptr(e, dynsymstridx, sym.st_name);
 			if (s) {
-				if (strlen(s) > 2 && 
-				    s[0] == '_' && s[1] == 'Z')
-					__cxa_demangle(s, name, &namesz, NULL);
+				if (s[0] == '_' && s[1] == 'Z' && s[2])
+					demangle(s, name, namesz);
 				else
 					strlcpy(name, s, namesz);
 				memcpy(symcopy, &sym, sizeof(sym));
@@ -308,9 +327,8 @@ symtab:
 		if (addr >= rsym && addr <= (rsym + sym.st_size)) {
 			s = elf_strptr(e, symtabstridx, sym.st_name);
 			if (s) {
-				if (strlen(s) > 2 && 
-				    s[0] == '_' && s[1] == 'Z')
-					__cxa_demangle(s, name, &namesz, NULL);
+				if (s[0] == '_' && s[1] == 'Z' && s[2])
+					demangle(s, name, namesz);
 				else
 					strlcpy(name, s, namesz);
 				memcpy(symcopy, &sym, sizeof(sym));



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201209070238.q872c7E6023674>