Skip to content

Commit 7ef9641

Browse files
authored
fix: handle raxNotFound (#85)
1 parent 1026139 commit 7ef9641

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

lib/resty/radixtree.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ffi_cdef[[
114114
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
115115
void *radix_tree_search(void *t, void *it, const unsigned char *buf,
116116
size_t len);
117-
int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
117+
int radix_tree_prev(void *it, const unsigned char *buf, size_t len);
118118
int radix_tree_stop(void *it);
119119
120120
void *radix_tree_new_it(void *t);
@@ -131,6 +131,9 @@ end
131131

132132
local _M = { _VERSION = 1.7 }
133133

134+
-- expose radix tree api for test
135+
_M._symbols = radix
136+
134137

135138
local function has_suffix(s, suffix)
136139
if type(s) ~= "string" or type(suffix) ~= "string" then
@@ -216,7 +219,7 @@ local function insert_route(self, opts)
216219
end
217220

218221
local data_idx = radix.radix_tree_find(self.tree, path, #path)
219-
if data_idx then
222+
if data_idx ~= nil then
220223
local idx = tonumber(ffi_cast('intptr_t', data_idx))
221224
local routes = self.match_data[idx]
222225
if routes and routes[1].path == path then
@@ -644,7 +647,7 @@ local function match_route(self, path, opts, args)
644647
end
645648

646649
while true do
647-
local idx = radix.radix_tree_pcre(it, path, #path)
650+
local idx = radix.radix_tree_prev(it, path, #path)
648651
if idx <= 0 then
649652
break
650653
end

src/easy_rax.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ radix_tree_find(void *t, const unsigned char *buf, size_t len)
7272
return NULL;
7373
}
7474

75-
return raxFind((rax *)t, (unsigned char *)buf, len);
75+
void *res = raxFind((rax *)t, (unsigned char *)buf, len);
76+
if (res == raxNotFound) {
77+
return NULL;
78+
}
79+
80+
return res;
7681
}
7782

7883

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

126131

127132
int
128-
radix_tree_pcre(void *it, const unsigned char *buf, size_t len)
133+
radix_tree_prev(void *it, const unsigned char *buf, size_t len)
129134
{
130135
raxIterator *iter = it;
131136
int res;

src/easy_rax.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int radix_tree_insert(void *t, const unsigned char *buf, size_t len,
5555
int idx);
5656
void *radix_tree_find(void *t, const unsigned char *buf, size_t len);
5757
void *radix_tree_search(void *t, void *it, const unsigned char *buf, size_t len);
58-
int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
58+
int radix_tree_prev(void *it, const unsigned char *buf, size_t len);
5959
int radix_tree_next(void *it, const unsigned char *buf, size_t len);
6060
int radix_tree_stop(void *it);
6161

t/rax.t

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use t::RX 'no_plan';
4+
5+
repeat_each(1);
6+
run_tests();
7+
8+
__DATA__
9+
10+
=== TEST 1: not found
11+
--- config
12+
location /t {
13+
content_by_lua_block {
14+
local ffi = require("ffi")
15+
local radix = require("resty.radixtree")
16+
local radix_symbols = radix._symbols
17+
18+
local tree = radix_symbols.radix_tree_new()
19+
local foo = "foo"
20+
local data_idx = radix_symbols.radix_tree_find(tree, foo, #foo)
21+
22+
local idx = tonumber(ffi.cast('intptr_t', data_idx))
23+
ngx.say(idx)
24+
}
25+
}
26+
--- request
27+
GET /t
28+
--- no_error_log
29+
[error]
30+
--- response_body
31+
0
32+
33+
=== TEST 2: rax not init
34+
--- config
35+
location /t {
36+
content_by_lua_block {
37+
local ffi = require("ffi")
38+
local radix = require("resty.radixtree")
39+
local radix_symbols = radix._symbols
40+
41+
local tree = nil
42+
local foo = "foo"
43+
local data_idx = radix_symbols.radix_tree_find(tree, foo, #foo)
44+
45+
local idx = tonumber(ffi.cast('intptr_t', data_idx))
46+
ngx.say(idx)
47+
}
48+
}
49+
--- request
50+
GET /t
51+
--- no_error_log
52+
[error]
53+
--- response_body
54+
0

0 commit comments

Comments
 (0)