am ab278538: Merge "[PATCH] perf tool: fix bug in usage of the basename() function"

* commit 'ab278538a9a069743ff72523cb72405fa25fe497':
  [PATCH] perf tool: fix bug in usage of the basename() function
diff --git a/perf-3.12.0/tools/perf/util/dso.c b/perf-3.12.0/tools/perf/util/dso.c
index e3c1ff8..0e18da0 100644
--- a/perf-3.12.0/tools/perf/util/dso.c
+++ b/perf-3.12.0/tools/perf/util/dso.c
@@ -381,7 +381,38 @@
 
 static void dso__set_basename(struct dso *dso)
 {
-	dso__set_short_name(dso, basename(dso->long_name));
+	char *lname, *base;
+
+	/*
+	 * basename may modify path buffer, so we must pass
+	 * a copy.
+	 */
+	lname = strdup(dso->long_name);
+	if (!lname)
+		return;
+
+	/*
+	 * basename may return pointer to internal
+	 * storage which is reused in subsequent calls
+	 * so copy the result
+	 */
+	base = strdup(basename(lname));
+
+	free(lname);
+
+	if (!base)
+		return;
+
+	if (dso->sname_alloc)
+		free((char *)dso->short_name);
+	else
+		dso->sname_alloc = 1;
+	/*
+	 * basename may modify content, so we must pass
+	 * a copy. Moreover basename may return pointer to internal
+	 * storage we may be reusing later on
+	 */
+	dso__set_short_name(dso, base);
 }
 
 int dso__name_len(const struct dso *dso)