Use tmpfileplus() instead of tempnam()

Finally get rid of security or deprecated warnings cause by tempnam() function usage
This commit is contained in:
alexey.lysiuk 2017-11-04 13:15:53 +02:00
parent e6c9ccf3a1
commit 0e706bfecf
7 changed files with 24 additions and 159 deletions

View file

@ -958,7 +958,6 @@ set (PCH_SOURCES
stats.cpp stats.cpp
stringtable.cpp stringtable.cpp
teaminfo.cpp teaminfo.cpp
tempfiles.cpp
v_blend.cpp v_blend.cpp
v_collection.cpp v_collection.cpp
v_draw.cpp v_draw.cpp
@ -1179,6 +1178,7 @@ add_executable( zdoom WIN32 MACOSX_BUNDLE
${PCH_SOURCES} ${PCH_SOURCES}
x86.cpp x86.cpp
strnatcmp.c strnatcmp.c
tmpfileplus.c
zstring.cpp zstring.cpp
math/asin.c math/asin.c
math/atan.c math/atan.c

View file

@ -59,7 +59,6 @@ extern void ChildSigHandler (int signum);
#include "s_sound.h" #include "s_sound.h"
#include "m_swap.h" #include "m_swap.h"
#include "i_cd.h" #include "i_cd.h"
#include "tempfiles.h"
#include "templates.h" #include "templates.h"
#include "stats.h" #include "stats.h"
#include "timidity/timidity.h" #include "timidity/timidity.h"

View file

@ -1,5 +1,4 @@
#include "tempfiles.h"
#include "oplsynth/opl_mus_player.h" #include "oplsynth/opl_mus_player.h"
#include "c_cvars.h" #include "c_cvars.h"
#include "mus2midi.h" #include "mus2midi.h"

View file

@ -42,6 +42,7 @@
#include "cmdlib.h" #include "cmdlib.h"
#include "templates.h" #include "templates.h"
#include "version.h" #include "version.h"
#include "tmpfileplus.h"
#ifndef _WIN32 #ifndef _WIN32
#include <unistd.h> #include <unistd.h>
@ -81,13 +82,12 @@ public:
protected: protected:
bool LaunchTimidity(); bool LaunchTimidity();
FTempFileName DiskName; char* DiskName;
#ifdef _WIN32 #ifdef _WIN32
HANDLE ReadWavePipe; HANDLE ReadWavePipe;
HANDLE WriteWavePipe; HANDLE WriteWavePipe;
HANDLE ChildProcess; HANDLE ChildProcess;
FString CommandLine; FString CommandLine;
size_t LoopPos;
bool Validated; bool Validated;
bool ValidateTimidity(); bool ValidateTimidity();
#else // _WIN32 #else // _WIN32
@ -166,7 +166,7 @@ CUSTOM_CVAR (Int, timidity_frequency, 44100, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
//========================================================================== //==========================================================================
TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args) TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args)
: DiskName("zmid"), : DiskName(nullptr),
#ifdef _WIN32 #ifdef _WIN32
ReadWavePipe(INVALID_HANDLE_VALUE), WriteWavePipe(INVALID_HANDLE_VALUE), ReadWavePipe(INVALID_HANDLE_VALUE), WriteWavePipe(INVALID_HANDLE_VALUE),
ChildProcess(INVALID_HANDLE_VALUE), ChildProcess(INVALID_HANDLE_VALUE),
@ -194,12 +194,6 @@ TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args)
CommandLine += "\" "; CommandLine += "\" ";
} }
#endif #endif
if (DiskName == NULL)
{
Printf(PRINT_BOLD, "Could not create temp music file\n");
return;
}
} }
//========================================================================== //==========================================================================
@ -210,6 +204,12 @@ TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args)
TimidityPPMIDIDevice::~TimidityPPMIDIDevice () TimidityPPMIDIDevice::~TimidityPPMIDIDevice ()
{ {
if (nullptr != DiskName)
{
remove(DiskName);
free(DiskName);
}
#if _WIN32 #if _WIN32
if (WriteWavePipe != INVALID_HANDLE_VALUE) if (WriteWavePipe != INVALID_HANDLE_VALUE)
{ {
@ -252,17 +252,12 @@ bool TimidityPPMIDIDevice::Preprocess(MIDIStreamer *song, bool looping)
return false; return false;
} }
// Tell TiMidity++ whether it should loop or not
#ifdef _WIN32
CommandLine.LockBuffer()[LoopPos] = looping ? 'l' : ' ';
CommandLine.UnlockBuffer();
#endif
Looping = looping; Looping = looping;
// Write MIDI song to temporary file // Write MIDI song to temporary file
song->CreateSMF(midi, looping ? 0 : 1); song->CreateSMF(midi, looping ? 0 : 1);
f = fopen(DiskName, "wb"); f = tmpfileplus(nullptr, "zmid", &DiskName, 1);
if (f == NULL) if (f == NULL)
{ {
Printf(PRINT_BOLD, "Could not open temp music file\n"); Printf(PRINT_BOLD, "Could not open temp music file\n");
@ -275,6 +270,16 @@ bool TimidityPPMIDIDevice::Preprocess(MIDIStreamer *song, bool looping)
{ {
Printf(PRINT_BOLD, "Could not write temp music file\n"); Printf(PRINT_BOLD, "Could not write temp music file\n");
} }
#ifdef _WIN32
CommandLine.AppendFormat("-o - -Ors%c%c%c -id%c %s",
timidity_stereo ? 'S' : 'M',
timidity_8bit ? '8' : '1',
timidity_byteswap ? 'x' : ' ',
looping ? 'l' : ' ',
DiskName);
#endif
return false; return false;
} }
@ -342,20 +347,6 @@ int TimidityPPMIDIDevice::Open(MidiCallback callback, void *userdata)
} }
} }
#ifdef _WIN32
CommandLine += "-o - -Ors";
CommandLine += timidity_stereo ? 'S' : 'M';
CommandLine += timidity_8bit ? '8' : '1';
if (timidity_byteswap)
{
CommandLine += 'x';
}
LoopPos = CommandLine.Len() + 4;
CommandLine += " -idl ";
CommandLine += DiskName.GetName();
#endif
return 0; return 0;
} }
@ -463,12 +454,12 @@ bool TimidityPPMIDIDevice::ValidateTimidity()
bool TimidityPPMIDIDevice::LaunchTimidity () bool TimidityPPMIDIDevice::LaunchTimidity ()
{ {
#ifdef _WIN32 if (ExeName.IsEmpty() || nullptr == DiskName)
if (CommandLine.IsEmpty())
{ {
return false; return false;
} }
#ifdef _WIN32
DPrintf (DMSG_NOTIFY, "cmd: \x1cG%s\n", CommandLine.GetChars()); DPrintf (DMSG_NOTIFY, "cmd: \x1cG%s\n", CommandLine.GetChars());
STARTUPINFO startup = { sizeof(startup), }; STARTUPINFO startup = { sizeof(startup), };
@ -516,11 +507,6 @@ bool TimidityPPMIDIDevice::LaunchTimidity ()
} }
return false; return false;
#else #else
if (ExeName.IsEmpty())
{
return false;
}
if (WavePipe[0] != -1 && WavePipe[1] == -1 && Stream != NULL) if (WavePipe[0] != -1 && WavePipe[1] == -1 && Stream != NULL)
{ {
// Timidity was previously launched, so the write end of the pipe // Timidity was previously launched, so the write end of the pipe
@ -579,7 +565,7 @@ bool TimidityPPMIDIDevice::LaunchTimidity ()
arglist.push_back("-"); arglist.push_back("-");
arglist.push_back(outmodearg.c_str()); arglist.push_back(outmodearg.c_str());
arglist.push_back(ifacearg.c_str()); arglist.push_back(ifacearg.c_str());
arglist.push_back(DiskName.GetName()); arglist.push_back(DiskName);
DPrintf(DMSG_NOTIFY, "Timidity EXE: \x1cG%s\n", exename); DPrintf(DMSG_NOTIFY, "Timidity EXE: \x1cG%s\n", exename);
int i = 1; int i = 1;

View file

@ -57,7 +57,6 @@
#include "i_module.h" #include "i_module.h"
#include "i_music.h" #include "i_music.h"
#include "i_musicinterns.h" #include "i_musicinterns.h"
#include "tempfiles.h"
#include "cmdlib.h" #include "cmdlib.h"
FModule OpenALModule{"OpenAL"}; FModule OpenALModule{"OpenAL"};

View file

@ -1,57 +0,0 @@
/*
** tempfiles.cpp
** Temporary name generator. Deletes the temporary file when deconstructed.
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include <stdio.h>
#include <stdlib.h>
#include "tempfiles.h"
FTempFileName::FTempFileName (const char *prefix)
{
// Under Linux, ld will complain that tempnam is dangerous, and
// mkstemp should be used instead. However, there is no mkstemp
// under VC++, and even if there was, I still need to know the
// file name so that it can be used as input to Timidity.
Name = tempnam (NULL, prefix);
}
FTempFileName::~FTempFileName ()
{
if (Name != NULL)
{
remove (Name);
free (Name);
Name = NULL;
}
}

View file

@ -1,61 +0,0 @@
/*
** tempfiles.h
**
**---------------------------------------------------------------------------
** Copyright 1998-2006 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#ifndef __TEMPFILES_H__
#define __TEMPFILES_H__
#ifdef _MSC_VER
#pragma once
#endif
#include <stdlib.h>
// Returns a file name suitable for use as a temp file.
// If you create a file with this name (and presumably you
// will), it will be deleted automatically by this class's
// destructor.
class FTempFileName
{
public:
FTempFileName (const char *prefix=NULL);
~FTempFileName ();
operator const char * () { return Name; }
const char * GetName () const { return Name; }
private:
char *Name;
};
#endif //__TEMPFILES_H__