Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Jul 2016 15:17:12 +0000 (UTC)
From:      Ed Schouten <ed@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r303443 - head/usr.bin/sed
Message-ID:  <201607281517.u6SFHC6S005616@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ed
Date: Thu Jul 28 15:17:12 2016
New Revision: 303443
URL: https://svnweb.freebsd.org/changeset/base/303443

Log:
  Don't call basename() and dirname() in an unportable way.
  
  POSIX allows these functions to modify their input buffer, so that they
  have storage for the return value. Pull copies of the filename before
  calling these utility functions.

Modified:
  head/usr.bin/sed/main.c

Modified: head/usr.bin/sed/main.c
==============================================================================
--- head/usr.bin/sed/main.c	Thu Jul 28 13:54:46 2016	(r303442)
+++ head/usr.bin/sed/main.c	Thu Jul 28 15:17:12 2016	(r303443)
@@ -301,6 +301,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag
 {
 	struct stat sb;
 	ssize_t len;
+	char *dirbuf, *basebuf;
 	static char *p = NULL;
 	static size_t plen = 0;
 	int c;
@@ -389,9 +390,14 @@ mf_fgets(SPACE *sp, enum e_spflag spflag
 				if ((size_t)len > sizeof(oldfname))
 					errx(1, "%s: name too long", fname);
 			}
+			if ((dirbuf = strdup(fname)) == NULL ||
+			    (basebuf = strdup(fname)) == NULL)
+				err(1, "strdup");
 			len = snprintf(tmpfname, sizeof(tmpfname),
-			    "%s/.!%ld!%s", dirname(fname), (long)getpid(),
-			    basename(fname));
+			    "%s/.!%ld!%s", dirname(dirbuf), (long)getpid(),
+			    basename(basebuf));
+			free(dirbuf);
+			free(basebuf);
 			if ((size_t)len >= sizeof(tmpfname))
 				errx(1, "%s: name too long", fname);
 			unlink(tmpfname);



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