Skip to content

optimize: shdict: switched exptime argument type to 'long' to avoid potential overflows. #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ install:
- git clone https://github.com/openresty/openresty.git ../openresty
- git clone https://github.com/openresty/openresty-devel-utils.git
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
- git clone -b refactor/ttl-use-long-type https://github.com/thibaultcha/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
- git clone https://github.com/openresty/lua-resty-lrucache.git
Expand Down
8 changes: 4 additions & 4 deletions lib/resty/core/shdict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ ffi.cdef[[
int ngx_http_lua_ffi_shdict_store(void *zone, int op,
const unsigned char *key, size_t key_len, int value_type,
const unsigned char *str_value_buf, size_t str_value_len,
double num_value, int exptime, int user_flags, char **errmsg,
double num_value, long exptime, int user_flags, char **errmsg,
int *forcible);

int ngx_http_lua_ffi_shdict_flush_all(void *zone);

int ngx_http_lua_ffi_shdict_get_ttl(void *zone,
const unsigned char *key, size_t key_len);
long ngx_http_lua_ffi_shdict_get_ttl(void *zone,
const unsigned char *key, size_t key_len);

int ngx_http_lua_ffi_shdict_set_expire(void *zone,
const unsigned char *key, size_t key_len, int exptime);
const unsigned char *key, size_t key_len, long exptime);

size_t ngx_http_lua_ffi_shdict_capacity(void *zone);
]]
Expand Down
108 changes: 108 additions & 0 deletions t/shdict.t
Original file line number Diff line number Diff line change
Expand Up @@ -1528,3 +1528,111 @@ foo after init_ttl = nil
[error]
[alert]
[crit]



=== TEST 47: exptime uses long type to avoid overflow in set() + ttl()
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:flush_all()

local ok, err = dogs:set("huge_ttl", true, 2 ^ 31)
if not ok then
ngx.say("err setting: ", err)
return
end

local ttl, err = dogs:ttl("huge_ttl")
if not ttl then
ngx.say("err retrieving ttl: ", err)
return
end

ngx.say("ttl: ", ttl)
}
}
--- request
GET /t
--- response_body
ttl: 2147483648
--- no_error_log
[error]
[alert]
[crit]



=== TEST 48: exptime uses long type to avoid overflow in expire() + ttl()
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:flush_all()

local ok, err = dogs:set("updated_huge_ttl", true)
if not ok then
ngx.say("err setting: ", err)
return
end

local ok, err = dogs:expire("updated_huge_ttl", 2 ^ 31)
if not ok then
ngx.say("err expire: ", err)
return
end

local ttl, err = dogs:ttl("updated_huge_ttl")
if not ttl then
ngx.say("err retrieving ttl: ", err)
return
end

ngx.say("ttl: ", ttl)
}
}
--- request
GET /t
--- response_body
ttl: 2147483648
--- no_error_log
[error]
[alert]
[crit]



=== TEST 49: init_ttl uses long type to avoid overflow in incr() + ttl()
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local dogs = ngx.shared.dogs
dogs:flush_all()

local ok, err = dogs:incr("incr_huge_ttl", 1, 0, 2 ^ 31)
if not ok then
ngx.say("err incr: ", err)
return
end

local ttl, err = dogs:ttl("incr_huge_ttl")
if not ttl then
ngx.say("err retrieving ttl: ", err)
return
end

ngx.say("ttl: ", ttl)
}
}
--- request
GET /t
--- response_body
ttl: 2147483648
--- no_error_log
[error]
[alert]
[crit]