From f826cc44bc0cc847c927d80b226ac8acb72a47b8 Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 25 May 2016 08:45:03 +0000 Subject: [PATCH] Make code compile when basename() is POSIX compliant. The POSIX basename() function is allowed to modify its input buffer, which means its argument is "char *". Pull a copy of the input string before computing the base. Reviewed by: jtl Differential Revision: https://reviews.freebsd.org/D6465 --- usr.sbin/pmcstat/pmcpl_gprof.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/usr.sbin/pmcstat/pmcpl_gprof.c b/usr.sbin/pmcstat/pmcpl_gprof.c index 5fc9b41ef414..b14778608f8d 100644 --- a/usr.sbin/pmcstat/pmcpl_gprof.c +++ b/usr.sbin/pmcstat/pmcpl_gprof.c @@ -310,8 +310,9 @@ pmcstat_callgraph_do_gmon_arcs(void) void pmcpl_gmon_initimage(struct pmcstat_image *pi) { + const char *execpath; int count, nlen; - char *sn; + char *sn, *snbuf; char name[NAME_MAX]; /* @@ -321,9 +322,11 @@ pmcpl_gmon_initimage(struct pmcstat_image *pi) * `basename(path)`+ "~" + NNN + ".gmon" till we get a free * entry. */ - if ((sn = basename(pmcstat_string_unintern(pi->pi_execpath))) == NULL) - err(EX_OSERR, "ERROR: Cannot process \"%s\"", - pmcstat_string_unintern(pi->pi_execpath)); + execpath = pmcstat_string_unintern(pi->pi_execpath); + if ((snbuf = strdup(execpath)) == NULL) + err(EX_OSERR, "ERROR: Cannot copy \"%s\"", execpath); + if ((sn = basename(snbuf)) == NULL) + err(EX_OSERR, "ERROR: Cannot process \"%s\"", execpath); nlen = strlen(sn); nlen = min(nlen, (int) (sizeof(name) - sizeof(".gmon"))); @@ -355,6 +358,7 @@ pmcpl_gmon_initimage(struct pmcstat_image *pi) } } while (count > 0); } + free(snbuf); LIST_INIT(&pi->pi_gmlist); }