[hackers] [sbase] Only use /tmp if template is a file and not a path || sin

From: <git_AT_suckless.org>
Date: Fri, 04 Jul 2014 17:02:22 +0200

commit f5ac08cc04c12d7e35f614918d2fc3c44e158dcf
Author: sin <sin_AT_2f30.org>
Date: Fri Jul 4 15:55:31 2014 +0100

    Only use /tmp if template is a file and not a path

diff --git a/mktemp.c b/mktemp.c
index e001dac..697d789 100644
--- a/mktemp.c
+++ b/mktemp.c
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <libgen.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
_AT_@ -20,8 +21,8 @@ main(int argc, char *argv[])
 {
         char *template = "tmp.XXXXXXXXXX";
         char *tmpdir = "/tmp", *p;
- char tmppath[PATH_MAX];
- int fd, r;
+ char path[PATH_MAX], tmp[PATH_MAX];
+ int fd;
 
         ARGBEGIN {
         case 'd':
_AT_@ -42,24 +43,35 @@ main(int argc, char *argv[])
         if ((p = getenv("TMPDIR")))
                 tmpdir = p;
 
- r = snprintf(tmppath, sizeof(tmppath),
- "%s/%s", tmpdir, template);
- if (r >= sizeof(tmppath) || r < 0)
+ if (strlcpy(tmp, template, sizeof(tmp)) >= sizeof(tmp))
                 eprintf("path too long
");
+ p = dirname(tmp);
+ if (p[0] != '.') {
+ if (strlcpy(path, template, sizeof(path)) >= sizeof(path))
+ eprintf("path too long
");
+ } else {
+ if (strlcpy(path, tmpdir, sizeof(path)) >= sizeof(path))
+ eprintf("path too long
");
+ if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
+ eprintf("path too long
");
+ if (strlcat(path, template, sizeof(path)) >= sizeof(path))
+ eprintf("path too long
");
+ }
+
         if (dflag) {
- if (!mkdtemp(tmppath)) {
+ if (!mkdtemp(path)) {
                         if (!qflag)
- eprintf("mkdtemp %s:", tmppath);
+ eprintf("mkdtemp %s:", path);
                         exit(EXIT_FAILURE);
                 }
         } else {
- if ((fd = mkstemp(tmppath)) < 0) {
+ if ((fd = mkstemp(path)) < 0) {
                         if (!qflag)
- eprintf("mkstemp %s:", tmppath);
+ eprintf("mkstemp %s:", path);
                         exit(EXIT_FAILURE);
                 }
                 close(fd);
         }
- puts(tmppath);
+ puts(path);
         return EXIT_SUCCESS;
 }
Received on Fri Jul 04 2014 - 17:02:22 CEST

This archive was generated by hypermail 2.3.0 : Fri Jul 04 2014 - 17:12:07 CEST