svn commit: r303443 - head/usr.bin/sed

Ed Schouten ed at FreeBSD.org
Thu Jul 28 15:17:13 UTC 2016


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);


More information about the svn-src-all mailing list