我的代码如下:
local ffi = require "ffi"
local ffi_C = ffi.C
local ffi_typeof = ffi.typeof
local ffi_new = ffi.new
local ffi_string = ffi.string
local NULL = ngx.null
local tostring = tostring
ffi.cdef[[
char * strtok(char * str, const char * delimiters);
]]
local p_char_type = ffi_typeof("char[?]")
function split(src, c)
local result = {}
local pch = ffi_new(p_char_type, 1)
local psrc = ffi_new(p_char_type, #src)
local pc = ffi_new(p_char_type, #c)
ffi.copy(psrc, src)
ffi.copy(pc, c)
pch = ffi_C.strtok(psrc, pc)
while pch do
table.insert(result, ffi_string(pch))
pch = ffi_C.strtok(NULL, pc)
ngx.log(ngx.ERR, "pch ok")
end
ngx.log(ngx.ERR, "split ok")
return result
end当我运行我的nginx时,发生了一些错误!在while循环返回后,nginx工作进程崩溃并返回信号11。最后一个ngx.log无法运行。我该怎么处理呢?
发布于 2014-06-05 01:57:42
local psrc = ffi_new(p_char_type, #src)
ffi.copy(psrc, src)当给定字符串源时,ffi.copy也会复制空终止符,但是您的数组太小,无法容纳它,从而导致溢出。
此外,不要使用strtok,而要考虑使用Lua模式。它们更安全,更容易使用,而且不依赖于FFI。
https://stackoverflow.com/questions/24031289
复制相似问题