Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prefix): fix bug when buffer realloc occurs during prefix lookup #61

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/resty/lmdb/prefix.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
local _M = {}


Expand Down Expand Up @@ -36,14 +36,6 @@
local value_buf_size = get_string_buf_size()
local ops = ffi_new("ngx_lua_resty_lmdb_operation_t[?]", page_size)

ops[0].opcode = C.NGX_LMDB_OP_PREFIX
ops[0].key.data = start
ops[0].key.len = #start

ops[1].opcode = C.NGX_LMDB_OP_PREFIX
ops[1].key.data = prefix
ops[1].key.len = #prefix

local dbi, err = get_dbi(false, db or DEFAULT_DB)
if err then
return nil, "unable to open DB for access: " .. err
Expand All @@ -55,6 +47,14 @@
ops[0].dbi = dbi

::again::
ops[0].opcode = C.NGX_LMDB_OP_PREFIX
ops[0].key.data = start
ops[0].key.len = #start
chronolaw marked this conversation as resolved.
Show resolved Hide resolved

ops[1].opcode = C.NGX_LMDB_OP_PREFIX
ops[1].key.data = prefix
ops[1].key.len = #prefix

local buf = get_string_buf(value_buf_size, false)
local ret = C.ngx_lua_resty_lmdb_ffi_prefix(ops, page_size,
buf, value_buf_size, err_ptr)
Expand Down
3 changes: 2 additions & 1 deletion src/ngx_lua_resty_lmdb_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ int ngx_lua_resty_lmdb_ffi_execute(ngx_lua_resty_lmdb_operation_t *ops,
rc = mdb_txn_commit(txn);
if (rc != 0) {
*err = mdb_strerror(rc);
goto err;

return NGX_ERROR;
}

} else {
Expand Down
75 changes: 75 additions & 0 deletions t/10-prefix.t
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,78 @@ false.../lua-resty-lmdb/lua-resty-lmdb/lib/resty/lmdb/prefix.lua:34: 'page_size'
[error]
[warn]
[crit]



=== TEST 9: prefix.page() operation with large page size [KAG-5874]
--- http_config eval: $::HttpConfig
--- main_config eval: $::MainConfig
--- config
location = /t {
content_by_lua_block {
local l = require("resty.lmdb")

ngx.say(l.db_drop(true))

ngx.say(l.set("test", "value"))

local inserted = { test = "value" }

for i = 1, 2048 do
-- string.rep with 120 makes sure each page goes just over the 1MB
-- default buffer size and triggers a realloc
assert(l.set(string.rep("test", 120) .. i, string.rep("value", 120)))
inserted[string.rep("test", 120) .. i] = string.rep("value", 120)
end

ngx.say(l.set("u", "value4"))
ngx.say(l.set("u1", "value5"))

local p = require("resty.lmdb.prefix")

local res, err = p.page("test", "test", nil, 1000)
if not res then
ngx.say("page errored: ", err)
end

ngx.say("FIRST PAGE")
for _, pair in ipairs(res) do
inserted[pair.key] = nil
end

res, err = p.page(res[#res].key .. "\x00", "test", nil, 1000)
if not res then
ngx.say("page errored: ", err)
end
ngx.say("SECOND PAGE")
for _, pair in ipairs(res) do
inserted[pair.key] = nil
end

res, err = p.page(res[#res].key .. "\x00", "test", nil, 1000)
if not res then
ngx.say("page errored: ", err)
end
ngx.say("THIRD PAGE")
for _, pair in ipairs(res) do
inserted[pair.key] = nil
end

ngx.say(next(inserted))
}
}
--- request
GET /t
--- response_body
true
true
true
true
FIRST PAGE
SECOND PAGE
THIRD PAGE
nil
--- no_error_log
[error]
[warn]
[crit]
Loading