Skip to content

Commit d800b14

Browse files
committed
optimize: shdict: switched exptime argument type to 'long' to avoid potential
overflows.
1 parent 52fb5fa commit d800b14

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ install:
5959
- git clone https://github.com/openresty/openresty.git ../openresty
6060
- git clone https://github.com/openresty/openresty-devel-utils.git
6161
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
62-
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
62+
- git clone -b refactor/ttl-use-long-type https://github.com/thibaultcha/lua-nginx-module.git ../lua-nginx-module
6363
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
6464
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
6565
- git clone https://github.com/openresty/lua-resty-lrucache.git

lib/resty/core/shdict.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ ffi.cdef[[
3434
int ngx_http_lua_ffi_shdict_store(void *zone, int op,
3535
const unsigned char *key, size_t key_len, int value_type,
3636
const unsigned char *str_value_buf, size_t str_value_len,
37-
double num_value, int exptime, int user_flags, char **errmsg,
37+
double num_value, long exptime, int user_flags, char **errmsg,
3838
int *forcible);
3939

4040
int ngx_http_lua_ffi_shdict_flush_all(void *zone);
4141

42-
int ngx_http_lua_ffi_shdict_get_ttl(void *zone,
43-
const unsigned char *key, size_t key_len);
42+
long ngx_http_lua_ffi_shdict_get_ttl(void *zone,
43+
const unsigned char *key, size_t key_len);
4444

4545
int ngx_http_lua_ffi_shdict_set_expire(void *zone,
46-
const unsigned char *key, size_t key_len, int exptime);
46+
const unsigned char *key, size_t key_len, long exptime);
4747

4848
size_t ngx_http_lua_ffi_shdict_capacity(void *zone);
4949
]]

t/shdict.t

+108
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,111 @@ foo after init_ttl = nil
15281528
[error]
15291529
[alert]
15301530
[crit]
1531+
1532+
1533+
1534+
=== TEST 47: exptime uses long type to avoid overflow in set() + ttl()
1535+
--- http_config eval: $::HttpConfig
1536+
--- config
1537+
location = /t {
1538+
content_by_lua_block {
1539+
local dogs = ngx.shared.dogs
1540+
dogs:flush_all()
1541+
1542+
local ok, err = dogs:set("huge_ttl", true, 2 ^ 31)
1543+
if not ok then
1544+
ngx.say("err setting: ", err)
1545+
return
1546+
end
1547+
1548+
local ttl, err = dogs:ttl("huge_ttl")
1549+
if not ttl then
1550+
ngx.say("err retrieving ttl: ", err)
1551+
return
1552+
end
1553+
1554+
ngx.say("ttl: ", ttl)
1555+
}
1556+
}
1557+
--- request
1558+
GET /t
1559+
--- response_body
1560+
ttl: 2147483648
1561+
--- no_error_log
1562+
[error]
1563+
[alert]
1564+
[crit]
1565+
1566+
1567+
1568+
=== TEST 48: exptime uses long type to avoid overflow in expire() + ttl()
1569+
--- http_config eval: $::HttpConfig
1570+
--- config
1571+
location = /t {
1572+
content_by_lua_block {
1573+
local dogs = ngx.shared.dogs
1574+
dogs:flush_all()
1575+
1576+
local ok, err = dogs:set("updated_huge_ttl", true)
1577+
if not ok then
1578+
ngx.say("err setting: ", err)
1579+
return
1580+
end
1581+
1582+
local ok, err = dogs:expire("updated_huge_ttl", 2 ^ 31)
1583+
if not ok then
1584+
ngx.say("err expire: ", err)
1585+
return
1586+
end
1587+
1588+
local ttl, err = dogs:ttl("updated_huge_ttl")
1589+
if not ttl then
1590+
ngx.say("err retrieving ttl: ", err)
1591+
return
1592+
end
1593+
1594+
ngx.say("ttl: ", ttl)
1595+
}
1596+
}
1597+
--- request
1598+
GET /t
1599+
--- response_body
1600+
ttl: 2147483648
1601+
--- no_error_log
1602+
[error]
1603+
[alert]
1604+
[crit]
1605+
1606+
1607+
1608+
=== TEST 49: init_ttl uses long type to avoid overflow in incr() + ttl()
1609+
--- http_config eval: $::HttpConfig
1610+
--- config
1611+
location = /t {
1612+
content_by_lua_block {
1613+
local dogs = ngx.shared.dogs
1614+
dogs:flush_all()
1615+
1616+
local ok, err = dogs:incr("incr_huge_ttl", 1, 0, 2 ^ 31)
1617+
if not ok then
1618+
ngx.say("err incr: ", err)
1619+
return
1620+
end
1621+
1622+
local ttl, err = dogs:ttl("incr_huge_ttl")
1623+
if not ttl then
1624+
ngx.say("err retrieving ttl: ", err)
1625+
return
1626+
end
1627+
1628+
ngx.say("ttl: ", ttl)
1629+
}
1630+
}
1631+
--- request
1632+
GET /t
1633+
--- response_body
1634+
ttl: 2147483648
1635+
--- no_error_log
1636+
[error]
1637+
[alert]
1638+
[crit]

0 commit comments

Comments
 (0)