Skip to content

Commit

Permalink
fix: prevent divide-by-zero crashes in svar arithmetic
Browse files Browse the repository at this point in the history
Resolves: #12
  • Loading branch information
mlugg committed Jul 29, 2021
1 parent abe929a commit 96d2971
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Features/ConfigPlus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ CON_COMMAND_F(svar_from_cvar, "svar_from_cvar <variable> <cvar> - capture a cvar
}
}

#define SVAR_OP(name, op) \
#define SVAR_OP(name, op, disallowSecondZero) \
CON_COMMAND_F(svar_##name, "svar_" #name " <variable> <variable|value> - perform the given operation on an svar\n", FCVAR_DONTRECORD) { \
if (args.ArgC() != 3) { \
return console->Print(svar_##name.ThisPtr()->m_pszHelpString); \
} \
\
long cur; \
int cur; \
\
{ \
auto it = g_svars.find({args[1]}); \
Expand All @@ -137,7 +137,7 @@ CON_COMMAND_F(svar_from_cvar, "svar_from_cvar <variable> <cvar> - capture a cvar
} \
\
char *end; \
long other = strtol(args[2], &end, 10); \
int other = strtol(args[2], &end, 10); \
\
if (end == args[2] || *end) { \
auto it = g_svars.find({args[2]}); \
Expand All @@ -148,14 +148,15 @@ CON_COMMAND_F(svar_from_cvar, "svar_from_cvar <variable> <cvar> - capture a cvar
} \
} \
\
g_svars[std::string(args[1])] = Utils::ssprintf("%d", cur op other); \
int val = (disallowSecondZero && other == 0) ? 0 : cur op other; \
g_svars[std::string(args[1])] = Utils::ssprintf("%d", val); \
}

SVAR_OP(add, +)
SVAR_OP(sub, -)
SVAR_OP(mul, *)
SVAR_OP(div, /)
SVAR_OP(mod, %)
SVAR_OP(add, +, false)
SVAR_OP(sub, -, false)
SVAR_OP(mul, *, false)
SVAR_OP(div, /, true)
SVAR_OP(mod, %, true)

struct Condition {
enum {
Expand Down

0 comments on commit 96d2971

Please sign in to comment.