Skip to content

Commit 6a04b13

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

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-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

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

0 commit comments

Comments
 (0)