gpt4 book ai didi

lua - 是否有一个选项可以让 LuaJIT 进行边界检查?

转载 作者:行者123 更新时间:2023-12-02 08:43:11 25 4
gpt4 key购买 nike

LuaJIT 知道它定义的 C 类型以及数组的长度,但它不检查边界:

ffi = require("ffi")

ten_ints = ffi.typeof("int [10]")
p1 = ten_ints()
print(ffi.sizeof(p1)) -- 40


var_ints = ffi.typeof("int [?]")
p2 = ffi.new(var_ints, 10)
print(ffi.sizeof(p2)) -- 40

p1[1000000] = 1 -- segfault
p2[1000000] = 1 -- segfault

有没有办法让它做到这一点,或者是我编写包装器的唯一选择?

最佳答案

简短的回答:没有办法,你必须编写/找到自己的包装器。

这是luajit.org的解释

No Hand-holding!

[...] The FFI library provides no memory safety, unlike regular Lua code. It will happily allow you to dereference a NULL pointer, to access arrays out of bounds or to misdeclare C functions. If you make a mistake, your application might crash, just like equivalent C code would. This behavior is inevitable, since the goal is to provide full interoperability with C code. Adding extra safety measures, like bounds checks, would be futile. [...] Likewise there's no way to infer the valid range of indexes for a returned pointer. Again: the FFI library is a low-level library.

关于lua - 是否有一个选项可以让 LuaJIT 进行边界检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29977289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com