lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have created an updated version of my Loop overflow patch, please find it
attached to this post.

In my previous patch I moved the pre-adjustment of the "step" value from the
initial value to the limit value; this simply moved the edge case from one
variable to the other, even if it did fix a number of normal loop use cases.

This patch simply adjusts the logic of the loop instead of pre-adjusting the
initial or limit values. Now, when the loop iterates an additional
comparison is done after the limit check. If the difference between the
limit and current value is greater or equal to the current step value do
another loop iteration, if not abort the loop cycle.

This now allows the following test script to succeed on all edge cases... if
there are any other cases I have forgotten about, or if this patch has side
effects I am unaware of please let me know. The patch fully passes the Lua
Test Suite as well... although perhaps some of these edge cases should be
added to the suite? :)


local write = function(...)
    io.write(...)
    io.flush()
end

local testloop = function(out,init,limit,step,reps)
    local count = 0
    write(out .. ": ")
    for i = init, limit, step do count = count + 1 end
    print(count .. " = " .. reps);
    assert(count == reps)
end

testloop("A", math.maxinteger, math.maxinteger, 1, 1)
testloop("B", math.maxinteger, math.maxinteger, -1, 1)
testloop("C", math.maxinteger - 100, math.maxinteger, 10, 11)
testloop("D", math.maxinteger, math.maxinteger - 100, -10, 11)
testloop("E", math.maxinteger - 95, math.maxinteger, 10, 10)
testloop("F", math.maxinteger, math.maxinteger - 95, -10, 10)

testloop("G", math.mininteger, math.mininteger, 1, 1)
testloop("H", math.mininteger, math.mininteger, -1, 1)
testloop("I", math.mininteger, math.mininteger + 100, 10, 11)
testloop("J", math.mininteger + 100, math.mininteger, -10, 11)
testloop("K", math.mininteger, math.mininteger + 95, 10, 10)
testloop("L", math.mininteger + 95, math.mininteger, -10, 10)

testloop("M", 0x7FFFFFFFFFFFFFF8, 0x7FFFFFFFFFFFFFFF, 1, 8)
testloop("N", 0x7FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFF8, -1, 8)


~Paige

Attachment: loopfix.patch
Description: Binary data