Skip to content

Commit

Permalink
bugfix: timestamp could overflow on 32-bit systems in the shared dict…
Browse files Browse the repository at this point in the history
… API; now we use 64-bit integers.
  • Loading branch information
agentzh committed Mar 6, 2012
1 parent fcfdd73 commit 3dfd00a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
30 changes: 17 additions & 13 deletions src/ngx_http_lua_shdict.c
Expand Up @@ -124,8 +124,8 @@ ngx_http_lua_shdict_lookup(ngx_shm_zone_t *shm_zone, ngx_uint_t hash,
{
ngx_int_t rc;
ngx_time_t *tp;
ngx_msec_t now;
ngx_msec_int_t ms;
uint64_t now;
int64_t ms;
ngx_rbtree_node_t *node, *sentinel;
ngx_http_lua_shdict_ctx_t *ctx;
ngx_http_lua_shdict_node_t *sd;
Expand Down Expand Up @@ -159,14 +159,18 @@ ngx_http_lua_shdict_lookup(ngx_shm_zone_t *shm_zone, ngx_uint_t hash,

*sdp = sd;

dd("node expires: %lld", (long long) sd->expires);

if (sd->expires != 0) {
tp = ngx_timeofday();

now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
ms = (ngx_msec_int_t) (sd->expires - now);
now = (uint64_t) tp->sec * 1000 + tp->msec;
ms = sd->expires - now;

dd("time to live: %lld", (long long) ms);

if (ms < 0) {
/* already expired */
dd("node already expired");
return NGX_DONE;
}
}
Expand All @@ -187,16 +191,16 @@ static int
ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n)
{
ngx_time_t *tp;
ngx_msec_t now;
uint64_t now;
ngx_queue_t *q;
ngx_msec_int_t ms;
int64_t ms;
ngx_rbtree_node_t *node;
ngx_http_lua_shdict_node_t *sd;
int freed = 0;

tp = ngx_timeofday();

now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
now = (uint64_t) tp->sec * 1000 + tp->msec;

/*
* n == 1 deletes one or two expired entries
Expand All @@ -220,7 +224,7 @@ ngx_http_lua_shdict_expire(ngx_http_lua_shdict_ctx_t *ctx, ngx_uint_t n)
return freed;
}

ms = (ngx_msec_int_t) (sd->expires - now);
ms = sd->expires - now;
if (ms > 0) {
return freed;
}
Expand Down Expand Up @@ -717,8 +721,8 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)

if (exptime > 0) {
tp = ngx_timeofday();
sd->expires = (ngx_msec_t) (tp->sec * 1000 + tp->msec)
+ (ngx_msec_t) (exptime * 1000);
sd->expires = (uint64_t) tp->sec * 1000 + tp->msec
+ exptime * 1000;

} else {
sd->expires = 0;
Expand Down Expand Up @@ -816,8 +820,8 @@ ngx_http_lua_shdict_set_helper(lua_State *L, int flags)

if (exptime > 0) {
tp = ngx_timeofday();
sd->expires = (ngx_msec_t) (tp->sec * 1000 + tp->msec)
+ (ngx_msec_t) (exptime * 1000);
sd->expires = (uint64_t) tp->sec * 1000 + tp->msec
+ exptime * 1000;

} else {
sd->expires = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_lua_shdict.h
Expand Up @@ -10,7 +10,7 @@ typedef struct {
u_char dummy;
u_short key_len;
ngx_queue_t queue;
ngx_msec_t expires;
uint64_t expires;
uint8_t value_type;
uint32_t value_len;
uint32_t user_flags;
Expand Down
4 changes: 2 additions & 2 deletions t/043-shdict.t
Expand Up @@ -14,6 +14,7 @@ plan tests => repeat_each() * (blocks() * 2 + 5);
no_long_string();
#master_on();
#workers(2);

run_tests();

__DATA__
Expand Down Expand Up @@ -532,7 +533,7 @@ hello, world
--- request
GET /test
--- response_body_like
^true nil true\nabort at (?:139|141)$
^true nil true\nabort at (?:139|140)$
Expand Down Expand Up @@ -1066,7 +1067,6 @@ res = nil, flags = nil
ngx.say(val, " ", type(val))
val = dogs:get("bah")
ngx.say(val, " ", type(val))
';
}
--- request
Expand Down

0 comments on commit 3dfd00a

Please sign in to comment.