From: Ansgar Burchardt Date: Mon, 8 Dec 2008 00:33:13 +0000 (+0100) Subject: Use safe temporary files X-Git-Url: https://repo.or.cz/w/jumpnbump.git/commitdiff_plain/8b6432e02528b6908fe6acaba7ad29027b7c7564 Use safe temporary files This may break Jump'n'bump on Windows. Maybe the patch should be cleaned up a bit. see http://bugs.debian.org/500611 --- diff --git a/modify/jnbunpack.c b/modify/jnbunpack.c index de7c851..aa55d79 100644 --- a/modify/jnbunpack.c +++ b/modify/jnbunpack.c @@ -23,6 +23,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include #include #include #include @@ -83,8 +84,13 @@ int main(int argc, char **argv) memset(filename, 0, sizeof(filename)); strncpy(filename, datafile[i].filename, 12); printf("Extracting %s ", filename); + fflush(stdout); - outfd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644); + if (unlink(filename) == -1 && errno != ENOENT) { + perror("cannot unlink file"); + exit(1); + } + outfd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0644); if (!outfd) { perror("cant open file"); exit(1); diff --git a/sdl/sound.c b/sdl/sound.c index 886d4b0..c6d129f 100644 --- a/sdl/sound.c +++ b/sdl/sound.c @@ -23,6 +23,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include +#include #include "globals.h" #include #ifndef _MSC_VER @@ -463,11 +465,8 @@ char dj_ready_mod(char mod_num) { #ifndef NO_SDL_MIXER FILE *tmp; -# if ((defined _MSC_VER) || (defined __MINGW32__)) - char filename[] = "jnb.tmpmusic.mod"; -# else - char filename[] = "/tmp/jnb.tmpmusic.mod"; -# endif + int tmp_fd; + char* filename; unsigned char *fp; int len; @@ -506,15 +505,24 @@ char dj_ready_mod(char mod_num) return 0; } - tmp = fopen(filename, "wb"); - if (tmp) { - fwrite(fp, len, 1, tmp); - fflush(tmp); - fclose(tmp); + filename = strdup("/tmp/jumpnbump.mod.XXXXXX"); + tmp_fd = mkstemp(filename); + if (tmp_fd == -1) { + free(filename); + return 0; + } + tmp = fdopen(tmp_fd, "wb"); + if (!tmp) { + free(filename); + return 0; } + fwrite(fp, len, 1, tmp); + fflush(tmp); + fclose(tmp); current_music = Mix_LoadMUS(filename); unlink(filename); + free(filename); if (current_music == NULL) { fprintf(stderr, "Couldn't load music: %s\n", SDL_GetError()); return 0;