[PATCH] xmkstemp: safe mkstemp (-65 bytes)

Alexander Shishkin virtuoso at slind.org
Thu Oct 21 14:07:55 UTC 2010


function                                             old     new   delta
xmkstemp                                               -      90     +90
convert                                              538     528     -10
.rodata                                           475974  475950     -24
copy_tempfile                                        157     132     -25
diffreg                                              731     705     -26
lpqr_main                                           1961    1928     -33
sed_main                                            1215    1178     -37
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/6 up/down: 90/-155)           Total: -65 bytes

Signed-off-by: Alexander Shishkin <virtuoso at slind.org>
---
 coreutils/dos2unix.c  |    8 +++-----
 editors/diff.c        |    5 ++---
 editors/patch.c       |    3 +--
 editors/sed.c         |    4 +---
 include/libbb.h       |    1 +
 libbb/xfuncs_printf.c |   15 +++++++++++++++
 printutils/lpr.c      |    4 +---
 7 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index ba1ca8c..eab8110 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -41,12 +41,10 @@ static void convert(char *fn, int conv_type)
 		fstat(fileno(in), &st);
 
 		temp_fn = xasprintf("%sXXXXXX", resolved_fn);
-		i = mkstemp(temp_fn);
-		if (i == -1
-		 || fchmod(i, st.st_mode) == -1
-		) {
+		i = xmkstemp(temp_fn);
+		if (fchmod(i, st.st_mode) == -1)
 			bb_simple_perror_msg_and_die(temp_fn);
-		}
+
 		out = xfdopen_for_write(i);
 	}
 
diff --git a/editors/diff.c b/editors/diff.c
index 83de527..d9d709d 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -685,9 +685,8 @@ static int diffreg(char *file[2])
 		 */
 		if (lseek(fd, 0, SEEK_SET) == -1 && errno == ESPIPE) {
 			char name[] = "/tmp/difXXXXXX";
-			int fd_tmp = mkstemp(name);
-			if (fd_tmp < 0)
-				bb_perror_msg_and_die("mkstemp");
+			int fd_tmp = xmkstemp(name);
+
 			unlink(name);
 			if (bb_copyfd_eof(fd, fd_tmp) < 0)
 				xfunc_die();
diff --git a/editors/patch.c b/editors/patch.c
index fff0690..33ff8b5 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -200,8 +200,7 @@ int copy_tempfile(int fdin, char *name, char **tempname)
 	int fd;
 
 	*tempname = xasprintf("%sXXXXXX", name);
-	fd = mkstemp(*tempname);
-	if(-1 == fd) bb_perror_msg_and_die("no temp file");
+	fd = xmkstemp(*tempname);
 
 	// Set permissions of output file
 	fstat(fdin, &statbuf);
diff --git a/editors/sed.c b/editors/sed.c
index 8d9f7b2..964d040 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -1370,9 +1370,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
 			}
 
 			G.outname = xasprintf("%sXXXXXX", argv[i]);
-			nonstdoutfd = mkstemp(G.outname);
-			if (-1 == nonstdoutfd)
-				bb_perror_msg_and_die("can't create temp file %s", G.outname);
+			nonstdoutfd = xmkstemp(G.outname);
 			G.nonstdout = xfdopen_for_write(nonstdoutfd);
 
 			/* Set permissions/owner of output file */
diff --git a/include/libbb.h b/include/libbb.h
index 01dc33e..409c434 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -425,6 +425,7 @@ int xopen_stdin(const char *pathname) FAST_FUNC;
 void xrename(const char *oldpath, const char *newpath) FAST_FUNC;
 int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC;
 off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC;
+int xmkstemp(char *template) FAST_FUNC;
 off_t fdlength(int fd) FAST_FUNC;
 
 uoff_t FAST_FUNC get_volume_size_in_bytes(int fd,
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index ba660a2..1f92f00 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -240,6 +240,21 @@ off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
 	return off;
 }
 
+int FAST_FUNC xmkstemp(char *template)
+{
+	mode_t saved_umask;
+	int fd;
+
+	saved_umask = umask(0);
+	fd = mkstemp(template);
+	umask(saved_umask);
+
+	if (fd == -1)
+		bb_perror_msg_and_die("Can't create temp file %s", template);
+
+	return fd;
+}
+
 // Die with supplied filename if this FILE* has ferror set.
 void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
 {
diff --git a/printutils/lpr.c b/printutils/lpr.c
index fb7860d..2849179 100644
--- a/printutils/lpr.c
+++ b/printutils/lpr.c
@@ -159,9 +159,7 @@ int lpqr_main(int argc UNUSED_PARAM, char *argv[])
 		// if data file is stdin, we need to dump it first
 		if (LONE_DASH(*argv)) {
 			strcpy(tempfile, "/tmp/lprXXXXXX");
-			dfd = mkstemp(tempfile);
-			if (dfd < 0)
-				bb_perror_msg_and_die("mkstemp");
+			dfd = xmkstemp(tempfile);
 			bb_copyfd_eof(STDIN_FILENO, dfd);
 			xlseek(dfd, 0, SEEK_SET);
 			*argv = (char*)bb_msg_standard_input;
-- 
1.7.2.1.45.gb66c2



More information about the busybox mailing list