Skip to content

Commit

Permalink
cpp: Use mkstemps() to create unique temporary file
Browse files Browse the repository at this point in the history
Use mkstemps() to safely create a unique temporary file instead of using
rand() to manually create a (potentially unsafe) temporary filename.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
tklauser committed Apr 26, 2016
1 parent 01a9a2a commit a86e9fd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cpp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>

#include "cpp.h"
Expand All @@ -21,12 +22,15 @@ int cpp_exec(char *in_file, char *out_file, size_t out_len, char *const argv[])
size_t argc = 7 + argv_len(argv);
char *tmp = xstrdup(in_file);
char **cpp_argv;
int ret = 0;
int fd, ret = -1;
char *base;
unsigned int i = 0;

base = basename(tmp);
slprintf(out_file, out_len, "/tmp/.tmp-%u-%s", rand(), base);
slprintf(out_file, out_len, "/tmp/.tmp-XXXXXX-%s", base);
fd = mkstemps(out_file, strlen(base) + 1);
if (fd < 0)
goto err;

cpp_argv = xmalloc(argc * sizeof(char *));

Expand All @@ -42,10 +46,11 @@ int cpp_exec(char *in_file, char *out_file, size_t out_len, char *const argv[])
cpp_argv[i++] = in_file;
cpp_argv[i++] = NULL;

if (proc_exec("cpp", cpp_argv))
ret = -1;
ret = proc_exec("cpp", cpp_argv);
close(fd);

xfree(cpp_argv);
err:
xfree(tmp);
return ret;
}

0 comments on commit a86e9fd

Please sign in to comment.