Skip to content

Commit

Permalink
Bug in [for] missing step
Browse files Browse the repository at this point in the history
https://forums.wesnoth.org/viewtopic.php?t=46450 reports a Lua crash if end is given but step is not. The wiki says the default step is 1. Note that if end is negative, the [for] does nothing (per sniity check).
  • Loading branch information
GregoryLundberg committed Jun 22, 2017
1 parent 0f44669 commit 468f073
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions data/lua/wml-flow.lua
Expand Up @@ -99,19 +99,17 @@ wesnoth.wml_actions["for"] = function(cfg)
local cfg_lit = helper.literal(cfg)
first = cfg.start or 0
loop_lim.last = cfg_lit["end"] or first
if cfg.step then loop_lim.step = cfg_lit.step end
loop_lim.step = cfg_lit.step or 1
end
loop_lim = wesnoth.tovconfig(loop_lim)
if loop_lim.step == 0 then -- Sanity check
helper.wml_error("[for] has a step of 0!")
end
if loop_lim.step ~= nil then
if (first < loop_lim.last and loop_lim.step <= 0)
or (first > loop_lim.last and loop_lim.step >= 0) then
-- Sanity check: If they specify something like start,end,step=1,4,-1
-- then we do nothing
return
end
if (first < loop_lim.last and loop_lim.step <= 0)
or (first > loop_lim.last and loop_lim.step >= 0) then
-- Sanity check: If they specify something like start,end,step=1,4,-1
-- then we do nothing
return
end
local i_var = cfg.variable or "i"
local save_i = utils.start_var_scope(i_var)
Expand Down

0 comments on commit 468f073

Please sign in to comment.