Skip to content

Commit ebb8f5c

Browse files
committed
code style fix & bugfix: use the wrong length for create list node
1 parent ed408d6 commit ebb8f5c

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

README.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5878,7 +5878,7 @@ ngx.shared.DICT.lpush
58785878

58795879
**context:** *init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.**
58805880

5881-
Insert the specified (numerical or string) `value` at the head of the list named `key` in the shm-based dictionary [ngx.shared.DICT](#ngxshareddict). Returns the length of the list after the push operations.
5881+
Inserts the specified (numerical or string) `value` at the head of the list named `key` in the shm-based dictionary [ngx.shared.DICT](#ngxshareddict). Returns the length of the list after the push operations.
58825882

58835883
If `key` does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, it will return `nil` and `"wrongtype operation"`.
58845884

@@ -5896,7 +5896,7 @@ ngx.shared.DICT.rpush
58965896

58975897
**context:** *init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.**
58985898

5899-
Similar to the [lpush](#ngxshareddictlpush) method, but insert the specified (numerical or string) `value` at the tail of the list named `key`.
5899+
Similar to the [lpush](#ngxshareddictlpush) method, but inserts the specified (numerical or string) `value` at the tail of the list named `key`.
59005900

59015901
This feature was first introduced in the `v0.*.*` release.
59025902

doc/HttpLuaModule.wiki

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4911,7 +4911,7 @@ See also [[#ngx.shared.DICT|ngx.shared.DICT]].
49114911
49124912
'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*''
49134913
4914-
Insert the specified (numerical or string) <code>value</code> at the head of the list named <code>key</code> in the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]]. Returns the length of the list after the push operations.
4914+
Inserts the specified (numerical or string) <code>value</code> at the head of the list named <code>key</code> in the shm-based dictionary [[#ngx.shared.DICT|ngx.shared.DICT]]. Returns the length of the list after the push operations.
49154915
49164916
If <code>key</code> does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, it will return <code>nil</code> and <code>"wrongtype operation"</code>.
49174917
@@ -4926,7 +4926,7 @@ See also [[#ngx.shared.DICT|ngx.shared.DICT]].
49264926
49274927
'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*''
49284928
4929-
Similar to the [[#ngx.shared.DICT.lpush|lpush]] method, but insert the specified (numerical or string) <code>value</code> at the tail of the list named <code>key</code>.
4929+
Similar to the [[#ngx.shared.DICT.lpush|lpush]] method, but inserts the specified (numerical or string) <code>value</code> at the tail of the list named <code>key</code>.
49304930
49314931
This feature was first introduced in the <code>v0.*.*</code> release.
49324932

src/ngx_http_lua_shdict.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n)
301301
list_queue = ngx_http_lua_shdict_get_list_head(sd, sd->key_len);
302302

303303
for (lq = ngx_queue_head(list_queue);
304-
lq != ngx_queue_sentinel(list_queue);
305-
lq = ngx_queue_next(lq))
304+
lq != ngx_queue_sentinel(list_queue);
305+
lq = ngx_queue_next(lq))
306306
{
307307
lnode = ngx_queue_data(lq, ngx_http_lua_shdict_list_node_t,
308308
queue);
@@ -543,6 +543,7 @@ ngx_http_lua_shdict_get_helper(lua_State *L, int get_stale)
543543
value.len = (size_t) sd->value_len;
544544

545545
switch (value_type) {
546+
546547
case LUA_TSTRING:
547548

548549
lua_pushlstring(L, (char *) value.data, value.len);
@@ -745,8 +746,8 @@ ngx_http_lua_shdict_flush_expired(lua_State *L)
745746
list_queue = ngx_http_lua_shdict_get_list_head(sd, sd->key_len);
746747

747748
for (lq = ngx_queue_head(list_queue);
748-
lq != ngx_queue_sentinel(list_queue);
749-
lq = ngx_queue_next(lq))
749+
lq != ngx_queue_sentinel(list_queue);
750+
lq = ngx_queue_next(lq))
750751
{
751752
lnode = ngx_queue_data(lq, ngx_http_lua_shdict_list_node_t,
752753
queue);
@@ -980,6 +981,7 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
980981
value_type = lua_type(L, 3);
981982

982983
switch (value_type) {
984+
983985
case LUA_TSTRING:
984986
value.data = (u_char *) lua_tolstring(L, 3, &value.len);
985987
break;
@@ -1081,7 +1083,8 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
10811083

10821084
replace:
10831085

1084-
if (value.data && value.len == (size_t) sd->value_len
1086+
if (value.data
1087+
&& value.len == (size_t) sd->value_len
10851088
&& sd->value_type != LUA_TTABLE)
10861089
{
10871090

@@ -1132,8 +1135,8 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
11321135
queue = ngx_http_lua_shdict_get_list_head(sd, key.len);
11331136

11341137
for (q = ngx_queue_head(queue);
1135-
q != ngx_queue_sentinel(queue);
1136-
q = ngx_queue_next(q))
1138+
q != ngx_queue_sentinel(queue);
1139+
q = ngx_queue_next(q))
11371140
{
11381141
p = (u_char *) ngx_queue_data(q,
11391142
ngx_http_lua_shdict_list_node_t,
@@ -1399,6 +1402,7 @@ ngx_http_lua_shared_dict_get(ngx_shm_zone_t *zone, u_char *key_data,
13991402
len = (size_t) sd->value_len;
14001403

14011404
switch (value->type) {
1405+
14021406
case LUA_TSTRING:
14031407

14041408
if (value->value.s.data == NULL || value->value.s.len == 0) {
@@ -1534,6 +1538,7 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
15341538
value_type = lua_type(L, 3);
15351539

15361540
switch (value_type) {
1541+
15371542
case LUA_TSTRING:
15381543
value.data = (u_char *) lua_tolstring(L, 3, &value.len);
15391544
break;
@@ -1594,8 +1599,8 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
15941599
queue = ngx_http_lua_shdict_get_list_head(sd, key.len);
15951600

15961601
for (q = ngx_queue_head(queue);
1597-
q != ngx_queue_sentinel(queue);
1598-
q = ngx_queue_next(q))
1602+
q != ngx_queue_sentinel(queue);
1603+
q = ngx_queue_next(q))
15991604
{
16001605
/* TODO: reuse matched size list node */
16011606
lnode = ngx_queue_data(q, ngx_http_lua_shdict_list_node_t, queue);
@@ -1686,11 +1691,12 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
16861691
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
16871692
"lua shared dict list: creating a new list node");
16881693

1689-
n = sizeof(ngx_http_lua_shdict_list_node_t)
1694+
n = offsetof(ngx_http_lua_shdict_list_node_t, data)
16901695
+ value.len;
16911696

1692-
lnode = ngx_slab_alloc_locked(ctx->shpool,
1693-
sizeof(ngx_http_lua_shdict_list_node_t));
1697+
dd("list node length: %d", n);
1698+
1699+
lnode = ngx_slab_alloc_locked(ctx->shpool, n);
16941700

16951701
if (lnode == NULL) {
16961702

@@ -1759,7 +1765,6 @@ ngx_http_lua_shdict_rpop(lua_State *L)
17591765
}
17601766

17611767

1762-
17631768
static int
17641769
ngx_http_lua_shdict_pop_helper(lua_State *L, int flags)
17651770
{
@@ -1873,6 +1878,7 @@ ngx_http_lua_shdict_pop_helper(lua_State *L, int flags)
18731878
value.len = (size_t) lnode->value_len;
18741879

18751880
switch (value_type) {
1881+
18761882
case LUA_TSTRING:
18771883

18781884
lua_pushlstring(L, (char *) value.data, value.len);
@@ -2184,6 +2190,7 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
21842190
hash = ngx_crc32_short(key, key_len);
21852191

21862192
switch (value_type) {
2193+
21872194
case LUA_TSTRING:
21882195
/* do nothing */
21892196
break;
@@ -2232,6 +2239,7 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
22322239
*errmsg = "not found";
22332240
return NGX_DECLINED;
22342241
}
2242+
22352243
/* rc == NGX_OK */
22362244

22372245
goto replace;
@@ -2266,7 +2274,8 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
22662274

22672275
replace:
22682276

2269-
if (str_value_buf && str_value_len == (size_t) sd->value_len
2277+
if (str_value_buf
2278+
&& str_value_len == (size_t) sd->value_len
22702279
&& sd->value_type != LUA_TTABLE)
22712280
{
22722281

@@ -2315,8 +2324,8 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
23152324
queue = ngx_http_lua_shdict_get_list_head(sd, key_len);
23162325

23172326
for (q = ngx_queue_head(queue);
2318-
q != ngx_queue_sentinel(queue);
2319-
q = ngx_queue_next(q))
2327+
q != ngx_queue_sentinel(queue);
2328+
q = ngx_queue_next(q))
23202329
{
23212330
p = (u_char *) ngx_queue_data(q,
23222331
ngx_http_lua_shdict_list_node_t,
@@ -2493,6 +2502,7 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
24932502
}
24942503

24952504
switch (*value_type) {
2505+
24962506
case LUA_TSTRING:
24972507
*str_value_len = value.len;
24982508
ngx_memcpy(*str_value_buf, value.data, value.len);

src/ngx_http_lua_shdict.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ typedef struct {
2525

2626

2727
typedef struct {
28-
uint8_t value_type;
29-
uint32_t value_len;
3028
ngx_queue_t queue;
29+
uint32_t value_len;
30+
uint8_t value_type;
3131
u_char data[1];
3232
} ngx_http_lua_shdict_list_node_t;
3333

t/133-shdict-list.t

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,39 @@ keys number: 0
678678
--- no_error_log
679679
[error]
680680

681+
682+
683+
=== TEST 17: long list node
684+
--- http_config
685+
lua_shared_dict dogs 1m;
686+
--- config
687+
location = /test {
688+
content_by_lua_block {
689+
local dogs = ngx.shared.dogs
690+
691+
local long_str = string.rep("foo", 10)
692+
693+
for i = 1, 3 do
694+
local len, err = dogs:lpush("list", long_str)
695+
if not len then
696+
ngx.say("push err: ", err)
697+
end
698+
end
699+
700+
for i = 1, 3 do
701+
local val, err = dogs:lpop("list")
702+
if val then
703+
ngx.say(val)
704+
end
705+
end
706+
}
707+
}
708+
--- request
709+
GET /test
710+
--- response_body
711+
foofoofoofoofoofoofoofoofoofoo
712+
foofoofoofoofoofoofoofoofoofoo
713+
foofoofoofoofoofoofoofoofoofoo
714+
--- no_error_log
715+
[error]
716+

0 commit comments

Comments
 (0)