Merge branch 'unsafe_context'

This commit is contained in:
Christoph Oelckers 2018-01-20 11:21:52 +01:00
commit 2fddea7bed
10 changed files with 88 additions and 62 deletions

View file

@ -558,7 +558,7 @@ void C_UnbindAll ()
AutomapBindings.UnbindAll(); AutomapBindings.UnbindAll();
} }
CCMD (unbindall) UNSAFE_CCMD (unbindall)
{ {
C_UnbindAll (); C_UnbindAll ();
} }

View file

@ -467,7 +467,7 @@ CCMD (print)
} }
} }
CCMD (exec) UNSAFE_CCMD (exec)
{ {
if (argv.argc() < 2) if (argv.argc() < 2)
return; return;
@ -495,7 +495,7 @@ void execLogfile(const char *fn, bool append)
} }
} }
CCMD (logfile) UNSAFE_CCMD (logfile)
{ {
if (Logfile) if (Logfile)
@ -651,7 +651,7 @@ CCMD (error)
} }
} }
CCMD (error_fatal) UNSAFE_CCMD (error_fatal)
{ {
if (argv.argc() > 1) if (argv.argc() > 1)
{ {
@ -674,7 +674,7 @@ CCMD (error_fatal)
//========================================================================== //==========================================================================
#if !defined(_WIN32) || !defined(_DEBUG) #if !defined(_WIN32) || !defined(_DEBUG)
CCMD (crashout) UNSAFE_CCMD (crashout)
{ {
*(volatile int *)0 = 0; *(volatile int *)0 = 0;
} }
@ -794,7 +794,7 @@ CCMD (warp)
// //
//========================================================================== //==========================================================================
CCMD (load) UNSAFE_CCMD (load)
{ {
if (argv.argc() != 2) if (argv.argc() != 2)
{ {
@ -819,7 +819,7 @@ CCMD (load)
// //
//========================================================================== //==========================================================================
CCMD (save) UNSAFE_CCMD (save)
{ {
if (argv.argc() < 2 || argv.argc() > 3) if (argv.argc() < 2 || argv.argc() > 3)
{ {

View file

@ -627,7 +627,7 @@ void C_InitConsole (int width, int height, bool ingame)
// //
//========================================================================== //==========================================================================
CCMD (atexit) UNSAFE_CCMD (atexit)
{ {
if (argv.argc() == 1) if (argv.argc() == 1)
{ {

View file

@ -65,7 +65,7 @@ class DWaitingCommand : public DThinker
{ {
DECLARE_CLASS (DWaitingCommand, DThinker) DECLARE_CLASS (DWaitingCommand, DThinker)
public: public:
DWaitingCommand (const char *cmd, int tics); DWaitingCommand (const char *cmd, int tics, bool unsafe);
~DWaitingCommand (); ~DWaitingCommand ();
void Serialize(FSerializer &arc); void Serialize(FSerializer &arc);
void Tick (); void Tick ();
@ -75,6 +75,7 @@ private:
char *Command; char *Command;
int TicsLeft; int TicsLeft;
bool IsUnsafe;
}; };
class DStoredCommand : public DThinker class DStoredCommand : public DThinker
@ -126,7 +127,8 @@ FButtonStatus Button_Mlook, Button_Klook, Button_Use, Button_AltAttack,
Button_AM_PanLeft, Button_AM_PanRight, Button_AM_PanDown, Button_AM_PanUp, Button_AM_PanLeft, Button_AM_PanRight, Button_AM_PanDown, Button_AM_PanUp,
Button_AM_ZoomIn, Button_AM_ZoomOut; Button_AM_ZoomIn, Button_AM_ZoomOut;
bool ParsingKeyConf, ParsingMenuDef = false; bool ParsingKeyConf;
static bool UnsafeExecutionContext;
// To add new actions, go to the console and type "key <action name>". // To add new actions, go to the console and type "key <action name>".
// This will give you the key value to use in the first column. Then // This will give you the key value to use in the first column. Then
@ -187,24 +189,6 @@ static const char *KeyConfCommands[] =
"clearplayerclasses" "clearplayerclasses"
}; };
static const char *MenuDefCommands[] =
{
"snd_reset",
"reset2defaults",
"reset2saved",
"menuconsole",
"clearnodecache",
"am_restorecolors",
"undocolorpic",
"special",
"puke",
"fpuke",
"pukename",
"event",
"netevent",
"openmenu"
};
// CODE -------------------------------------------------------------------- // CODE --------------------------------------------------------------------
IMPLEMENT_CLASS(DWaitingCommand, false, false) IMPLEMENT_CLASS(DWaitingCommand, false, false)
@ -213,19 +197,22 @@ void DWaitingCommand::Serialize(FSerializer &arc)
{ {
Super::Serialize (arc); Super::Serialize (arc);
arc("command", Command) arc("command", Command)
("ticsleft", TicsLeft); ("ticsleft", TicsLeft)
("unsafe", IsUnsafe);
} }
DWaitingCommand::DWaitingCommand () DWaitingCommand::DWaitingCommand ()
{ {
Command = NULL; Command = NULL;
TicsLeft = 1; TicsLeft = 1;
IsUnsafe = false;
} }
DWaitingCommand::DWaitingCommand (const char *cmd, int tics) DWaitingCommand::DWaitingCommand (const char *cmd, int tics, bool unsafe)
{ {
Command = copystring (cmd); Command = copystring (cmd);
TicsLeft = tics+1; TicsLeft = tics+1;
IsUnsafe = unsafe;
} }
DWaitingCommand::~DWaitingCommand () DWaitingCommand::~DWaitingCommand ()
@ -240,7 +227,10 @@ void DWaitingCommand::Tick ()
{ {
if (--TicsLeft == 0) if (--TicsLeft == 0)
{ {
const bool wasUnsafe = UnsafeExecutionContext;
UnsafeExecutionContext = IsUnsafe;
AddCommandString (Command); AddCommandString (Command);
UnsafeExecutionContext = wasUnsafe;
Destroy (); Destroy ();
} }
} }
@ -602,25 +592,6 @@ void C_DoCommand (const char *cmd, int keynum)
} }
} }
if (ParsingMenuDef)
{
int i;
for (i = countof(MenuDefCommands)-1; i >= 0; --i)
{
if (strnicmp (beg, MenuDefCommands[i], len) == 0 &&
MenuDefCommands[i][len] == 0)
{
break;
}
}
if (i < 0)
{
Printf ("Invalid command for MENUDEF/ZScript: %s\n", beg);
return;
}
}
// Check if this is an action // Check if this is an action
if (*beg == '+' || *beg == '-') if (*beg == '+' || *beg == '-')
{ {
@ -687,6 +658,12 @@ void C_DoCommand (const char *cmd, int keynum)
if (args.argc() >= 2) if (args.argc() >= 2)
{ // Set the variable { // Set the variable
if (UnsafeExecutionContext && !(var->GetFlags() & CVAR_MOD))
{
Printf(TEXTCOLOR_RED "Cannot set console variable" TEXTCOLOR_GOLD " %s " TEXTCOLOR_RED "from unsafe command\n", var->GetName());
return;
}
var->CmdSet (args[1]); var->CmdSet (args[1]);
} }
else else
@ -707,9 +684,9 @@ DEFINE_ACTION_FUNCTION(DOptionMenuItemCommand, DoCommand)
if (CurrentMenu == nullptr) return 0; if (CurrentMenu == nullptr) return 0;
PARAM_PROLOGUE; PARAM_PROLOGUE;
PARAM_STRING(cmd); PARAM_STRING(cmd);
ParsingMenuDef = true; UnsafeExecutionContext = true;
C_DoCommand(cmd); C_DoCommand(cmd);
ParsingMenuDef = false; UnsafeExecutionContext = false;
return 0; return 0;
} }
@ -769,7 +746,7 @@ void AddCommandString (char *cmd, int keynum)
// Note that deferred commands lose track of which key // Note that deferred commands lose track of which key
// (if any) they were pressed from. // (if any) they were pressed from.
*brkpt = ';'; *brkpt = ';';
Create<DWaitingCommand> (brkpt, tics); Create<DWaitingCommand> (brkpt, tics, UnsafeExecutionContext);
} }
return; return;
} }
@ -1061,6 +1038,17 @@ void FConsoleCommand::Run (FCommandLine &argv, APlayerPawn *who, int key)
m_RunFunc (argv, who, key); m_RunFunc (argv, who, key);
} }
void FUnsafeConsoleCommand::Run (FCommandLine &args, APlayerPawn *instigator, int key)
{
if (UnsafeExecutionContext)
{
Printf(TEXTCOLOR_RED "Cannot execute unsafe command " TEXTCOLOR_GOLD "%s\n", m_Name);
return;
}
FConsoleCommand::Run (args, instigator, key);
}
FConsoleAlias::FConsoleAlias (const char *name, const char *command, bool noSave) FConsoleAlias::FConsoleAlias (const char *name, const char *command, bool noSave)
: FConsoleCommand (name, NULL), : FConsoleCommand (name, NULL),
bRunning(false), bKill(false) bRunning(false), bKill(false)
@ -1381,9 +1369,13 @@ CCMD (alias)
alias = NULL; alias = NULL;
} }
} }
else if (ParsingKeyConf)
{
new FUnsafeConsoleAlias (argv[1], argv[2]);
}
else else
{ {
new FConsoleAlias (argv[1], argv[2], ParsingKeyConf); new FConsoleAlias (argv[1], argv[2], false);
} }
} }
} }
@ -1521,6 +1513,13 @@ void FConsoleAlias::SafeDelete ()
} }
} }
void FUnsafeConsoleAlias::Run (FCommandLine &args, APlayerPawn *instigator, int key)
{
UnsafeExecutionContext = true;
FConsoleAlias::Run(args, instigator, key);
UnsafeExecutionContext = false;
}
void FExecList::AddCommand(const char *cmd, const char *file) void FExecList::AddCommand(const char *cmd, const char *file)
{ {
// Pullins are special and need to be separated from general commands. // Pullins are special and need to be separated from general commands.

View file

@ -127,6 +127,22 @@ protected:
FConsoleCommand Cmd_##n##_Ref (#n, Cmd_##n); \ FConsoleCommand Cmd_##n##_Ref (#n, Cmd_##n); \
void Cmd_##n (FCommandLine &argv, APlayerPawn *who, int key) void Cmd_##n (FCommandLine &argv, APlayerPawn *who, int key)
class FUnsafeConsoleCommand : public FConsoleCommand
{
public:
FUnsafeConsoleCommand (const char *name, CCmdRun RunFunc)
: FConsoleCommand (name, RunFunc)
{
}
virtual void Run (FCommandLine &args, APlayerPawn *instigator, int key) override;
};
#define UNSAFE_CCMD(n) \
static void Cmd_##n (FCommandLine &, APlayerPawn *, int key); \
static FUnsafeConsoleCommand Cmd_##n##_Ref (#n, Cmd_##n); \
void Cmd_##n (FCommandLine &argv, APlayerPawn *who, int key)
const int KEY_DBLCLICKED = 0x8000; const int KEY_DBLCLICKED = 0x8000;
class FConsoleAlias : public FConsoleCommand class FConsoleAlias : public FConsoleCommand
@ -147,6 +163,17 @@ protected:
bool bKill; bool bKill;
}; };
class FUnsafeConsoleAlias : public FConsoleAlias
{
public:
FUnsafeConsoleAlias (const char *name, const char *command)
: FConsoleAlias (name, command, true)
{
}
virtual void Run (FCommandLine &args, APlayerPawn *instigator, int key) override;
};
// Actions // Actions
struct FButtonStatus struct FButtonStatus
{ {

View file

@ -2821,7 +2821,7 @@ void D_DoomMain (void)
// //
//========================================================================== //==========================================================================
CCMD(restart) UNSAFE_CCMD(restart)
{ {
// remove command line args that would get in the way during restart // remove command line args that would get in the way during restart
Args->RemoveArgs("-iwad"); Args->RemoveArgs("-iwad");
@ -2917,4 +2917,4 @@ CUSTOM_CVAR(Bool, I_FriendlyWindowTitle, true, CVAR_GLOBALCONFIG|CVAR_ARCHIVE|CV
I_SetWindowTitle(DoomStartupInfo.Name.GetChars()); I_SetWindowTitle(DoomStartupInfo.Name.GetChars());
else else
I_SetWindowTitle(NULL); I_SetWindowTitle(NULL);
} }

View file

@ -2618,7 +2618,7 @@ CCMD (playdemo)
} }
} }
CCMD (timedemo) UNSAFE_CCMD (timedemo)
{ {
if (argv.argc() > 1) if (argv.argc() > 1)
{ {

View file

@ -224,7 +224,7 @@ CCMD (map)
// //
//========================================================================== //==========================================================================
CCMD(recordmap) UNSAFE_CCMD(recordmap)
{ {
if (netgame) if (netgame)
{ {
@ -277,7 +277,7 @@ CCMD(recordmap)
// //
//========================================================================== //==========================================================================
CCMD (open) UNSAFE_CCMD (open)
{ {
if (netgame) if (netgame)
{ {

View file

@ -315,7 +315,7 @@ void M_SaveDefaultsFinal ()
GameConfig = NULL; GameConfig = NULL;
} }
CCMD (writeini) UNSAFE_CCMD (writeini)
{ {
const char *filename = (argv.argc() == 1) ? NULL : argv[1]; const char *filename = (argv.argc() == 1) ? NULL : argv[1];
if (!M_SaveDefaults (filename)) if (!M_SaveDefaults (filename))

View file

@ -708,7 +708,7 @@ ADD_STAT(music)
// //
//========================================================================== //==========================================================================
CCMD (writeopl) UNSAFE_CCMD (writeopl)
{ {
if (argv.argc() == 2) if (argv.argc() == 2)
{ {
@ -746,7 +746,7 @@ CCMD (writeopl)
// //
//========================================================================== //==========================================================================
CCMD (writewave) UNSAFE_CCMD (writewave)
{ {
if (argv.argc() >= 2 && argv.argc() <= 3) if (argv.argc() >= 2 && argv.argc() <= 3)
{ {
@ -784,7 +784,7 @@ CCMD (writewave)
// //
//========================================================================== //==========================================================================
CCMD (writemidi) UNSAFE_CCMD (writemidi)
{ {
if (argv.argc() != 2) if (argv.argc() != 2)
{ {