unexpected index -1 comes up when copy a table with only one element

9 views
Skip to first unread message

xiang...@gmail.com

unread,
Nov 18, 2022, 8:48:43 PM11/18/22
to openresty-en
this is my origin post:
unexpected index -1 comes up when copy a table with only one element · Issue #188 · openresty/luajit2 (github.com)

test.lua
```lua
local function jcopy(o)
  local function copy(v)
    if type(v) == "table" then
      local cv = {}
      for key, value in pairs(v) do
        if key == -1 then
          error(tostring(key))
        end
        cv[key] = copy(value)
      end
      return cv
    else
      return v
    end
  end

  return copy(o)
end

local function main()
  local n = 0
  for i = 1, 100, 1 do
    local c = jcopy { cond = { { "you shouldn't see this via index -1" } } }
    if c.cond[-1] then
      if n == 0 then
        -- print for the first time
        print(c.cond[-1][1])
      end
      n = n + 1
    end
  end
  if n > 0 then
    print("-1 index happends:" .. n)
  end
end

main()

```
then run `test.lua` 50 times
```sh
for ((i=1; i<=50; i++)); do resty test.lua; done
```
you will get output like:
```
root@DESKTOP-S3UK088:~/rsks# for ((i=1; i<=50; i++))
> do
>   resty test.lua
> done
you shouldn't see this via index -1
-1 index happends:77
you shouldn't see this via index -1
-1 index happends:29
you shouldn't see this via index -1
-1 index happends:8
you shouldn't see this via index -1
-1 index happends:6
you shouldn't see this via index -1
-1 index happends:38
```
But you shouldn't see any output if the copy work as expected.
this is the minimal repo: https://github.com/xiangnanscu/luajit-bug
Reply all
Reply to author
Forward
0 new messages