Skip to content

Commit cd483ec

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

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
@@ -6182,7 +6182,7 @@ ngx.shared.DICT.lpush
61826182

61836183
**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.**
61846184

6185-
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.
6185+
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.
61866186

61876187
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"`.
61886188

@@ -6200,7 +6200,7 @@ ngx.shared.DICT.rpush
62006200

62016201
**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.**
62026202

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

62056205
This feature was first introduced in the `v0.*.*` release.
62066206

doc/HttpLuaModule.wiki

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,7 +5174,7 @@ See also [[#ngx.shared.DICT|ngx.shared.DICT]].
51745174
51755175
'''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.*''
51765176
5177-
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.
5177+
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.
51785178
51795179
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>.
51805180
@@ -5189,7 +5189,7 @@ See also [[#ngx.shared.DICT|ngx.shared.DICT]].
51895189
51905190
'''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.*''
51915191
5192-
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>.
5192+
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>.
51935193
51945194
This feature was first introduced in the <code>v0.*.*</code> release.
51955195

src/ngx_http_lua_shdict.c

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

312312
for (lq = ngx_queue_head(list_queue);
313-
lq != ngx_queue_sentinel(list_queue);
314-
lq = ngx_queue_next(lq))
313+
lq != ngx_queue_sentinel(list_queue);
314+
lq = ngx_queue_next(lq))
315315
{
316316
lnode = ngx_queue_data(lq, ngx_http_lua_shdict_list_node_t,
317317
queue);
@@ -552,6 +552,7 @@ ngx_http_lua_shdict_get_helper(lua_State *L, int get_stale)
552552
value.len = (size_t) sd->value_len;
553553

554554
switch (value_type) {
555+
555556
case LUA_TSTRING:
556557

557558
lua_pushlstring(L, (char *) value.data, value.len);
@@ -754,8 +755,8 @@ ngx_http_lua_shdict_flush_expired(lua_State *L)
754755
list_queue = ngx_http_lua_shdict_get_list_head(sd, sd->key_len);
755756

756757
for (lq = ngx_queue_head(list_queue);
757-
lq != ngx_queue_sentinel(list_queue);
758-
lq = ngx_queue_next(lq))
758+
lq != ngx_queue_sentinel(list_queue);
759+
lq = ngx_queue_next(lq))
759760
{
760761
lnode = ngx_queue_data(lq, ngx_http_lua_shdict_list_node_t,
761762
queue);
@@ -989,6 +990,7 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
989990
value_type = lua_type(L, 3);
990991

991992
switch (value_type) {
993+
992994
case LUA_TSTRING:
993995
value.data = (u_char *) lua_tolstring(L, 3, &value.len);
994996
break;
@@ -1090,7 +1092,8 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
10901092

10911093
replace:
10921094

1093-
if (value.data && value.len == (size_t) sd->value_len
1095+
if (value.data
1096+
&& value.len == (size_t) sd->value_len
10941097
&& sd->value_type != LUA_TTABLE)
10951098
{
10961099

@@ -1141,8 +1144,8 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)
11411144
queue = ngx_http_lua_shdict_get_list_head(sd, key.len);
11421145

11431146
for (q = ngx_queue_head(queue);
1144-
q != ngx_queue_sentinel(queue);
1145-
q = ngx_queue_next(q))
1147+
q != ngx_queue_sentinel(queue);
1148+
q = ngx_queue_next(q))
11461149
{
11471150
p = (u_char *) ngx_queue_data(q,
11481151
ngx_http_lua_shdict_list_node_t,
@@ -1408,6 +1411,7 @@ ngx_http_lua_shared_dict_get(ngx_shm_zone_t *zone, u_char *key_data,
14081411
len = (size_t) sd->value_len;
14091412

14101413
switch (value->type) {
1414+
14111415
case LUA_TSTRING:
14121416

14131417
if (value->value.s.data == NULL || value->value.s.len == 0) {
@@ -1543,6 +1547,7 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
15431547
value_type = lua_type(L, 3);
15441548

15451549
switch (value_type) {
1550+
15461551
case LUA_TSTRING:
15471552
value.data = (u_char *) lua_tolstring(L, 3, &value.len);
15481553
break;
@@ -1603,8 +1608,8 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
16031608
queue = ngx_http_lua_shdict_get_list_head(sd, key.len);
16041609

16051610
for (q = ngx_queue_head(queue);
1606-
q != ngx_queue_sentinel(queue);
1607-
q = ngx_queue_next(q))
1611+
q != ngx_queue_sentinel(queue);
1612+
q = ngx_queue_next(q))
16081613
{
16091614
/* TODO: reuse matched size list node */
16101615
lnode = ngx_queue_data(q, ngx_http_lua_shdict_list_node_t, queue);
@@ -1695,11 +1700,12 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
16951700
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
16961701
"lua shared dict list: creating a new list node");
16971702

1698-
n = sizeof(ngx_http_lua_shdict_list_node_t)
1703+
n = offsetof(ngx_http_lua_shdict_list_node_t, data)
16991704
+ value.len;
17001705

1701-
lnode = ngx_slab_alloc_locked(ctx->shpool,
1702-
sizeof(ngx_http_lua_shdict_list_node_t));
1706+
dd("list node length: %d", n);
1707+
1708+
lnode = ngx_slab_alloc_locked(ctx->shpool, n);
17031709

17041710
if (lnode == NULL) {
17051711

@@ -1768,7 +1774,6 @@ ngx_http_lua_shdict_rpop(lua_State *L)
17681774
}
17691775

17701776

1771-
17721777
static int
17731778
ngx_http_lua_shdict_pop_helper(lua_State *L, int flags)
17741779
{
@@ -1882,6 +1887,7 @@ ngx_http_lua_shdict_pop_helper(lua_State *L, int flags)
18821887
value.len = (size_t) lnode->value_len;
18831888

18841889
switch (value_type) {
1890+
18851891
case LUA_TSTRING:
18861892

18871893
lua_pushlstring(L, (char *) value.data, value.len);
@@ -2193,6 +2199,7 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
21932199
hash = ngx_crc32_short(key, key_len);
21942200

21952201
switch (value_type) {
2202+
21962203
case LUA_TSTRING:
21972204
/* do nothing */
21982205
break;
@@ -2241,6 +2248,7 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
22412248
*errmsg = "not found";
22422249
return NGX_DECLINED;
22432250
}
2251+
22442252
/* rc == NGX_OK */
22452253

22462254
goto replace;
@@ -2275,7 +2283,8 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
22752283

22762284
replace:
22772285

2278-
if (str_value_buf && str_value_len == (size_t) sd->value_len
2286+
if (str_value_buf
2287+
&& str_value_len == (size_t) sd->value_len
22792288
&& sd->value_type != LUA_TTABLE)
22802289
{
22812290

@@ -2324,8 +2333,8 @@ ngx_http_lua_ffi_shdict_store(ngx_shm_zone_t *zone, int op, u_char *key,
23242333
queue = ngx_http_lua_shdict_get_list_head(sd, key_len);
23252334

23262335
for (q = ngx_queue_head(queue);
2327-
q != ngx_queue_sentinel(queue);
2328-
q = ngx_queue_next(q))
2336+
q != ngx_queue_sentinel(queue);
2337+
q = ngx_queue_next(q))
23292338
{
23302339
p = (u_char *) ngx_queue_data(q,
23312340
ngx_http_lua_shdict_list_node_t,
@@ -2502,6 +2511,7 @@ ngx_http_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key,
25022511
}
25032512

25042513
switch (*value_type) {
2514+
25052515
case LUA_TSTRING:
25062516
*str_value_len = value.len;
25072517
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)