From 3cd37427b5c5f26f62cff583fbde914467ddafe3 Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 26 Jan 2007 21:43:58 +0000 Subject: Both files in this commit exhibit the wrong way to use temporary files. For TEHistory.cpp, it uses tmpfile() which produces a file which has a name that can be guessed. For vmemo.cpp, it uses tmpname() which only creates a predictable string. Both uses have been switched to using mkstemp() wrapped around umask(). This produces a much less predictable file that also has guaranteed restrictive permissions. I went a little farther in vmemo because it calls out to the shell using system to 'mv' the new file. That is kinda wasteful so I switched it to use rename instead. --- (limited to 'noncore') diff --git a/noncore/apps/opie-console/TEHistory.cpp b/noncore/apps/opie-console/TEHistory.cpp index 317ce57..e2be42a 100644 --- a/noncore/apps/opie-console/TEHistory.cpp +++ b/noncore/apps/opie-console/TEHistory.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -96,9 +97,29 @@ void HistoryBuffer::setScroll(bool on) { assert( ion < 0 ); assert( length == 0); - FILE* tmp = tmpfile(); if (!tmp) { perror("konsole: cannot open temp file.\n"); return; } - ion = dup(fileno(tmp)); if (ion<0) perror("konsole: cannot dup temp file.\n"); - fclose(tmp); + char* tmpDir = getenv("TMPDIR"); + char* tmpFilePath = 0; + if (tmpDir && *tmpDir != '\0') { + tmpFilePath = new char[strlen(tmpDir) + strlen("/opie-console-HistoryBuffer-XXXXXX") + 1]; + strcpy(tmpFilePath, tmpDir); + free(tmpDir); + } else { + tmpFilePath = new char[strlen("/tmp/opie-console-HistoryBuffer-XXXXXX") + 1]; + strcpy(tmpFilePath, "/tmp"); + } + strcat(tmpFilePath, "/opie-console-HistoryBuffer-XXXXXX"); + mode_t currUmask = umask(S_IRWXO | S_IRWXG); + int tmpfd = mkstemp(tmpFilePath); + delete [] tmpFilePath; + umask(currUmask); + if (tmpfd == -1) { + perror("konsole: cannot open temp file.\n"); + return; + } + ion = dup(tmpfd); + if (ion<0) + perror("konsole: cannot dup temp file.\n"); + close(tmpfd); } else { -- cgit v0.9.0.2