  	}
  
  	/* Use full buffering. */
-@@ -553,7 +547,7 @@ int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...)
+@@ -553,7 +562,7 @@ int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...)
  	int ret;
  
  	va_start(ap, fmt);

 		obj->myalloc_ = 1;
 	}
 	if (!obj->buf_) {
@@ -361,28 +361,22 @@ jas_stream_t *jas_stream_tmpfile()
 	}
 	obj->fd = -1;
 	obj->flags = 0;
-	obj->pathname[0] = '\0';
 	stream->obj_ = obj;
 
 	/* Choose a file name. */
-	tmpnam(obj->pathname);
+	snprintf(obj->pathname, L_tmpnam, "%stmp.XXXXXXXXXX", P_tmpdir);
 
 	/* Open the underlying file. */
-	if ((obj->fd = open(obj->pathname, O_CREAT | O_EXCL | O_RDWR | O_TRUNC | O_BINARY,
-	  JAS_STREAM_PERMS)) < 0) {
+	if ((obj->fd = mkstemp(obj->pathname)) < 0) {
 		jas_stream_destroy(stream);
 		return 0;
 	}
 
 	/* Unlink the file so that it will disappear if the program
 	terminates abnormally. */

 	jas_stream_puts(stream, buf);
 	va_end(ap);
 	return ret;
@@ -992,7 +1001,7 @@ static int mem_resize(jas_stream_memobj_t *m, int bufsize)
 	unsigned char *buf;
 
 	assert(m->buf_);
