Skip to content

Commit

Permalink
fix: handle raxNotFound (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonShining committed Jan 3, 2021
1 parent 1026139 commit 7ef9641
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ffi_cdef[[
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
void *radix_tree_search(void *t, void *it, const unsigned char *buf,
size_t len);
int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
int radix_tree_prev(void *it, const unsigned char *buf, size_t len);
int radix_tree_stop(void *it);
void *radix_tree_new_it(void *t);
Expand All @@ -131,6 +131,9 @@ end

local _M = { _VERSION = 1.7 }

-- expose radix tree api for test
_M._symbols = radix


local function has_suffix(s, suffix)
if type(s) ~= "string" or type(suffix) ~= "string" then
Expand Down Expand Up @@ -216,7 +219,7 @@ local function insert_route(self, opts)
end

local data_idx = radix.radix_tree_find(self.tree, path, #path)
if data_idx then
if data_idx ~= nil then
local idx = tonumber(ffi_cast('intptr_t', data_idx))
local routes = self.match_data[idx]
if routes and routes[1].path == path then
Expand Down Expand Up @@ -644,7 +647,7 @@ local function match_route(self, path, opts, args)
end

while true do
local idx = radix.radix_tree_pcre(it, path, #path)
local idx = radix.radix_tree_prev(it, path, #path)
if idx <= 0 then
break
end
Expand Down
9 changes: 7 additions & 2 deletions src/easy_rax.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ radix_tree_find(void *t, const unsigned char *buf, size_t len)
return NULL;
}

return raxFind((rax *)t, (unsigned char *)buf, len);
void *res = raxFind((rax *)t, (unsigned char *)buf, len);
if (res == raxNotFound) {
return NULL;
}

return res;
}


Expand Down Expand Up @@ -125,7 +130,7 @@ radix_tree_next(void *it, const unsigned char *buf, size_t len)


int
radix_tree_pcre(void *it, const unsigned char *buf, size_t len)
radix_tree_prev(void *it, const unsigned char *buf, size_t len)
{
raxIterator *iter = it;
int res;
Expand Down
2 changes: 1 addition & 1 deletion src/easy_rax.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
int idx);
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
void *radix_tree_search(void *t, void *it, const unsigned char *buf, size_t len);
int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
int radix_tree_prev(void *it, const unsigned char *buf, size_t len);
int radix_tree_next(void *it, const unsigned char *buf, size_t len);
int radix_tree_stop(void *it);

Expand Down
54 changes: 54 additions & 0 deletions t/rax.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:

use t::RX 'no_plan';

repeat_each(1);
run_tests();

__DATA__
=== TEST 1: not found
--- config
location /t {
content_by_lua_block {
local ffi = require("ffi")
local radix = require("resty.radixtree")
local radix_symbols = radix._symbols
local tree = radix_symbols.radix_tree_new()
local foo = "foo"
local data_idx = radix_symbols.radix_tree_find(tree, foo, #foo)
local idx = tonumber(ffi.cast('intptr_t', data_idx))
ngx.say(idx)
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
0
=== TEST 2: rax not init
--- config
location /t {
content_by_lua_block {
local ffi = require("ffi")
local radix = require("resty.radixtree")
local radix_symbols = radix._symbols
local tree = nil
local foo = "foo"
local data_idx = radix_symbols.radix_tree_find(tree, foo, #foo)
local idx = tonumber(ffi.cast('intptr_t', data_idx))
ngx.say(idx)
}
}
--- request
GET /t
--- no_error_log
[error]
--- response_body
0

0 comments on commit 7ef9641

Please sign in to comment.